ggplot: colour points by groups based on user defined colours

烂漫一生 提交于 2019-12-17 09:45:42

问题


I am trying to define the colours of groups of points plotted in ggplot. I adapted code from this post:

Color ggplot points based on defined color codes

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 

The data then look like this:

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

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


回答1:


You are somewhere between two different solutions.

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))

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).

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.




回答2:


It works if you use unique and as.character:

ggplot(data = df, aes(col, D, colour = zone))+ 
  geom_point() +
  scale_colour_manual(breaks = df$zone, 
                      values = unique(as.character(df$color.codes)))




回答3:


Sven beat me by a few secs, but slightly different:

df.unique <- unique(df[, c("zone", "color.names")])
p + scale_colour_manual(breaks = df.unique$zone, values = as.character(df.unique$color.names))


来源:https://stackoverflow.com/questions/21536835/ggplot-colour-points-by-groups-based-on-user-defined-colours

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!