本文介绍了使用ggplot绘制柱状图中柱状图的观察数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想在直方图的上方添加每个变量的观察数量。这是我的数据框。 表示< - 结构(列表(Driver = c(Crop agriculture,Crop agriculture, 基础设施,基础设施,采矿,采矿,混合农业,混合农业,其他土地利用,其他土地利用,牧场, 草地,树木作物,水,水),时期= c(1990-2000,2000-2005,1990-2000, 2000-2005,1990-2000,2000-2005,1990-2000,2000-2005,1990-2000,2000-2005,1990-2000 ,2000-2005,1990-2000,2000-2005,1990-2000,2000-2005),均值= c(36.2697273704497,61.3311804191792,28.7523209391483, 30.955220240622,49.4900570536558,41.5037095947389,13.1454310847706, 10.3642833385884,28.5871996967629,23.3988064930454,53.9768942854543, 61.3460606144189,17.3260123546446,16.1278503954073,36.6165841628849, 32.6896740558384),N = C(N = 1669,n = 783,n = 298,n = 151,n = 20,n = 7,n = 1355,n = 925 n = 1623 ,n = 851,n = 10986,n = 6039,n = 316,n = 211,n = 466,n = 244)),.Names = c(Driver,Period,mean,n),class =data.frame,row.names = c(NA, -16L)) 基于这个数据框,这就是我想要绘制的。 means.barplot geom_bar(position =dodge,stat =identity)+ labs(x =,y =EF(T / ha))+ theme(axis.text = element_text(size = 16), axis.title = element_text(size = 20), legend.title = element_text(size = 20,face =bold), legend.text = element_text(size = 20), axis.line = element_line(color =black))+ scale_fill_grey(Period)+ scale_y_continuous限制= c(0,70))+ theme_classic(base_size = 20,base_family =)+ theme(panel.grid.minor = element_line(color =gray,size = 0.5) ) 然而,我也想为这个图表添加每个变量的观察值(driver for在这种情况下的每个时期)。我还没有设法做到这一点。有人可以帮我解决这个问题吗?谢谢。 解决方案一种方法是使用 ggplot_build()和注释()。您想通过创建一次ggplot对象来获取x和y位置的值。这里的对象是 g 。当您使用 ggplot_build(objectname)$ data [[1]] 时,您可以获取所需的值。在 annotate()中,可以使用 foo 中的x和y值以及所需的标签(即mydf $ n )。作为额外的补充,我添加了主题(axis.text.x = element_text(angle = 45,hjust = 1))来修改x轴标签。 g geom_bar(position =dodge,stat =identity)+ labs(x =,y =EF(T / ha))+ theme(axis.text = element_text (size = 16), axis.title = element_text(size = 20), legend.title = element_text(size = 20,face =bold), legend.text = element_text(size = 20), axis.line = element_line(color =black))+ scale_fill_grey(Period)+ scale_y_continuous(limits = c(0,70) )+ theme_classic(base_size = 20,base_family =)+ theme(panel.grid.minor = element_line(color =gray,size = 0.5))+ theme( axis.text.x = element_text(angle = 45,hjust = 1)) ###为annotate()创建一个数据框 foo< - ggplot_b uild(g)$ data [[1]] g + annotate(text,x = foo $ x,y = foo $ y + 1,label = mydf $ n,size = 3) I want to add above the bar of an histogram the number of observations for each variables. Here is my dataframe. means <- structure(list(Driver = c("Crop agriculture", "Crop agriculture","Infrastructure", "Infrastructure", "Mining", "Mining", "Mixed Agriculture","Mixed Agriculture", "Other land use", "Other land use", "Pasture","Pasture", "Tree crops", "Tree crops", "Water", "Water"), Period = c("1990-2000","2000-2005", "1990-2000", "2000-2005", "1990-2000", "2000-2005","1990-2000", "2000-2005", "1990-2000", "2000-2005", "1990-2000","2000-2005", "1990-2000", "2000-2005", "1990-2000", "2000-2005"), mean = c(36.2697273704497, 61.3311804191792, 28.7523209391483,30.955220240622, 49.4900570536558, 41.5037095947389, 13.1454310847706,10.3642833385884, 28.5871996967629, 23.3988064930454, 53.9768942854543,61.3460606144189, 17.3260123546446, 16.1278503954073, 36.6165841628849,32.6896740558384), n = c("n = 1669", "n = 783", "n = 298", "n = 151","n = 20", "n = 7", "n = 1355", "n = 925", "n = 1623", "n = 851","n = 10986", "n = 6039", "n = 316", "n = 211", "n = 466", "n = 244")), .Names = c("Driver", "Period", "mean", "n"), class = "data.frame", row.names = c(NA,-16L))Based on this dataframe, this is what I want to plot. means.barplot <- ggplot(means, aes(x = Driver, y = mean, fill = Period, width = .85)) +geom_bar(position = "dodge", stat = "identity") +labs(x = "", y = "EF (T/ha)") +theme(axis.text = element_text(size = 16), axis.title = element_text(size = 20), legend.title = element_text(size = 20, face = "bold"), legend.text = element_text(size = 20), axis.line = element_line(colour = "black")) +scale_fill_grey("Period") +scale_y_continuous(limits = c(0, 70)) +theme_classic(base_size = 20, base_family = "") +theme(panel.grid.minor = element_line(colour =" grey", size = 0.5))However, I also want to add to this plot the mumber of observations for each variables (driver for each period in this case). I have not managed to do that yet. Can someone help me out with that? Thanks. 解决方案 One way is to use ggplot_build() and annotate(). You want to get values for x and y positions by creating a ggplot object once. Here, the object is g. When you use ggplot_build(objectname)$data[[1]], you can obtain the values you need. In annotate(), you use x and y values in foo and the label you want (i.e., mydf$n). As an extra, I added theme(axis.text.x = element_text(angle = 45, hjust = 1)) to modify x axis label.g <- ggplot(mydf, aes(x = Driver, y = mean, fill = Period, width = .85)) + geom_bar(position = "dodge", stat = "identity") + labs(x = "", y = "EF (T/ha)") + theme(axis.text = element_text(size = 16), axis.title = element_text(size = 20), legend.title = element_text(size = 20, face = "bold"), legend.text= element_text(size=20), axis.line = element_line(colour = "black")) + scale_fill_grey("Period") + scale_y_continuous(limits=c(0,70)) + theme_classic(base_size = 20, base_family = "") + theme(panel.grid.minor = element_line(colour = "grey", size = 0.5)) + theme(axis.text.x = element_text(angle = 45, hjust = 1))### Create a data frame for annotate()foo <- ggplot_build(g)$data[[1]]g + annotate("text", x = foo$x, y = foo$y + 1, label = mydf$n, size = 3) 这篇关于使用ggplot绘制柱状图中柱状图的观察数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-21 13:01