R 将图例的alpha值与ggplot2中的直方图匹配
我有一个data.frameR 将图例的alpha值与ggplot2中的直方图匹配,r,ggplot2,histogram,R,Ggplot2,Histogram,我有一个data.framepp,前几行是这样的 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'),
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())
但我得到了完全相同的情节。如何获取图例中的alpha值以匹配绘图
注意:我必须使用
geom_直方图
三次,因为我希望直方图彼此重叠。特定的填充到颜色映射也很重要 我认为这段代码适合您
ggplot(pp,aes(x=x,统计(密度)))+
几何图形直方图(数据=子集(pp,fill='a'),fill=“红色”,alpha=0.3,binwidth=0.05)+
geom_直方图(数据=子集(pp,fill='b'),fill=“绿色”,alpha=0.3,binwidth=0.10)+
geom_直方图(数据=子集(pp,fill='c'),fill=“blue”,alpha=0.3,binwidth=0.15)+
主题(legend.title=element\u blank())
我认为这个代码适合你
ggplot(pp,aes(x=x,统计(密度)))+
几何图形直方图(数据=子集(pp,fill='a'),fill=“红色”,alpha=0.3,binwidth=0.05)+
geom_直方图(数据=子集(pp,fill='b'),fill=“绿色”,alpha=0.3,binwidth=0.10)+
geom_直方图(数据=子集(pp,fill='c'),fill=“blue”,alpha=0.3,binwidth=0.15)+
主题(legend.title=element\u blank())
您可以在调用geom\u直方图时使用position\u identity
执行此操作,而无需调用函数3次。这也将自动更新图例中的alpha,因为alpha只设置了1次(而不是原始代码中每个调用中的3次)。以下是使用一些模拟数据的示例:
库(tidyverse)
df%
聚集(key=“fill”,value=“x”)
df
#>#A tible:3000 x 2
#>填充x
#>
#>1A-0.951
#>2 a 0.321
#>3 a 0.551
#>4 a 0.166
#>5a-1.55
#>6 a 1.27
#>7a-0.224
#>8a-0.243
#>9a-1.48
#>10 a-0.888
#>#…还有2990行
ggplot(df,aes(x=x,fill=fill))+
几何图形直方图(aes(y=密度),位置=位置标识(),α=0.5)+
刻度填充手动(值=c(“a”=“红色”、“b”=“绿色”、“c”=“蓝色”))+
主题(经典)+
主题(legend.title=element\u blank())
#>`stat_bin()`使用`bins=30`。使用`binwidth`选择更好的值。
由(v0.2.1)于2018年10月31日创建。您可以在调用geom\u直方图时使用position\u identity
执行此操作,而无需调用函数3次。这还将自动更新图例中的alpha,因为alpha仅设置了1次(而不是原始代码中每次调用3次。以下是使用一些模拟数据的示例:
库(tidyverse)
df%
聚集(key=“fill”,value=“x”)
df
#>#A tible:3000 x 2
#>填充x
#>
#>1A-0.951
#>2 a 0.321
#>3 a 0.551
#>4 a 0.166
#>5a-1.55
#>6 a 1.27
#>7a-0.224
#>8a-0.243
#>9a-1.48
#>10 a-0.888
#>#…还有2990行
ggplot(df,aes(x=x,fill=fill))+
几何图形直方图(aes(y=密度),位置=位置标识(),α=0.5)+
刻度填充手动(值=c(“a”=“红色”、“b”=“绿色”、“c”=“蓝色”))+
主题(经典)+
主题(legend.title=element\u blank())
#>`stat_bin()`使用`bins=30`。使用`binwidth`选择更好的值。
由(v0.2.1)于2018年10月31日创建您就快到了
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
pp=data.frame(fill=rep(c('a','b','c'),100),x=rnorm(300))
图书馆(GG2)
p你就快到了
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
pp=data.frame(fill=rep(c('a','b','c'),100),x=rnorm(300))
图书馆(GG2)
p您不必使用geom_hisgram()
多次。垃圾箱的位置由position
参数控制,position='identity'
将为您提供所需的内容,而默认值为position='stack'
scale\u fill\u manual
已正确映射颜色,即使使用单个geom\u直方图()
请尝试以下代码:
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'))
您不必使用geom_hisgram()
多次。垃圾箱的位置由position
参数控制,position='identity'
将为您提供所需的内容,而默认值为position='stack'
scale\u fill\u manual
已正确映射颜色,即使使用单个geom\u直方图()
请尝试以下代码:
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'))
漂亮!谢谢你给我看“立场”论点。漂亮!谢谢你给我看“立场”论点。