ggplot:根据用户定义的颜色按组划分颜色点 [英] ggplot: colour points by groups based on user defined colours
问题描述
我正在尝试定义在 ggplot 中绘制的点组的颜色.我改编了这篇文章的代码:
I am trying to define the colours of groups of points plotted in ggplot. I adapted code from this post:
但是一旦我有多个由同一个分组变量定义的行(而不是每行一个单独的颜色),代码就会失败,我不知道为什么.下面是一个可重现的示例:
but as soon as I have more than one row defined by the same grouping variable (rather than a separate colour for each row), the code fails, and I can't figure out why. Below is a reproducible example:
#create some data
zone <- c("E","E","C","C","C","E","E") #grouping variable
col <- c(50,100,150,200,250,300,350) #x variable
D <- c(.4,.45,.20,.22,.30,.31,.35) #y variable
df1 <- data.frame(zone, D, col); df1
#create a colour scheme based on grouping variable 'zone'
zone <-c("E","C")
color.codes<-as.character(c("#3399FF", "#FF0000"))
color.names<-c("blue", "red")
df2=data.frame(zone, color.codes, color.names); df2
# merge color specifications with data
df <-merge(df1,df2, by=("zone"), all.x=TRUE, all.y=TRUE); df
数据如下所示:
zone D col color.codes color.names
C 0.20 150 #FF0000 red
C 0.22 200 #FF0000 red
C 0.30 250 #FF0000 red
E 0.40 50 #3399FF blue
E 0.45 100 #3399FF blue
E 0.31 300 #3399FF blue
E 0.35 350 #3399FF blue
目标是生成一个图,其中区域C"中的点为红色,E"中的点为蓝色,但使用示例中的代码引用的所有内容都以红色绘制:
The goal is to produce a plot where points in zone 'C' are red and those in 'E' are blue, but using the code from the example cited everything is plotted in red:
p <- ggplot(data=df, aes(col, D, colour = zone))+
geom_point()
p + scale_colour_manual(breaks = df$zone, values = df$color.codes)
谁能看到致命的缺陷,为什么这段代码不能以这种方式跨组工作?
预先非常感谢
Can anyone see the fatal flaw, why this code won't work across groups in this way?
Thanks so much in advance
推荐答案
您介于两种不同的解决方案之间.
You are somewhere between two different solutions.
一种方法是不将颜色放入 df
数据框中,并在比例调用中指定 zone
和所需颜色之间的映射:
One approach is to not put the colors into the df
data frame and specify the mapping between zone
and desired color in the scale call:
ggplot(data=df, aes(col, D, colour = zone))+
geom_point() +
scale_colour_manual(values=setNames(color.codes, zone))
请注意,这不使用 df
中的 color.codes
或 color.names
,也不使用 df2
code> 直接(尽管它确实使用了用于制作 df2
的列;如果你有类似 df2
的东西而不是单独的列,你可以使用 setNames(df2$color.codes, df2$zone)
代替).
Note that this does not use color.codes
or color.names
from df
, nor does it use df2
directly (though it does use the columns that are used to make df2
; if you have something like df2
and not the columns separately, you can use setNames(df2$color.codes, df2$zone)
instead).
另一种方法将颜色直接映射到颜色代码并使用 scale_color_identity
,但随后必须通过一些额外的标签来获得正确的图例.
The other approach maps color directly to the color codes and uses scale_color_identity
, but then has to go through some additional labeling to get the legend right.
ggplot(data=df, aes(col, D, colour = color.codes)) +
geom_point() +
scale_colour_identity("zone", breaks=color.codes, labels=zone, guide="legend")
在我看来,第一个是更好的解决方案.
The first is, in my opinion, the better solution.
这篇关于ggplot:根据用户定义的颜色按组划分颜色点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!