【问题标题】:How to add a single label to a stacked bar chart in GGplot2如何将单个标签添加到GGplot2中的堆叠条形图
【发布时间】:2018-04-25 16:19:11
【问题描述】:

我想在 GGplot2 的堆积条形图中添加一个数字标签。

我有以下代码:

   # Load the packages
library(dplyr)
library(readr)
library(tidyr)
library(ggplot2)
library(RColorBrewer)

# Create a data set called M
M <- structure(list(X1 = c("Diversity", "Endangerment", "Marketability", "Total"),
B1 = c(5.1, 4.9, 4.7, 4.6), B2 = c(3.5, 
3, 3.2, 3.1), B3 = c(1.4, 1.4, 1.3, 1.5), Petal.Width = c(7.1, 
4.7, 3.2, 5.1)), .Names = c("X1", "B1", "B2", "B3", "B4"), row.names = c(NA, 
4L), class = "data.frame")
M

# Plot the stacked bar 
P <- M %>% 
  gather(variable, value, -X1) %>% 
  filter(X1!="Total") %>% #Removes total variable
  group_by(variable) %>% 
  mutate(sum=sum(value)) %>% 
  ungroup() %>% 
  mutate(variable=reorder(variable, -sum)) %>% #Reorders the chart based on breed total score
  ggplot(aes(variable, value, fill=X1, width =.7)) +
  geom_col(col="black") + #Black border around the bars
  expand_limits(y = 0) +
  scale_y_continuous(limits = c(0,70), breaks=c(0,10,20, 30,40, 50,60, 70), expand = c(0.02, 0.02))+
  geom_text(aes(label = sum), vjust = -.25)+ #Here the text is added
  scale_fill_brewer(palette="Dark2") + #Changes the colour scale
  labs(x="Breed",
       y="Weighted score",
       fill="") +
  theme_bw() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.direction="vertical", legend.position=c(0.9, 0.85), 
        legend.text=element_text(size=11),
        legend.key = element_rect(size = 4),
        legend.key.size = unit(1.5, 'lines'))  # Ensures spacing between the different lines on the legend.
P

我想尝试在每个条形上方包含“总和”值。有什么想法我该怎么做吗?

【问题讨论】:

    标签: r ggplot2 label stacked-chart


    【解决方案1】:

    回答

    所以,我创建了另一个 data.frame,但使用 summarise 后,我们将只有 1 个总和值。

    代码

    df <- structure(list(X1 = c("Diversity", "Endangerment", "Marketability", "Total"),
                    B1 = c(5.1, 4.9, 4.7, 4.6), B2 = c(3.5, 
                                                       3, 3.2, 3.1), B3 = c(1.4, 1.4, 1.3, 1.5), Petal.Width = c(7.1, 
                                                                                                                 4.7, 3.2, 5.1)), .Names = c("X1", "B1", "B2", "B3", "B4"), row.names = c(NA, 
                                                                                                                                                                                          4L), class = "data.frame")
    
    df %>% 
      gather(variable, value, -X1) %>% 
      filter(X1!="Total") %>% 
      group_by(variable) %>% 
      mutate(sum=sum(value)) %>% 
      ungroup() %>% 
      mutate(variable=reorder(variable, -sum)) ->df
    
    df %>% 
      group_by(variable) %>% 
      summarise(max = max(sum)) ->df_max
    
    df %>% 
      ggplot(aes(variable, value, width =.7)) +
      geom_col(col="black",aes(fill=X1)) + #Black border around the bars
      expand_limits(y = 0) +
      scale_y_continuous(limits = c(0,70), breaks=c(0,10,20, 30,40, 50,60, 70),         expand = c(0.02, 0.02))+
      geom_text(data = df_max,
            aes(label = max, y = max), vjust = -.25)+ #Here the text is added
      scale_fill_brewer(palette="Dark2") + #Changes the colour scale
      labs(x="Breed",
           y="Weighted score",
           fill="") +
      theme_bw() +
      theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.direction="vertical", legend.position=c(0.9, 0.85), 
        legend.text=element_text(size=11),
        legend.key = element_rect(size = 4),
        legend.key.size = unit(1.5, 'lines'))  # Ensures spacing between the different lines on the legend.
    

    输出

    【讨论】:

      【解决方案2】:

      如果我没有正确理解,你想要这样的东西:

      mtcars %>% 
        group_by(cyl=as.factor(cyl) ) %>% 
        summarise(sum= sum(am)) %>% 
        ggplot(aes(cyl, sum))+
        geom_col(width = .5)+
        geom_text(aes(label = sum), vjust = -.25)+ #Here the text is added
        scale_y_continuous(limits = c(0,8.5)) 
      

      【讨论】:

      • 这几乎可行,但现在我得到了三批相同的总数(放置在堆叠条的每个类别中),而不是像上面的示例中那样在条的顶部有一个总数。有什么建议吗?
      • 您能否提供一个可复制的数据示例?
      • 我现在已经编辑了最初的帖子,并添加了一个可重现的例子 Vinicius。
      猜你喜欢
      • 2022-01-21
      • 2012-01-04
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      相关资源
      最近更新 更多