I have a dataset that has diameter values for 4 treatment groups for several different months. I am plotting Diameter ~ Treatment
for each month, as well as the Diameter changes between months ~ Treatment
.
Dataset looks like this:
# the data that contains diameter for each month and diameter differences between months > head(gatheredDiameterAndTreatmentData) Treatment Month Diameter 1 Aux_Drop Diameter_mm.Sep01 55.88 2 Aux_Spray Diameter_mm.Sep01 63.50 3 DMSO Diameter_mm.Sep01 66.04 4 Water Diameter_mm.Sep01 43.18 5 Aux_Drop Diameter_mm.Sep01 38.10 6 Aux_Spray Diameter_mm.Sep01 76.20 # data that contains mean diameter and mean diameter changes for each month > head(subMeansDiameter) Treatment Month Diameter SEdiam 1 Aux_Drop Diameter_mm.Dec 83.63857 29.62901 2 Aux_Drop Diameter_mm.Feb01 101.20923 24.84024 3 Aux_Drop Diameter_mm.Feb02 110.00154 22.51364 4 Aux_Drop Diameter_mm.Jan 93.00308 25.13485 5 Aux_Drop Diameter_mm.Mar 116.84000 22.19171 6 Aux_Drop Diameter_mm.Nov01 74.50667 17.40454
Here is my code:
# assign the factors name to pick
factorsOnXaxis.DiameterByMonth = c(
"Diameter_mm.Sep01", "DiameterDiff.Sep01ToDec", "Diameter_mm.Dec", "DiameterDiff.DecToMar", "Diameter_mm.Mar")
# assign name to above factors
factorsOnXaxisName = c('Sep','Dec-Sep','Dec', 'Mar-Dec', 'Mar')
# start plotting
gatheredDiameterAndTreatmentData %>%
subset(Diameter != "NA") %>%
ggplot(aes(x = factor(Month), y = Diameter)) +
geom_point(aes(colour = Treatment), na.rm = TRUE,
position = position_dodge(width = 0.2)) +
geom_point(data = subMeansDiameter, size = 4, aes(colour = Treatment),
na.rm = TRUE, position = position_dodge(width = 0.2)) +
theme_bw() + # remove background
# add custom color to the "Treatment" levels
scale_colour_manual(
values = c("Aux_Drop" = "Purple", "Aux_Spray" = "Red",
"DMSO" = "Orange", "Water" = "Green")) +
# rearrange the x-axis
scale_x_discrete(limits = factorsOnXaxis.DiameterByMonth, labels = factorsOnXaxisName) +
# to connect the "subMeans - Diameter" values across time points
geom_line(data = subMeansDiameter, aes(
x = Month, y = Diameter, group = Treatment, colour = Treatment),
position = position_dodge(width = 0.2))
Which gives me a plot like this:
Instead of geom_line
connecting line for each time points I want the line to be joined between specified x-axis factors, i.e
- between Sep, Dec, March
- between Dec-Sep to Mar-Dec
I tried to manipulate the code line that uses geom_line
as:
geom_line(data = subMeansDiameter, aes( x = c("DiameterDiff.Sep01ToDec", "DiameterDiff.DecToMar"), y = Diameter, group = Treatment, colour = Treatment), position = position_dodge(width = 0.2))
to connect the line between Dec-Sep
to Mar-Dec
.
But, this is not working. How can I change my code?
Here is the data file I stores as *.tsv.
gatheredDiameterAndTreatmentData = http://s000.tinyupload.com/index.php?file_id=38251290073324236098
subMeans = http://s000.tinyupload.com/index.php?file_id=93947954496987393129
Here you need to define groups explicitly as color is not enough.
Your example is not reproducible but here's something that will give you the idea, here's a plot with no explicit group:
And now here's one with a group aesthetic, I have split the data using
Sepal.Length
's values but you'll most likely use anifelse
deending on the month :