在ggplot2中的堆积条形图上显示数据值

时间:2011-07-11 01:42:58

标签: r graphics ggplot2

我想在ggplot2中的堆积条形图上显示数据值。这是我尝试过的代码

Year      <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4))
Category  <- c(rep(c("A", "B", "C", "D"), times = 4))
Frequency <- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)
Data      <- data.frame(Year, Category, Frequency)
library(ggplot2)
p <- qplot(Year, Frequency, data = Data, geom = "bar", fill = Category,     theme_set(theme_bw()))
p + geom_text(aes(label = Frequency), size = 3, hjust = 0.5, vjust = 3, position =     "stack") 

enter image description here

我想在每个部分的中间显示这些数据值。在这方面的任何帮助将受到高度赞赏。感谢

2 个答案:

答案 0 :(得分:153)

可以使用position = position_stack(vjust = 0.5)中的geom_text轻松堆叠ggplot 2.2.0个标签。

ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency)) +
  geom_bar(stat = "identity") +
  geom_text(size = 3, position = position_stack(vjust = 0.5))

enter image description here

另请注意,“position_stack()position_fill()现在按照与分组相反的顺序堆叠值,这使得默认堆栈顺序与图例匹配。”


答案适用于旧版ggplot

这是一种计算条形中点的方法。

library(ggplot2)
library(plyr)

# calculate midpoints of bars (simplified using comment by @DWin)
Data <- ddply(Data, .(Year), 
   transform, pos = cumsum(Frequency) - (0.5 * Frequency)
)

# library(dplyr) ## If using dplyr... 
# Data <- group_by(Data,Year) %>%
#    mutate(pos = cumsum(Frequency) - (0.5 * Frequency))

# plot bars and add text
p <- ggplot(Data, aes(x = Year, y = Frequency)) +
     geom_bar(aes(fill = Category), stat="identity") +
     geom_text(aes(label = Frequency, y = pos), size = 3)

Resultant chart

答案 1 :(得分:19)

正如hadley所提到的,与堆积条形图中的标签相比,有更有效的方式来传达您的信息。事实上,堆积图表不是很有效,因为条形图(每个类别)不共享轴,因此比较困难。

在这些实例中使用两个图表,共享一个公共轴几乎总是更好。在您的示例中,我假设您要显示总体总数,然后显示每个类别在给定年份中贡献的比例。

library(grid)
library(gridExtra)
library(plyr)

# create a new column with proportions
prop <- function(x) x/sum(x)
Data <- ddply(Data,"Year",transform,Share=prop(Frequency))

# create the component graphics
totals <- ggplot(Data,aes(Year,Frequency)) + geom_bar(fill="darkseagreen",stat="identity") + 
  xlab("") + labs(title = "Frequency totals in given Year")
proportion <- ggplot(Data, aes(x=Year,y=Share, group=Category, colour=Category)) 
+ geom_line() + scale_y_continuous(label=percent_format())+ theme(legend.position = "bottom") + 
  labs(title = "Proportion of total Frequency accounted by each Category in given Year")

# bring them together
grid.arrange(totals,proportion)

这将为您提供如下所示的2面板显示:

Vertically stacked 2 panel graphic

如果您想添加频率值,表格是最佳格式。