R 图例更改为几何图形栏指定的颜色
我有以下曲线图,其中包括来自两个不同数据帧的数据:R 图例更改为几何图形栏指定的颜色,r,ggplot2,legend,geom-bar,R,Ggplot2,Legend,Geom Bar,我有以下曲线图,其中包括来自两个不同数据帧的数据: ggplot(one100, aes(x=hour, y=countPerHour)) + ylab("Times reached") + xlab("Hour of the day") + geom_bar( stat="identity", color="turquoise3", fill="turquoise2", alpha=0.3) + geom_bar( stat="identity",
ggplot(one100, aes(x=hour, y=countPerHour)) +
ylab("Times reached") +
xlab("Hour of the day") +
geom_bar( stat="identity",
color="turquoise3", fill="turquoise2", alpha=0.3) +
geom_bar( stat="identity",
aes(x=noventa99$hour, y=noventa99$countPerHour),
color="green3", fill="green3", alpha=0.3) +
theme_bw()
我用下面的代码添加了一个图例,但在添加的那一刻,我丢失了填充geom_bar()条的原始颜色
这是我得到的结果:
one100数据框如下所示,其中“小时”是一个因素
"hour" "countPerHour"
"9" 30
"11" 24
"8" 21
"10" 18
"13" 17
"12" 15
"7" 15
"14" 14
"5" 12
"17" 7
"15" 7
"16" 6
"4" 6
"6" 5
"21" 4
"20" 3
"19" 2
"3" 2
"2" 2
"23" 1
"22" 1
"18" 1
"0" 1
"1" 0
noventa99包含以下数据:
"hour" "countPerHour"
"1" 1
"3" 2
"4" 1
"5" 3
"6" 1
"7" 6
"8" 5
"9" 7
"10" 6
"11" 3
"12" 4
"13" 4
"14" 5
"15" 4
"16" 3
"17" 2
"18" 1
"19" 1
"21" 1
"22" 1
"23" 1
"20" 0
"2" 0
"0" 0
如何保持我指定的原始颜色填充条形图?问题很简单,因为您将值传递给控制条形图轮廓的
color
,而不是fill
。也就是说,生成图例的最佳方法是组合数据,以便将变量映射到这些美学,而不是硬编码的值:
库(ggplot2)
一小时100美元:国际9 11 8 10 13 12 7 14 5 17。。。
#>$countPerHour:int 30 24 21 18 17 15 15 14 12 7。。。
#使用填充和颜色设置aes一次,不使用`$`子集
ggplot(组合_数据,aes(小时,每小时计数,填充=临界值,颜色=临界值))+
geom_col(alpha=0.3)+#geom_col是'geom_bar(stat=“identity)”的缩写
#设置填充和颜色美学的比例
比例填充手册(“临界占空比”,数值=c(“100%”=“绿绿色2”,“80-90%”=“绿色3”))+
比例-颜色-手册(“临界职业百分比”,数值=c(“100%”表示“绿松石色2”,“80-90%”表示“绿色3”)+
实验室(x=“一天中的小时”,y=“达到的次数”)+#一次设置多个标签
hrbrthemes::theme_ipsum_rc()#漂亮的主题
非常感谢您的详细解释和代码!
"hour" "countPerHour"
"1" 1
"3" 2
"4" 1
"5" 3
"6" 1
"7" 6
"8" 5
"9" 7
"10" 6
"11" 3
"12" 4
"13" 4
"14" 5
"15" 4
"16" 3
"17" 2
"18" 1
"19" 1
"21" 1
"22" 1
"23" 1
"20" 0
"2" 0
"0" 0