ggplot2 中具有三重图例的散点图

Scatter plot in ggplot2 with triple legend

我想制作一个带有形状、颜色和填充的散点图作为我的数据的编码。一切看起来形状和颜色都很好,但是填充选项似乎没有响应。我已经检查过与该主题类似的其他帖子,但仍然无法弄清楚如何处理这个问题。在我的案例中,我想要的是根据两种不同的过滤器尺寸(0.22=空心,0.45=实心)填充的过滤器列,以及根据我的时间着色的所有内容。 请找到我使用的数据集和代码,

 Samples    Filter  Type    Time    Coord1  Coord2
 45P116 0.45    Oil Incubation  16  -0.177125327477 0.0329641384336
 22P264 0.22    Oil Incubation  64  -0.036421737819 0.41278336259
 45P113 0.45    Oil Incubation  13  -0.183501817053 -0.0333774595889
 45BL30 0.45    Control 30  -0.0169216562011    -0.160467270544
 22P231 0.22    Oil Incubation  30  -0.0742549515959    0.134945242558
 22P116 0.22    Oil Incubation  16  -0.168257444167 0.0425318235085
 22P164 0.22    Oil Incubation  64  -0.0995650111648    0.321208450137
 22BL64 0.22    Control 64  0.344181403614  0.011496893707
 45P29  0.45    Oil Incubation  9   -0.139337303592 -0.154032242023
 22P216 0.22    Oil Incubation  16  -0.182624942475 0.036656544869
 22P30  0.22    Oil Incubation  0   0.232890938751  -0.0365286035332
 45P23  0.45    Oil Incubation  3   0.335212340096  0.00229714294784
 22P364 0.22    Oil Incubation  64  -0.0265850037098    0.267838399813
 45P26  0.45    Oil Incubation  6   -0.0663530024752    -0.123614294282
 45P316 0.45    Oil Incubation  16  -0.180387066353 0.0244231332556
 22P16  0.22    Oil Incubation  6   -0.0938690796601    -0.123292570064
 22P13  0.22    Oil Incubation  3   -0.000929711149241  -0.0840936522112
 22P36  0.22    Oil Incubation  6   -0.0846096566973    -0.126315336717
 22P39  0.22    Oil Incubation  9   -0.145400678025 -0.171058987178
 45P164 0.45    Oil Incubation  64  -0.160359423757 0.439794230436
 22P19  0.22    Oil Incubation  9   -0.138575642598 -0.151960075733
 45P313 0.45    Oil Incubation  13  -0.176079013758 0.050997652543
 45BL64 0.45    Control 64  0.0288347544654 -0.1458771364
 45P330 0.45    Oil Incubation  30  -0.11228777477  0.204236728786
 45P36  0.45    Oil Incubation  6   -0.0761185616066    -0.121261486374
 45P130 0.45    Oil Incubation  30  -0.094228858564 0.100652965768
 22BL0  0.22    Control 0   0.239232948872  -0.036785772233
 22P213 0.22    Oil Incubation  13  -0.18113904214  -0.0287178094404
 22BL6  0.22    Control 6   -0.0600555194535    -0.194650371605
 45P33  0.45    Oil Incubation  3   0.340399779328  0.0171852693215
 45P30  0.45    Oil Incubation  0   0.431938929593  0.0196841881272
 45BL6  0.45    Control 6   0.357256101911  -0.0188140900434
 45BL9  0.45    Control 9   0.303457604982  -0.0443188527517
 22P316 0.22    Oil Incubation  16  -0.164460159926 -0.0129812590712
 22P313 0.22    Oil Incubation  13  -0.188614376178 0.00058901489101
 45P39  0.45    Oil Incubation  9   -0.149217759045 -0.167311625113
 22P131 0.22    Oil Incubation  30  -0.0651994575885    0.0656871734895
 22BL31 0.22    Control 30  0.335856403002  0.0724160545763
 45P19  0.45    Oil Incubation  9   -0.142421940226 -0.160880360066
 22P331 0.22    Oil Incubation  30  -0.0500301022508    0.0416246477279
 45BL0  0.45    Control 0   0.381611397751  0.0200030589204
 22P113 0.22    Oil Incubation  13  -0.192581008392 -0.00938532222554
 45P16  0.45    Oil Incubation  6   -0.085999339488 -0.121939087208
 45BL3  0.45    Control 3   0.332023830601  0.036732406277
 45P13  0.45    Oil Incubation  3   0.315664193595  0.0248256483492
 45P216 0.45    Oil Incubation  16  -0.172909653606 0.030607355003
 45P213 0.45    Oil Incubation  13  -0.18326004657  0.00570693606563
 22P29  0.22    Oil Incubation  9   -0.150029591452 -0.16569360913
 22P26  0.22    Oil Incubation  6   -0.110220935459 -0.134301793132
 45P364 0.45    Oil Incubation  64  -0.100468231806 0.248528601323
 45BL16 0.45    Control 16  0.341331109786  0.00915399605284
 45BL13 0.45    Control 13  0.155782679403  -0.17960863481
 45P230 0.45    Oil Incubation  30  -0.0452735875021    0.0316966419992

和代码:

    PCoA <- read.delim(file.choose(), header=T)
attach(PCoA)
library(ggplot2)
PCoA$Time <- as.character(PCoA$Time)
PCoA$Filter <- as.character(PCoA$Filter)
cols <- c("0" = "red","3" = "blue","6" = "brown", "9" = "hotpink", "13" = "orange","16" = "olivedrab","30" = "azure3", "64" = "purple")
ggplot(PCoA, aes(x=Coord1, y=Coord2, shape=Type, colour=Time)) + 
  scale_colour_manual(values = cols, breaks=c("0","3","6","9","13","16","30","64")) + 
  geom_point(size=7) + xlab("PC1 (39.7%)") + ylab("PC2 (20.2%)") + 
  geom_hline(aes(yintercept=0)) + geom_vline(aes(xintercept=0)) + 
  theme(legend.text = element_text(size = 16), 
        legend.title = element_text(size=18), 
        axis.title.x = element_text(size=18, vjust=-0.7), 
        axis.title.y = element_text(size=18, vjust=2)) + 
  theme(axis.text = element_text(colour = "black", size="16")) + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.background = element_blank(), 
        axis.line = element_line(colour = "white"))

你需要用对shapes (21:25) and to specify your fill. This post有一些很好的提示。虽然在这一点上,您的数据变得越来越难以用不同的轮廓和填充颜色来说明。 (也许可以将 Time 编码为填充,并为一个滤镜使用较浅的色调,为另一个滤镜使用较暗的色调 - 或者 - 使 Filter 键入轮廓颜色).

这是你编辑的图表:

  • 数据格式编码 as.character() 在 ggplot 中而不是在 原始数据框。
  • scale_parameter_manual()
  • 中指定的图例名称
  • 填充定义在aes()
  • shapefill 添加为最后一行
  • 填充 使用 override.aes
  • 修复的图例

ggplot(PCoA, aes(x=Coord1, y=Coord2, shape=as.character(Type), colour=as.character(Time), fill=as.character(Filter) )) + #Edit
  scale_colour_manual(name="Time", values = cols, breaks=c("0","3","6","9","13","16","30","64")) + #Edit
  geom_point(size=7) + xlab("PC1 (39.7%)") + ylab("PC2 (20.2%)") + 
  geom_hline(aes(yintercept=0)) + geom_vline(aes(xintercept=0)) + 
  theme(legend.text = element_text(size = 16), 
        legend.title = element_text(size=18), 
        axis.title.x = element_text(size=18, vjust=-0.7), 
        axis.title.y = element_text(size=18, vjust=2)) + 
  theme(axis.text = element_text(colour = "black", size="16")) + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.background = element_blank(), 
        axis.line = element_line(colour = "white")) +
  scale_shape_manual(name="Type", values=c(21,24)) + #Edit
  scale_fill_manual(name="Filter", values=c("black", "white")) + #Edit
  guides(fill = guide_legend(override.aes = list(   #Edit
                fill = c("black", "white"),         #Edit
                shape=22,                           #Edit
                colour="black") ))                  #Edit

你的原图+编辑图:

编辑

OP询问如何对填充进行微调,使变量1定义的填充由变量2决定,即如果变量2是A,则点被颜色填充,但如果变量2是B,则所有的点都是白色的。由于第三个变量决定了形状,这增加了额外的复杂性。

有几个可能的解决方案:

  1. 通过 aes(shape=interaction(Variable2, Variable3)) 组合变量并指定仅允许颜色 (15:20) 和颜色+填充 (21:25) 的形状。
  2. 比 #1 更精简但控制更少的是指定具有空心中心 (0:6) 而不是填充中心 (21:25) 的形状。那么fill就不用定义了。
  3. 加载库(缩放)并设置填充 alpha 属性(最后一个示例 here),即 scale_fill_manual(values = alpha(c("blue", "red"), .3)) (此解决方案对于此示例可能不可行。)
  4. 分别绘制点和美学:即geom_point(data= subset, aes())

方法 #1:

ggplot(PCoA, aes(x=Coord1, y=Coord2, 
                 shape=interaction(as.character(Type), Filter),
                 colour=as.character(Time), 
                 fill=as.character(Time) )) + #Edit
  scale_colour_manual(name="Time", values = cols, 
      breaks=c("0","3","6","9","13","16","30","64")) + #Edit
  geom_point(size=7) + xlab("PC1 (39.7%)") + ylab("PC2 (20.2%)") + 
  geom_hline(aes(yintercept=0)) + geom_vline(aes(xintercept=0)) + 
  theme(legend.text = element_text(size = 16), 
        legend.title = element_text(size=18), 
        axis.title.x = element_text(size=18, vjust=-0.7), 
        axis.title.y = element_text(size=18, vjust=2),
        legend.key = element_rect(fill=NA)) +                       #Edit
  theme(axis.text = element_text(colour = "black", size="16")) + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.background = element_blank(), 
        axis.line = element_line(colour = "white")) +
  scale_shape_manual(name="Type/Filter", values=c(21, 16, 24, 17),
                     labels=c("Control, 0.22", "Incubation, 0.22",
                              "Control, 0.45", "Incubation, 0.45") ) + #Edit
  scale_fill_manual(name="Filter", values=rep("white", length(cols)), guide=FALSE )  #Edit