覆盖ggplot图中的默认颜色 [英] Overriding default colours in a ggplot diagram

查看:191
本文介绍了覆盖ggplot图中的默认颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R相当陌生,正在尝试改变生成的图的颜色。

  p = ggplot(plasma1,aes(x = (y = Control,color = Supp))
+ theme(panel.background = element_rect(fill ='white',color ='black'))
+ geom_point(size = 2,shape = 21)
+ geom_errorbar(aes(ymin = Control-SEMcontrol,ymax = Control + SEMcontrol),width = 1)

p + theme(panel.grid.major = element_blank(),panel .grid.minor = element_blank())
+ xlab(X)+ ylab(Y)+ geom_line(linetype =dashed)

我看过主题(),但我似乎只能改变网格线而不是趋势线。 (理想情况下,我想将红色和蓝色更改为红色和黑色)解决方案

注意:关于SO的约定是答案是为可再现的代码保留的,这些代码展示了一个解决方案,任何东西(比如建议)都属于评论,这就是为什么提问者提供他们的数据作为问题的一部分我们必须为你做点什么,而大多数人都是(有理由)不愿意这样做。



你要求的答案在下面,但在进入之前你应该请注意, ggplot 的默认配色方案是经过仔细选择的,所以只有在有充分理由时才应该更改它。问题是人类进化造成了某些颜色所以如果你有一条红色的曲线和一条黑色的曲线,那么红色的曲线就会留下一个更强烈的印象,这个事实被广泛用于某些领域(比如广告)从逻辑上操纵观察者,但它在科学数据可视化中没有地位。 ggplot 默认值,这些默认值基于HCL颜色系统(依次基于



正如其中一条评论所述,您也可以使用Penn State的Cynthia Brewer教授开发的


I'm fairly new to R and am trying to change the colours of my generated diagram.

 p = ggplot(plasma1, aes(x=Day, y=Control, colour=Supp))
 + theme(panel.background = element_rect(fill='white', colour='black'))
 + geom_point(size=2, shape=21) 
 + geom_errorbar(aes(ymin=Control-SEMcontrol, ymax=Control+SEMcontrol), width=1)

 p + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) 
 + xlab("X") + ylab("Y") + geom_line(linetype="dashed")

I've looked at themes() but I can only seem to change the grid lines and not the trend line. (Ideally I would like to change red & blue to red & black

解决方案

Note: The convention on SO is that "answers" are reserved for reproducible code that demonstrates a solution. Anything less (like a suggestion) belongs in a comment. This is why it is so essential that questioners provide their data as part of the question; otherwise we have to make some up for you, which most people are (justifiably) unwilling to do.

The answer you asked for is below, but before getting into that you should be aware that ggplot's default color scheme is carefully chosen, so you should only change it if there is a good reason. The problem is that human evolution has caused certain colors (like red) to get a perceptual boost relative to other colors. So if you have a red curve and a black curve, the red curve leaves a stronger "impression". This fact is used extensively in certain fields (like advertising) to psychologically manipulate the viewer, but it has no place in scientific data visualization. The ggplot defaults, which are based on the HCL color system (which in turn is based on the Munsell color system), try to achieve two objectives: to create a color palette where each color is maximally distinguishable from all the other colors, and to even out the relative perceptual impact. There is a fairly technical discussion of this topic here, and some nice examples here.

Bottom line: don't change the colors unless you have a really good reason to do so.

Having said all that, the simple answer to your question is to use scale_color_manual(...), as below:

# all this to set up the example - you have this already
set.seed(1)    # for reproducible example
x <- rep(c(1,2,4,8,11,14), each=5)
df1 <- data.frame(Day=x,Control=125*(1-exp(-x/5))+rnorm(30,sd=25),Supp="N")
df2 <- data.frame(Day=x,Control=90*(1-exp(-x/3))+rnorm(30,sd=25),Supp="C")
plasma1 <- aggregate(Control~Day+Supp,rbind(df2, df1), FUN=function(x)c(Control=mean(x),SEMcontrol=sd(x)/sqrt(length(x))))
plasma1 <- data.frame(plasma1[,1:2],plasma1[[3]])

# you start here
library(ggplot2)
ggp <- ggplot(plasma1, aes(x=Day, y=Control, color=Supp))+
  geom_point(size=3, shape=21)+
  geom_line(linetype="dashed")+
  geom_errorbar(aes(ymax=Control+SEMcontrol, ymin=Control-SEMcontrol), width=0.3)+
  theme_bw()+theme(panel.grid=element_blank())
ggp + scale_color_manual(values=c(C="red",N="black"))

Which produces this:

As mentioned in one of the comments, you could also use one of the Brewer Palettes developed by Prof. Cynthia Brewer at Penn State. These were originally intended for cartographic applications, but have become widely used generally in scientific visualization.

ggp + scale_color_brewer(palette="Set1")

这篇关于覆盖ggplot图中的默认颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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