ggplot中的条形图和geom_line [英] Bar graph and geom_line in ggplot

查看:72
本文介绍了ggplot中的条形图和geom_line的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在条形图上绘制geom_line,我的条形按年份填充.我的代码是:

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")

我要解决的问题是为芬兰,德国,意大利的每个国家画一条三线(从 net 净出口).

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

推荐答案

您应该改用构面.这样一来,很明显,您只是在一个国家内进行比较,而不是在国家之间进行比较.

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))

这篇关于ggplot中的条形图和geom_line的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆