首页 文章

为多个数据集创建ggplot2图例

提问于
浏览
0

我试图在带有图例的ggplot中自动显示灰色的背景数据 . 我的目标是在图例中包含灰色数据点,或者制作带有手动 Headers 的第二个图例 . 但是我在做这两个中的任何一个时都失败了 . 我的数据格式很长 .

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

enter image description here

我试图将data.frames与 rbind.data.frame 合并,这会产生一个很好的图例,但是后来我无法用灰色为背景数据着色并同时保持ggplot颜色 .

我也意识到这解决了这个问题:

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

但是我可以使用一个函数创建一个复杂的空图,之后我会添加 geom_points .

3 回答

  • 0

    假设您的绘图没有需要填充参数的其他geom,以下是一种解决方法,可以修复背景数据 geom_point 图层的颜色,而不会影响其他 geom_point 图层:

    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 为您提供一个看起来像默认圆点的形状,但除了color参数外还接受填充参数 . 然后,您可以在 scale_fill_manual() 中将xx的 geom_point 图层填充设置为灰色(这会创建填充图例),而将 color = "grey50" 设置在 aes() 之外(这不会添加到颜色图例中) .

    yy的 geom_point 图层的色标不受任何影响 .

    plot

    附:刚刚意识到我使用了“grey50”而不是“grey60”......但其他一切仍然适用 . :)

  • 0

    一种解决方案是创建颜色矢量并将其传递给 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")
    

    enter image description here

  • 0

    我提出了与Z.Lin完全相同的解决方案,但使用了 rbind.data.frame 的组合数据框 . 同样,它使用 scale_colour_manual 和向量 colors 指定颜色映射:

    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
    

    enter image description here

相关问题