Reputation: 139
I am trying to draw a geom_line on a bar chart, my bars are filled by year. My 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 I am trying to resolve is to draw a three lines (net export from net
) for each country Finland, Germany, Italy.
With my last code line i am getting only three point which are connected with lines
Upvotes: 1
Views: 2216
Reputation: 22313
You should use facets instead. That way it is clear that you are only comparing within one country and 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))
Upvotes: 3