0

I have a data (R dataframe) like this:

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
.......
.......

I want to make a scatter (or x, y) plot of diameter for each treatment group. I have found lattice library plot more helpful as of now and I have used:

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

to generate the plot:

enter image description here

However, I want to add the scatter plot for "Diameter from Dec" next to the "Diameter of Sep" for each treatments with different color. I am not able to find a workable example that I can use for my purpose so far.

Method with lattice, ggplot2 or base plot or any other would be really helpful.

Thanks,

everestial007
  • 6,665
  • 7
  • 32
  • 72

2 Answers2

1

Something like this?

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

You can adjust the amount of separation between the different coloured points by changing width inside position_dodge.


Sample data

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)
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • This is totally good. Btw, how did you create new "data or variable" i.e "Month", "Diameter" ? – everestial007 Sep 12 '18 at 23:23
  • @everestial007 In `gather(Month, Diameter, -Treatment)`, we reshape data across all columns except `Treatment` from wide to long, and in doing so store column names in a new column `Month` and its values in column `Diameter`. – Maurits Evers Sep 12 '18 at 23:26
  • could you add the code to reshape the data. I tried using other examples but I can't seem to work it out. – everestial007 Sep 13 '18 at 00:00
  • 1
    @everestial007 `gather(Month, Diameter, -Treatment)` already reshapes the data from wide to long. It's just the one line. – Maurits Evers Sep 13 '18 at 00:03
  • Alrite, I got it. I really need to look into how this reshape works. The problem that has happened is that I have several other columns of data for other traits, and this seems to plot everything by treatment. For now, I will reduce the data only by months. Thanks – everestial007 Sep 13 '18 at 00:12
1

Here's a tidyverse solution. It uses tidyr::gather to put the two diameter types into one column. You can then facet on the values in that column. I hide the colour legend, since the categories are apparent from the axis labels.

Assuming the data frame is named 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

neilfws
  • 32,751
  • 5
  • 50
  • 63