将图例的alpha值与ggplot2中的直方图匹配

时间:2018-10-31 19:13:32

标签: r ggplot2 histogram

我有一个data.frame pp,前几行看起来像这样,

  fill x
1 a    0.3048367
2 b    0.3318136
3 c    0.2081782
4 a    0.2969265
5 b    0.2637778
6 c    0.3509706

我想创建如下图,

library(ggplot2)
p <- ggplot(data = pp) + 
     geom_histogram(data=subset(pp, fill == 'a'),
         aes(x=x, y=..density.., fill=fill), alpha=0.3) + 
     geom_histogram(data=subset(pp, fill == 'b'),
         aes(x=x, y=..density.., fill=fill), alpha=0.3) + 
     geom_histogram(data=subset(pp, fill == 'c'),
         aes(x=x, y=..density.., fill=fill), alpha=0.3) +  
     scale_fill_manual(values = c("a" = "red", "b" = "green", c = "blue")) +  
     theme(legend.title=element_blank())

enter image description here

现在,图例中的alpha值与直方图的alpha值不匹配。我尝试添加,

p + guides(fill = guide_legend(override.aes = list(alpha = 0.3)))

但是我得到完全相同的情节。如何获得图例中的alpha值以匹配图?

注意:我必须使用geom_histogram三次,因为我希望直方图彼此重叠。颜色填充的特定填充也很重要。

4 个答案:

答案 0 :(得分:2)

您不必多次使用geom_hisogram()。料仓的位置由position参数控制,position='identity'将为您提供所需的内容,而默认值为position='stack'scale_fill_manual已经正确映射了颜色,即使使用单个geom_histogram()也请正确映射颜色,请尝试以下代码:

ggplot(pp, aes(x = x, y = ..density.., fill = fill)) +
  geom_histogram(alpha = .3, position = 'identity') +
  scale_fill_manual(values = c('a' = 'red', 'b' = 'green', 'c' = 'blue'))

答案 1 :(得分:1)

您可以在调用position_identity时使用geom_histogram来执行此操作,而不必调用3次该函数。这还将自动更新图例中的Alpha,因为Alpha仅被设置了1次(而不是原始代码中每个调用中的3次。这是使用一些模拟数据的示例:

library(tidyverse)

df <- data_frame(
  a = rnorm(1000, 0, 1),
  b = rnorm(1000, 2, 1),
  c = rnorm(1000, -2, 0.5)
) %>%
  gather(key = "fill", value = "x")

df
#> # A tibble: 3,000 x 2
#>    fill       x
#>    <chr>  <dbl>
#>  1 a     -0.951
#>  2 a      0.321
#>  3 a      0.551
#>  4 a      0.166
#>  5 a     -1.55 
#>  6 a      1.27 
#>  7 a     -0.224
#>  8 a     -0.243
#>  9 a     -1.48 
#> 10 a     -0.888
#> # ... with 2,990 more rows

ggplot(df, aes(x = x, fill = fill)) +
  geom_histogram(aes(y = ..density..), position = position_identity(), alpha = 0.5) +
  scale_fill_manual(values = c("a" = "red", "b" = "green", "c" = "blue")) +
  theme_classic() +
  theme(legend.title = element_blank())
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

reprex package(v0.2.1)于2018-10-31创建

答案 2 :(得分:1)

你快到了。

pp=data.frame(fill=rep(c('a','b','c'),100),x=rnorm(300))

library(ggplot2)
p <- ggplot(data = pp) + 
     geom_histogram(data=subset(pp, fill == 'a'),
         aes(x=x, y=..density.., fill=fill), alpha=0.3) + 
     geom_histogram(data=subset(pp, fill == 'b'),
         aes(x=x, y=..density.., fill=fill), alpha=0.3) + 
     geom_histogram(data=subset(pp, fill == 'c'),
         aes(x=x, y=..density.., fill=fill), alpha=0.3) +  
     scale_fill_manual(values = c("a" = "red", "b" = "green", c = "blue"),aesthetics=c(alpha=.3)) +  
     theme(legend.title=element_blank())

p

enter image description here

答案 3 :(得分:0)

我认为这段代码对您有用。

ggplot(pp,aes(x=x,stat(density))) + geom_histogram(data=subset(pp,fill == 'a'),fill = "red", alpha = 0.3,binwidth = 0.05) + geom_histogram(data=subset(pp,fill== 'b'),fill = "green", alpha = 0.3,binwidth = 0.10) + geom_histogram(data=subset(pp,fill== 'c'),fill = "blue", alpha = 0.3,binwidth = 0.15)+ theme(legend.title=element_blank())