質問

Data: Data Data1: Data1

Code:

CashSurDef <- ggplot(data=wdiSSA_CashSurDef.df,aes(x=Country.Name, y=avg, fill=colour))
CashSurDef <- CashSurDef + geom_bar(stat="identity",position=position_dodge(),alpha=.75) + theme_bw()
CashSurDef <- CashSurDef + scale_fill_manual(breaks = wdiSSA_CashSurDef.df$Country.Name,
                                    values = unique(as.character(wdiSSA_CashSurDef.df$colour)))
CashSurDef <- CashSurDef + scale_y_continuous(breaks=seq(-25,25,2.5)) + geom_hline(yintercept=0, linetype="dashed", colour="blue", size=1.1)
CashSurDef <- CashSurDef + labs(title="Cash surplus/deficit (% of GDP) (Average 2005-2012)", y="", x="Country") + coord_flip() 
CashSurDef <- CashSurDef + theme(plot.title=element_text(face="bold", size=rel(2), hjust=0.5, vjust=1.5),
                            axis.text.x=element_text(color="black", size=rel(1.8), hjust=0.5, vjust=0.5),
                            axis.text.y=element_text(color="black", size=rel(1.8), hjust=1),
                            axis.title.x=element_text(face="bold", color="black", size=rel(1.6), hjust=0.5, vjust=0.2),
                            axis.title.y=element_text(color="black", size=rel(1.6), hjust=0.5, vjust=0.2),
                            strip.text=element_text(face="bold", size=rel(1.8)),
                            legend.text=element_text(face="bold", size=rel(1.25)),
                            legend.title=element_blank(),
                            legend.position="bottom")
CashSurDef

Code1:

Unemploy <- ggplot(data=wdiSSA_Unemploy.df,aes(x=Country.Name, y=avg, fill=colour))
Unemploy <- Unemploy + geom_bar(stat="identity",position=position_dodge(),alpha=.75) + theme_bw()
Unemploy <- Unemploy + scale_fill_manual(breaks = wdiSSA_Unemploy.df$Country.Name,
                                    values = unique(as.character(wdiSSA_Unemploy.df$colour)))
Unemploy <- Unemploy + scale_y_continuous(breaks=seq(0,35,5)) + geom_hline(yintercept=5, linetype="dashed", colour="blue", size=1.1)
Unemploy <- Unemploy + labs(title="Average unemployment rate (2005-2012), total (%)", y="", x="Country") + coord_flip()
Unemploy <- Unemploy + theme(plot.title=element_text(face="bold", size=rel(2), hjust=0.5, vjust=1.5),
                        axis.text.x=element_text(color="black", size=rel(1.8), hjust=0.5, vjust=0.5),
                        axis.text.y=element_text(color="black", size=rel(1.8), hjust=1),
                        axis.title.x=element_text(face="bold", color="black", size=rel(1.6), hjust=0.5, vjust=0.2),
                        axis.title.y=element_text(color="black", size=rel(1.6), hjust=0.5, vjust=0.2),
                        strip.text=element_text(face="bold", size=rel(1.8)),
                        legend.text=element_text(face="bold", size=rel(1.25)),
                        legend.title=element_blank(),
                        legend.position="bottom")
Unemploy

Result: Result Result1: Result1

Question: I am trying to plot the data and use the predefined fill captured by the column "colour" in "Data", and which was added based on the conditional statement: if(avg>0, tan3, grey). When I plot though, the colour filling gets reversed as you may observe in the picture "Result". I applied the same method for other variables in my data (cf. "Data1", "Result1"), and it worked perfectly. Any thoughts, suggestions?

Cheers

役に立ちましたか?

解決

First, you left out the code to reorder the country names factor by avg, so your code does not produce these plots.

Second, using a color name in a data frame doesn't help you. ggplot(...,aes(fill=...)) interprets that column as a factor to distinguish between groups. It assigns it's own colors, which is why you need to use scale_fill_manual(...) to actually set the colors to something you want.

Third, you don't need breaks=... in the call to scale_fill_manual(...).

Fourth, and this is the answer to your main question, unique(as.character(df$colours)) returns the colors in an order based on when they appear in df. In the cash flow table, tan3 appears first, in the unemployment table grey appears first. One way around this is to replace your calls

scale_fill_manual(breaks = df$Country.Name, values = unique(df$colour)))

with

scale_fill_manual(values = 
            as.character(setNames(unique(df$colour),unique(df$colour))))

with df replaced by the names of your data frames. This creates a named vector of colors, where the color name and the color are the same, e.g.

c(tan3="tan3",grey="grey",NA)

which will associate the colors properly.

Finally, IMO you would be much better off doing something like this (here I'm using cash as the name of your dataframe)

...
ggplot(data=cash,aes(x=Country.Name, y=avg, 
                     fill=(ifelse(avg>0,"Positive","Negative"))))+
   geom_bar(stat="identity",position=position_dodge(),
            alpha=.75) +
   scale_fill_manual(values = c(Positive="grey",Negative="tan3"))
...
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top