Create ggplot2 legend for multiple datasets

寵の児 提交于 2021-02-16 14:55:09

问题


I am trying to display background data in grey in a ggplot with legend automatically. My aim is to either include the grey datapoints in the legend, or to make a second legend with a manual title. However I fail at doing any of the two. My data is in long format.

require(ggplot2)

xx<-data.frame(observation="all cats",x=1:2,y=1:2)
yy<-data.frame(observation=c("red cats","blue cats"),x=3:4,y=3:4)

g<-ggplot() + 
  geom_point(aes(x,y, colour=factor(observation)), colour="grey60", size=5, data=xx) +
  geom_point(aes(x,y, colour=factor(observation)), size=5, data=yy) + 
  scale_color_discrete(name = "ltitle") 

g

I tried to merge the data.frames with rbind.data.frame, which produces a nice legend, but then I am not able to colour the background data in grey and keep ggplot colours at the same time.

I also realized that this solves the problem:

g<-ggplot(aes(x,y, colour=factor(observation)), colour="grey60", data=xx) + 
  geom_point(size=5) +
  geom_point(aes(x,y, colour=factor(observation)), size=5, data=yy) + 
  scale_color_discrete(name = "ltitle") 
g

however I can't do this, because I'm using a function which creates a complicated empty plot before, in which I then add the geom_points.


回答1:


Assuming your plot doesn't have other geoms that require a fill parameter, the following is a workaround that fixes the colour of your background data geom_point layer without affecting the other geom_point layers:

g <- ggplot() + 
  geom_point(aes(x, y, 
                 fill = "label"),                              # key change 1
             shape = 21,                                       # key change 2
             color = "grey50", size = 5, 
             data = xx) +
  geom_point(aes(x, y, colour = factor(observation)), size = 5, data = yy) + 
  scale_color_discrete(name = "ltitle") +
  scale_fill_manual(name = "", values = c("label" = "grey50")) # key change 3
g

shape = 21 gives you a shape that looks like the default round dot, but accepts a fill parameter in addition to the colour parameter. You can then set xx's geom_point layer's fill to grey in scale_fill_manual() (this creates a fill legend), while leaving color = "grey50" outside aes() (this does not add to the colour legend).

The colour scale for yy's geom_point layer is not affected by any of this.

p.s. Just realized I used "grey50" instead of "grey60"... But everything else still applies. :)




回答2:


One solution is to create color vector and pass it to scale_color_manual.

xx <- data.frame(observation = "all cats",x = 1:2,y = 1:2)
yy <- data.frame(observation = c("red cats", "blue cats"),x = 3:4,y = 3:4)
# rbind both datasets
# OP tried to use rbind.data.frame here
plotData <- rbind(xx, yy)

# Create color vector
library(RColorBrewer)
# Extract 3 colors from brewer Set1 palette
colorData <- brewer.pal(length(unique(plotData$observation)), "Set1")
# Replace first color first wanted grey
colorData[1] <- "grey60"

# Plot data
library(ggplot2)
ggplot(plotData, aes(x, y, colour = observation)) + 
    geom_point(size = 5)+
    scale_color_manual(values = colorData, name = "ltitle")

                                     




回答3:


I came up with pretty much same solution as Z.Lin but using the combined dataframe from rbind.data.frame. Similarly, it uses scale_colour_manual with a vector colors specifying the color mapping:

require(ggplot2)

xx<-data.frame(observation="all cats",x=1:2,y=1:2)
yy<-data.frame(observation=c("red cats","blue cats"),x=3:4,y=3:4)

zz <- rbind.data.frame(xx,yy)

colors <- c(
  "all cats" = "grey60",
  "red cats" = "red",
  "blue cats" = "blue"
)

g<-ggplot() + 
  geom_point(aes(x,y, colour=factor(observation)), size=5, data=zz) +
  scale_color_manual(values= colors, name = "ltitle") 
g



来源:https://stackoverflow.com/questions/47865121/create-ggplot2-legend-for-multiple-datasets

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