改变ggplot因素颜色? [英] changing ggplot factor colors?

查看:135
本文介绍了改变ggplot因素颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到这里盒子和胡须的阴谋的电话:

  p + geom_boxplot(aes(fill = factor(cyl)))

产生明亮的红色/绿色/蓝色,用于boxlot填充,而:

  p + geom_boxplot (aes(fill = factor(vs)))

生成一个独特的淡绿色/红色的颜色。在我的数据中,我得到了第二组颜色,但想要第一组颜色(例如在

  p + geom_boxplot(aes (fill = factor(cyl)))

什么控制ggplot使用哪一组颜色?改变它?



谢谢

解决方案

您可以检查它是如何从 此处生成的。



您可以使用 scale_fill_manual 这些颜色:

  p + scale_fill_manual(values = c(#F8766D,#00BA38))

在这里,我使用 cyl ggplot_build(p)$ data >获得颜色。



或者,您可以使用另一个调色板,如下所示:

  p + scale_fill_brewer(palette =Set1)

为了找到调色板中的颜色,您可以这样做:

  require(RColorBrewer)
brewer.pal(9,Set1)

如果您有兴趣,请查看包裹以了解调色板和其他选项。



编辑: @ user248237dfsf,正如我已经在顶部的链接中指出的那样,@Andrie中的这个函数显示了生成的颜色:

 ((diff(h)%% 360)<(code> ggplotColours<  - 函数(n = 6,h = c(0,360)+15) 1)h [2] < -  h [2] -360 / n 
hcl(h =(seq(h [1],h [2],length = n)),c = 100,1 = 65)
}

> ggplotColours(2)
#[1]#F8766D#00BFC4

> ggplotColours(3)
#[1]#F8766D#00BA38#619CFF

如果您查看生成的两种颜色,第一种颜色是相同的,但第二种颜色不相同,当n = 2和n = 3时。这是因为它会产生均匀间距色调的颜色。如果你想用 cyl 作为 vs 的颜色,那么你必须设置 scale_fill_manual 并使用这些函数生成的n = 3生成的颜色。



验证这确实是你正在做的事情: (mtcars,aes(factor(cyl),mpg))+
geom_boxplot(aes(fill =因子(cyl)))

p2 < - ggplot(mtcars,aes(factor(cyl),mpg))+
geom_boxplot(aes(fill = factor(vs)))

现在,如果您这样做:

#[1]#F8766D#00BA38#619CFF



$#$#$#$#$#$ c>

您会发现这些是使用 ggplotColours 生成的颜色,差异也很明显。我希望这有助于。


I notice that here Box and whiskers plot the call:

p + geom_boxplot(aes(fill = factor(cyl)))

generates bright red/green/blue colors for boxplots fill, while:

p + geom_boxplot(aes(fill = factor(vs)))

Generates a distinct pale green/red of colors. In my data, I get the second set of colors, but would like the first set (like in

p + geom_boxplot(aes(fill = factor(cyl)))

what controls which set of colors ggplot uses and how can you change it?

Thanks

解决方案

The default colours are evenly spaced hues around the colour wheel. You can check how this is generated from here.

You can use scale_fill_manual with those colours:

p + scale_fill_manual(values=c("#F8766D", "#00BA38"))

Here, I used ggplot_build(p)$data from cyl to get the colors.

Alternatively, you can use another palette as well like so:

p + scale_fill_brewer(palette="Set1")

And to find the colours in the palette, you can do:

require(RColorBrewer)
brewer.pal(9, "Set1")

Check the package for knowing the palettes and other options, if you're interested.

Edit: @user248237dfsf, as I already pointed out in the link at the top, this function from @Andrie shows the colors generated:

ggplotColours <- function(n=6, h=c(0, 360) +15){
  if ((diff(h)%%360) < 1) h[2] <- h[2] - 360/n
    hcl(h = (seq(h[1], h[2], length = n)), c = 100, l = 65)
}

> ggplotColours(2)
# [1] "#F8766D" "#00BFC4"

> ggplotColours(3)
# [1] "#F8766D" "#00BA38" "#619CFF"

If you look at the two colours generated, the first one is the same, but the second colour is not the same, when n=2 and n=3. This is because it generates colours of evenly spaced hues. If you want to use the colors for cyl for vs then you'll have to set scale_fill_manual and use these colours generated with n=3 from this function.

To verify that this is indeed what's happening you could do:

p1 <- ggplot(mtcars, aes(factor(cyl), mpg)) + 
           geom_boxplot(aes(fill = factor(cyl)))

p2 <- ggplot(mtcars, aes(factor(cyl), mpg)) + 
           geom_boxplot(aes(fill = factor(vs)))

Now, if you do:

ggplot_build(p1)$data[[1]]$fill
# [1] "#F8766D" "#00BA38" "#619CFF"

ggplot_build(p2)$data[[1]]$fill
# [1] "#F8766D" "#00BFC4" "#F8766D" "#00BFC4" "#F8766D"

You see that these are the colours that are generated using ggplotColours and the reason for the difference is also obvious. I hope this helps.

这篇关于改变ggplot因素颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆