在 ggplot2 中的堆积条形图上显示数据值 [英] Showing data values on stacked bar chart in ggplot2

查看:147
本文介绍了在 ggplot2 中的堆积条形图上显示数据值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Year <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4))类别 <- c(rep(c("A", "B", "C", "D"), times = 4))频率 <- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)数据 <- data.frame(Year, Category, Frequency)图书馆(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")

我想在每个部分的中间显示这些数据值.在这方面的任何帮助将不胜感激.谢谢

解决方案

来自

另请注意,position_stack()position_fill() 现在以分组的相反顺序堆叠值,这使得默认堆叠顺序与图例匹配."

<小时>

对旧版本的 ggplot 有效的答案:

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

库(ggplot2)图书馆(plyr)# 计算柱的中点(使用@DWin 的注释简化)数据 <- ddply(Data, .(Year),变换,pos = cumsum(频率)-(0.5 * 频率))# library(dplyr) ## 如果使用 dplyr ...# 数据 <- group_by(Data,Year) %>%# mutate(pos = cumsum(Frequency) - (0.5 * Frequency))# 绘制条形图并添加文本p <- ggplot(Data, aes(x = Year, y = Frequency)) +geom_bar(aes(fill = Category), stat="identity") +geom_text(aes(label = Frequency, y = pos), size = 3)

I'd like to show data values on stacked bar chart in ggplot2. Here is my attempted code

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

I'd like to show these data values in the middle of each portion. Any help in this regard will be highly appreciated. Thanks

解决方案

From ggplot 2.2.0 labels can easily be stacked by using position = position_stack(vjust = 0.5) in geom_text.

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

Also note that "position_stack() and position_fill() now stack values in the reverse order of the grouping, which makes the default stack order match the legend."


Answer valid for older versions of ggplot:

Here is one approach, which calculates the midpoints of the bars.

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)

这篇关于在 ggplot2 中的堆积条形图上显示数据值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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