首页 文章

R:ggplot堆积条形图,y轴计数,但百分比为标签

提问于
浏览
5

我正在寻找一种方法来标记带百分比的堆积条形图,而y轴显示原始计数(使用ggplot) . 这是没有标签的情节的MWE:

library(ggplot2)
df <- as.data.frame(matrix(nrow = 7, ncol= 3,
                       data = c("ID1", "ID2", "ID3", "ID4", "ID5", "ID6", "ID7",
                                "north", "north", "north", "north", "south", "south", "south",
                                "A", "B", "B", "C", "A", "A", "C"),
                      byrow = FALSE))

colnames(df) <- c("ID", "region", "species")

p <- ggplot(df, aes(x = region, fill = species))
p  + geom_bar()

我有一个更大的 table ,R很好地计算每个地区的不同物种 . 现在,我想展示原始计数值(最好在y轴上)和百分比(作为标签)来比较区域之间物种的比例 .

我用 geom_text() 试了很多东西,但我认为与其他问题(e.g. this one)的主要区别在于

  • 我没有y值的单独列(它们只是每个区域不同物种的数量)和

  • 我需要每个区域的标签总和达到100%(因为它们被认为代表单独的种群),而不是整个图的所有标签 .

任何帮助深表感谢!!

2 回答

  • 7

    正如@Gregor所提到的,分别汇总数据,然后将数据汇总提供给ggplot . 在下面的代码中,我们使用 dplyr 动态创建摘要:

    library(dplyr)
    
    ggplot(df %>% count(region, species) %>%    # Group by region and species, then count number in each group
             mutate(pct=n/sum(n),               # Calculate percent within each region
                    ypos = cumsum(n) - 0.5*n),  # Calculate label positions
           aes(region, n, fill=species)) +
      geom_bar(stat="identity") +
      geom_text(aes(label=paste0(sprintf("%1.1f", pct*100),"%"), y=ypos))
    

    enter image description here

    Update: 使用 dplyr 0.5及更高版本,您不再需要提供y值来居中每个栏中的文本 . 相反,您可以使用 position_stack(vjust=0.5)

    ggplot(df %>% count(region, species) %>%    # Group by region and species, then count number in each group
             mutate(pct=n/sum(n)),              # Calculate percent within each region
           aes(region, n, fill=species)) +
      geom_bar(stat="identity") +
      geom_text(aes(label=paste0(sprintf("%1.1f", pct*100),"%")), 
                position=position_stack(vjust=0.5))
    
  • 1

    我同意约翰娜的观点 . 你可以尝试:

    d <- aggregate(.~region+species, df, length)
    d$percent <- paste(round(ID/sum(ID)*100),'%',sep='')
    ggplot(d, aes(region, ID, fill=species)) + geom_bar(stat='identity') + 
      geom_text(position='stack', aes(label=paste(round(ID/sum(ID)*100),'%',sep='')), vjust=5)
    

相关问题