ggplot2中的散点图与三重传奇

时间:2015-06-29 09:33:02

标签: r ggplot2 scatter-plot

我想制作一个带有形状,颜色和填充的散点图作为我的数据的编码。一切看起来都很好,有形状和颜色,但填充选项似乎没有响应。我已经检查了这个主题的其他类似帖子,但仍然无法弄清楚如何处理这个问题。在我的情况下我想要的是根据两种不同的过滤器尺寸填充过滤器柱(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"))

1 个答案:

答案 0 :(得分:3)

你需要使用正确的shapes(21:25)并指定你的填充。这个post有一些很好的提示。虽然此时,您的数据变得越来越难以用不同的轮廓和填充颜色来说明。 (也许代替时间作为填充,并使用较浅的色调用于一个滤镜,而使用较暗的另一个滤镜 - 或 - 使滤镜键入您的轮廓颜色)。

以下是您编辑的图表:

  • ggplot中编码as.character()的数据格式,而不是 原始数据框。
  • scale_parameter_manual()
  • 中指定的图例名称 在aes() 中定义的
  • fill
  • 形状 fill 添加为最后一行
  • 使用override.aes 修复了
  • fill 图例
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

您的原始情节+编辑的情节: original and edited plots

修改

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())
  5. 方法#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
    

    plot with colored fill on treatments and blank fill on controls