简体   繁体   English

如何更改 R 中堆积条的颜色?

[英]How do I change the color of stacked bar in R?

Could you please help me with it?你能帮我吗? I would like to change the clear for white, rain for blue, sleet for light blue, snow for light gray, fog for light orange and windy for light yellow.我想把晴天换成白色,下雨换成蓝色,雨夹雪换成浅蓝色,雪换成浅灰色,雾换成浅橙色,有风换成浅黄色。 I have created a stacked bar in the following.我在下面创建了一个堆积条。

在此处输入图像描述

barplot(tabl2, xlab="Injury level", xaxt = "n",
        legend=c("Clear", "Rain", "Sleet", "Snow", "Fog", "Windy"), col=1:6)
axis(1, at=seq(from=0.75, to=3.15, by=1.2), labels = c("Minor", "Serious", "Fatal"))

See here that看到这里

d.Titanic <- as.data.frame(Titanic)

> levels(d.Titanic$Class)
[1] "1st"  "2nd"  "3rd"  "Crew"

colors correspond to above levels.颜色对应于以上级别。 If you need other colors, see colors() for all "word" colors.如果您需要其他颜色,请参阅所有“单词”颜色的colors() Or use a hex notation.或者使用十六进制表示法。 The choices don't end there.选择不止于此。

barplot(Freq ~ Class + Survived, data = d.Titanic,
        subset = Age == "Adult" & Sex == "Male",
        main = "barplot(Freq ~ Class + Survived, *)", 
        col = c("red", "green", "blue", "white"),
        ylab = "# {passengers}", legend = TRUE)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM