【问题标题】:Add extra value to ggplot barplot为 ggplot barplot 添加额外的价值
【发布时间】:2017-08-02 08:28:55
【问题描述】:

我有以下数据:

Sp  Type    Val1    Val2
A   One     200     50
A   Two     100     10
C   One     300     150
C   Two     200     10

我做了以下得到堆积条形图:

ggplot() +
  geom_bar(data=test, aes(y = Val1, x = Sp, fill = Type), stat="identity",position='stack')

因此,我得到了 A、B 的两个堆叠条,每个条都有类型 1 和 2 的堆叠(A 的总大小为 200+100 =300)。在这里,val2 是每个类型中未知数的一部分。如何将它覆盖在堆栈的各个部分?即在 Val1 中的 A 型中,未知的分数是 Val2。

提前致谢。

AP

【问题讨论】:

  • 请更准确。是否要将Val2 覆盖在Val1 上方。或者你想要一个包含两个值的堆积条形图?
  • 它是堆叠的条形图,带有 Val2 阴影。我的意思是我想在 Val1 栏中添加 Val2。即A将有2个堆栈,1个200个,2个100个。在200个堆栈中,我想用50和100用10着色一个区域。如果我们有50和10个颜色相同会更好,这样,它看起来像在两个堆栈中都标记了比例!。
  • 好的,那就看看我的回答吧!其他的只是总结了 Val1 和 Val2。

标签: r ggplot2 bar-chart stackedbarseries


【解决方案1】:

这是你要找的吗?:

library(ggplot2)
data <- data.frame(Sp  = c("A","A","C","C"), 
                   Type = c("one","two","one","two"), 
                   Val1 = c(200,100,300,200),
                   Val2 = c(50,10,150,10))
library(reshape2)
data <- melt(data, id=c("Sp","Type"))
data$Type2 <- paste(data$Type, data$variable, sep="_")

[更新] 融化后得到的数据:

 Sp Type variable value    Type2
1  A  one     Val1   200 one_Val1 # it has value of 200 for Sp A
2  A  two     Val1   100 two_Val1 # it has value of 100 for Sp A
3  C  one     Val1   300 one_Val1
4  C  two     Val1   200 two_Val1
5  A  one     Val2    50 one_Val2
6  A  two     Val2    10 two_Val2
7  C  one     Val2   150 one_Val2
8  C  two     Val2    10 two_Val2

而 one_Val1 等于 200 和 two_Val1 等于 100 --> 200 + 100 = 300

ggplot() +
  geom_bar(data=data, aes(y = value, x = Sp, fill = Type2), stat="identity",position='stack')

我首先融化了您的数据,以便在一列中获取 Val1 和 Val2 的值,以便进一步使用它并将其与 Type 列一起粘贴。

【讨论】:

  • A 的总大小与您的解决方案之和未达到 300 (200+100)。
  • one_Val1 and two_Val1 = 300 --> 这是 Sp: A for Type one
  • 这不是真的。
  • 如果我认为你错误地总结了这个问题,说:你想覆盖堆栈的各个选项......实际上不是很清楚
【解决方案2】:

你可以试试:

d$xmin <- rep(c(0.55, 1.55),each=2)
d$xmax <- rep(c(1.45, 2.45),each=2)
d$ymin <- c(100, 0, 200, 0)
d$ymax <- c(150, 10, 350, 10)

ggplot(d) + 
    geom_col(aes(x=Sp, y=Val1, fill=Type)) +
    geom_rect(aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), alpha=0.5) 

这个想法是在条上手动添加矩形(我在这里使用 geom_col 因为这个函数默认使用stat_identity)。因此,您可以自己计算最小值和最大值,并添加一些 alpha 以覆盖条形图。

或者您可以尝试更自动的dplyr 解决方案:

library(tidyverse)
d %>% 
  arrange(Sp, -as.numeric(Type)) %>% 
  mutate(ymin=ifelse(Type=="One",lag(Val1),0),
         ymax=ifelse(Type=="Two",Val2, lag(Val1)+Val2)) %>% 
  mutate(Sp_n=as.numeric(Sp)) %>% 
  ggplot() + 
  geom_col(aes(x=Sp_n, y=Val1, fill=Type))+
  geom_rect(aes(xmin=Sp_n-0.45, xmax=Sp_n+0.45, ymin=ymin, ymax=ymax),
  fill="white", alpha= 0.7) +
  scale_x_continuous(breaks = 1:2, labels = unique(d$Sp))

【讨论】:

    【解决方案3】:

    如果您希望它按 Val1/Val2 值划分

    library(ggplot2)
    library(reshape2)
    test <- melt(test, id=c("Sp","Type"))
    ggplot(data=test) +
      geom_bar(aes(y = value, x = Sp, fill = Type), stat="identity",position='stack')+
      facet_wrap(~variable)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 2019-11-10
      • 2019-10-08
      • 2020-03-25
      相关资源
      最近更新 更多