使用ggplot2在facet躲避的barplot上添加文本 [英] Add text on top of a facet dodged barplot using ggplot2

查看:140
本文介绍了使用ggplot2在facet躲避的barplot上添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制两年不同年份的躲闪条形图,并将收入数据放在条形图的顶部。在尝试了一些建议后,我发现在这里,我仍然无法完全得到我想要的(所有数字都显示在中间的栏/列的中间,而不是均匀分布)。任何建议,将不胜感激。
$ b

我最近的尝试

 #禁用科学记数法
options(scipen= 100,digits= 1)

censusData< - 结构(列表(年份= c(2012L,2007L,2012L,2007L ,2012L,2007L,
2012L,2007L,2012L,2007L,2012L,2012L,2012L,2012L,2012L,
2007L,2012L,2007L,2012L,2007L,2012L, $ b),County = c(A,A,B,B,C,C,Sum,Sum,A,
A B,B,C,C,Sum,Sum,A,A,B,B,C,
(1L,1L,1L,1L,1L,
1L,1L,1L,2L,2L,2L,2L,2L,2L) ,2L,2L,3L,3L,3L,3L,3L,
3L,3L,3L),。标签= c(Total_Revenue,Crop_Revenue,L价值= c(1645.51,1203.806,68.9​​11,60.949,
923.163,525.918,2637.584,1790.673,1069.497,787.459,47.157,
38.735),价值= c(1645.51,1203.806,68.9​​11,60.949,
923.163,525.918,2637.584,1790.673,1069.497,787.459,47.157,
38.735 ,825.050228,470.024,1941.704228,1296.218,576.013,
416.347,21.754,22.214,98.112772,55.894,695.879772,494.455
)),row.names = c(NA,-24L),.Names = c(Year,County,variable,
value),class =data.frame)

#躲避的barplot
qbarplot_yr_1< ggplot(censusData,aes(县,值))+ facet_grid(。 〜年)+
geom_bar(aes(fill = variable),position =dodge,stat =identity)+
xlab(County)+ ylab(Revenue(Million USD) )+
scale_fill_discrete(name ='Legend',labels = c(Total,Crop,Livestocks))+
theme(axis.ticks.x = element_blank())+
theme(panel.background = element_rect(color ='dark gray'))+
theme(strip.text.x = element_text(size = 20,face =bold),
strip .background = element_rect(color =dark grey))

#在栏顶部添加文本
qbarplot_yr_1 + geom_text(data = censusData,
aes(x = County ,y = value + 150,label = format(value,nsmall = 0,scientific = FALSE)),
color =blue)

解决方案

您还需要避开文本值。尝试

  qbarplot_yr_1 + geom_text(data = censusData,
aes(x = County,group = variable,y = value + 150,
label = format(value,nsmall = 0,digits = 1,scientific = FALSE)),
color =blue,position = position_dodge(.9),hjust = .5)

我们还需要 group = 知道闪避的价值。

I'd like to plot a dodged barplot for two different years and put the revenue numbers on top of the bar accordingly. After trying quite some suggestion I found over here, I still can't quite get what I want (all the numbers were displayed in the middle of the middle bars/columns instead of spreading equally). Any suggestion would be appreciated. Thanks!

My latest attempt

# Disable scientific notation
options("scipen" = 100, "digits" = 1)

censusData <- structure(list(Year = c(2012L, 2007L, 2012L, 2007L, 2012L, 2007L, 
                                      2012L, 2007L, 2012L, 2007L, 2012L, 2007L, 2012L, 2007L, 2012L, 
                                      2007L, 2012L, 2007L, 2012L, 2007L, 2012L, 2007L, 2012L, 2007L
                                      ), County = c("A", "A", "B", "B", "C", "C", "Sum", "Sum", "A", 
                                      "A", "B", "B", "C", "C", "Sum", "Sum", "A", "A", "B", "B", "C", 
                                      "C", "Sum", "Sum"), variable = structure(c(1L, 1L, 1L, 1L, 1L, 
                                      1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
                                      3L, 3L, 3L), .Label = c("Total_Revenue", "Crop_Revenue", "Livestock_Revenue"
                                      ), class = "factor"), value = c(1645.51, 1203.806, 68.911, 60.949, 
                                      923.163, 525.918, 2637.584, 1790.673, 1069.497, 787.459, 47.157, 
                                      38.735, 825.050228, 470.024, 1941.704228, 1296.218, 576.013, 
                                      416.347, 21.754, 22.214, 98.112772, 55.894, 695.879772, 494.455
                                      )), row.names = c(NA, -24L), .Names = c("Year", "County", "variable", 
                                      "value"), class = "data.frame")

# Dodged barplot
qbarplot_yr_1 <- ggplot(censusData, aes(County, value)) + facet_grid(. ~ Year) +  
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  xlab("County") + ylab("Revenue (Million USD)") +
  scale_fill_discrete(name = 'Legend', labels=c("Total", "Crop", "Livestocks")) +
  theme(axis.ticks.x = element_blank()) +
  theme(panel.background = element_rect(colour='dark grey')) +
  theme(strip.text.x = element_text(size = 20, face="bold"),
        strip.background = element_rect(colour="dark grey"))

# Add text on top of the bar
qbarplot_yr_1 + geom_text(data = censusData,
                          aes(x = County, y = value + 150, label = format(value, nsmall = 0, scientific = FALSE)), 
                          color="blue")

解决方案

You need to dodge the text values as well. Try

qbarplot_yr_1 + geom_text(data = censusData,
         aes(x = County, group=variable, y = value + 150, 
         label = format(value, nsmall = 0, digits=1, scientific = FALSE)), 
         color="blue", position=position_dodge(.9), hjust=.5)

We also need the group= so it knows which values to dodge on.

这篇关于使用ggplot2在facet躲避的barplot上添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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