r - Bar graph and geom_line in ggplot -
i trying draw geom_line on bar chart, bars filled year. code is:
library(ggplot2) library(plyr) library(reshape) df <- data.frame(decl.indicators=c("finland", "finland", "germany" ,"germany","italy","italy"), year=c(2009,2010,2009,2010,2009,2010), expval=c(2136410,1462620,371845300,402397520,357341970,357341970), impval=c(-33668520,-37837140,-283300110,-306157870,-103628920,-105191850)) net <- ddply(df, .(year,decl.indicators), summarise, net = sum(expval + impval)) df.m <- melt(df, id.vars = c("year", "decl.indicators")) ggplot(df.m,aes(x=decl.indicators,y=value, fill=factor(year)))+ geom_bar(stat="identity",position="dodge",colour="darkgreen") last_plot() + geom_line(data = net, aes(decl.indicators, net,group = 1), size = 1) + geom_hline(yintercept = 0,colour = "grey90")
problem trying resolve draw 3 lines (net export net
) each country finland, germany, italy.
with last code line getting 3 point connected lines
you should use facets instead. way clear comparing within 1 country , not between countries.
ggplot(df.m, aes(x = factor(year), y = value, fill = factor(year))) + geom_bar(stat = "identity", position = "dodge", colour="darkgreen") + facet_grid(~decl.indicators) + geom_line(data = net, aes(y = net, group = 1))
Comments
Post a Comment