【发布时间】:2019-07-04 05:45:56
【问题描述】:
我目前有两个数据框。我希望使用 ggplot 在一个图中从它们中获取多个条形图。我想从两个数据帧中获得不同年份(1850-1950、1951-2012、2013-2100)的“NEE”变量的平均值,并并排绘制,就像在这个绿色条形图可视化(https://ars.els-cdn.com/content/image/1-s2.0-S0048969716303424-fx1_lrg.jpg)中一样。 两个数据帧的标头如下(这只是一部分)。1850-1859年的两个数据帧的标头相同:
我如何从一个图中的两个数据帧中绘制 1850-1852、1854-1856、1857-1859 年的条形图。我知道在这种情况下条形图将是相同的,因为两个数据框都相似,但我想知道一个想法,我可以将代码编辑到我想要的年份。 (请注意,我有 39125 个带有 9 个变量的 obs)
这是我到目前为止所做的(通过遵循本网站上成员发布的解决方案)。我成功实现了 data1 和 data2 geom_col。但是我怎样才能将它们合并在一起并绘制 1850-1852、1854-1856 的 geom_col , 1857-1859 并排从两个数据帧?graph of data1 graph of data2 :
data1 %>%
# case_when lets us define yr_group based on Year:
mutate(yr_group = case_when(Year <= 1950 ~ "1850-1950",
Year <= 2012 ~ "1951-2012",
Year <= 2100 ~ "2013-2100",
TRUE ~ "Other range")) %>%
# For each location and year group, get the mean of all the columns:
group_by(Lon, Lat, yr_group) %>%
summarise_all(mean) %>%
# Plot the mean Total for each yr_group
ggplot(aes(yr_group, NEE)) + geom_col(position =
"dodge")+theme_classic()+xlab("Year")+ylab(ln)+labs(subtitle="CCSM4
RCP2.6")+
geom_hline(yintercept=0, color = "black", size=1)
【问题讨论】:
-
您只想要一个以平均总计作为 y 的条形吗?
ggplot(df, aes(x = "1850-1860", mean(Total), fill = Total)) + geom_bar(stat="identity", position = "dodge")?? -
@Jon Spring 非常感谢!是否可以在一个图中获得两个 geom_bars。我有两个 geom_bar 图,我怎样才能将它们排列在一个图中(不是指 grid.arrange)。 (我尝试了多种解决方案但不适合我)
bar1<-ggplot(data1, aes(x = "1850-1860", mean(Total), fill = Total)) + geom_bar(stat="identity", position = "dodge",width=0.3) bar2<-ggplot(data1, aes(x = "2006-2100", mean(Total), fill = Total)) + geom_bar(stat="identity", position = "dodge",width=0.3)