通过不同时间段的处理制作散点图(或X,Y)

时间:2018-09-12 22:57:22

标签: r ggplot2 plot lattice

我有一个这样的数据(R数据框):

Treatment   Diameter(inches).Sep    Diameter(inches).Dec
Aux_Drop    NA  NA
Aux_Spray    3.7    2
DMSO    NA  NA
Water   4.2 2
Aux_Drop    2.6 3
Aux_Spray    3.7    3
DMSO    4   2
Water   5.2 1
Aux_Drop    5.4 2
Aux_Spray    3.4    2
DMSO    4.8 2
Water   4.2 2
Aux_Drop    4.7 2
Aux_Spray    2.7    2
DMSO    3.4 2
Water   4.9 2
.......
.......

我想为每个diameter组制作一个treatment的散点图(或x,y)。我发现lattice库图到目前为止更有帮助,并且我使用过:

require(lattice)
xyplot(`Diameter(inches).Sep` ~ Treatment , merged.Sep.Dec.Mar, pch= 20)

生成图:

enter image description here

但是,我想为每种颜色不同的处理在“ 9月直径”旁边添加“ 12月直径”散点图。我找不到一个可行的示例到目前为止,我可以使用。

使用latticeggplot2base plot或其他任何方法将非常有帮助。

谢谢

2 个答案:

答案 0 :(得分:1)

像这样吗?

library(tidyverse)
df %>%
    gather(Month, Diameter, -Treatment) %>%
    ggplot(aes(Treatment, Diameter)) +
    geom_point(aes(colour = Month), position = position_dodge(width = 0.9))

enter image description here

您可以通过更改width内的position_dodge来调整不同颜色点之间的间隔量。


样本数据

df <- read.table(text =
    "Treatment   Diameter(inches).Sep    Diameter(inches).Dec
Aux_Drop    NA  NA
Aux_Spray    3.7    2
DMSO    NA  NA
Water   4.2 2
Aux_Drop    2.6 3
Aux_Spray    3.7    3
DMSO    4   2
Water   5.2 1
Aux_Drop    5.4 2
Aux_Spray    3.4    2
DMSO    4.8 2
Water   4.2 2
Aux_Drop    4.7 2
Aux_Spray    2.7    2
DMSO    3.4 2
Water   4.9 2", header = T)

答案 1 :(得分:1)

这是一个tidyverse解决方案。它使用tidyr::gather将两种直径类型放入一列。然后,您可以对该列中的值进行分析。我隐藏了颜色图例,因为类别在轴标签中显而易见。

假定数据帧的名称为mydata

library(tidyverse)
mydata %>% 
  gather(Result, Value, -Treatment) %>% 
    ggplot(aes(Result, Value)) + 
    geom_jitter(aes(color = Result), 
                width = 0.1) + 
    facet_wrap(~Treatment) +
    guides(color = FALSE)

enter image description here