Massimo2013
Massimo2013

Reputation: 593

Not able to order fill colors as desired

I need to produce a plot with two separate geom_area commands, in order to draw some time series above and some below the zero line. Here is a simple example:

library("reshape2")
library("ggplot2")

d <- as.data.frame(t(read.table(sep=";",header=F,row.names=1,text="
year;1999;2000;2001;2002;2003;2004;2005;2006;2007;2008;2009;2010;2011;2012
primary balance;-5.63;-11.88;-15.37;-18.3;-20.09;-21.45;-21.87;-23.25;-26.98;-29.56;-28.92;-28.46;-29.64;-32.61
snow-ball effect;1.61;0.81;2.67;4.99;7.23;8.02;9.45;9.6;11.01;14.06;22.81;24.41;25.76;26.89
adjustment;2.83;5.38;6.52;3.93;2.28;2.45;3.94;5.28;4.5;6.73;6.94;7.59;7.73;8.07")))
dd <- melt(d,id.vars="year")
dd1 <- subset(dd,variable=="primary balance")
dd2 <- subset(dd,variable!="primary balance")
ggplot()+
  geom_area(data=dd1,aes(x=year,y=value,fill=variable,order=variable),alpha=.5)+
  geom_area(data=dd2,aes(x=year,y=value,fill=variable,order=variable),alpha=.5)

The plot is: plot

Although the order of levels for variable is: (1) "primary balance", (2) "snow-ball effect" and (3) "adjustment", there is now way I can tell ggplot to assign colors and put items in the legend in the correct order.

Upvotes: 1

Views: 1956

Answers (3)

Mika Prouk
Mika Prouk

Reputation: 515

dd <- melt(d,id.vars="year")
o <- ordered(c("primary balance","snow-ball effect","adjustment"), c("primary balance","snow-ball effect","adjustment"))
dd$variable <- ordered(dd$variable, o)
dd1 <- subset(dd,variable=="primary balance")
dd2 <- subset(dd,variable!="primary balance")
p <- ggplot()+ geom_area(data=dd1,aes(x=year,y=value,fill=variable), alpha=.5)
p <- p +  geom_area(data=dd2,aes(x=year,y=value,fill=variable), alpha=.5)
p <- p + scale_fill_manual(values = setNames(scales::hue_pal()(length(levels(dd$variable))), o),
                           breaks = o)
p

Do you want it in that way? The problem is ggplot sorts factors alphabetically. So colors might be changed. With ordered you generate a factor and tell how its levels are sorted. enter image description here

Upvotes: 2

rcs
rcs

Reputation: 68809

You can change the order using scale_fill_manual(values=..., breaks=...):

ggplot() +
geom_area(data=dd1,
          aes(x=year, y=value, fill=variable, order=variable), 
          alpha=.5) +
geom_area(data=dd2,
          aes(x=year, y=value, fill=variable, order=variable),
          alpha=.5) +
scale_fill_manual(values=scales::hue_pal()(3),
                  breaks=c("adjustment",
                           "snow-ball effect",
                           "primary balance"))

plot

Upvotes: 6

Haritha Elango
Haritha Elango

Reputation: 88

You can use scale_fill_manual to manually choose colours and decide the ordering in the legend.

ggplot()
+geom_area(data=dd1,aes(x=year,y=value,fill=variable,order=variable),alpha=.5)
+geom_area(data=dd2,aes(x=year,y=value,fill=variable,order=variable),alpha=.5)
+scale_fill_manual(values=c("primary balance"="yellow","snow-ball effect" = "violet","adjustment" = "green"),breaks = levels(dd$variable))

enter image description here

Upvotes: 2

Related Questions