Search code examples
rplotgroupinglattice

Two grouping variables in a dotplot


I am trying to create a simple dotplot (lattice package) in R with the following data frame:

df<-data.frame(Sediment=c("Algae", "Algae", "Algae", "Bare", "Bare", "Bare", "Dredged", "Dredged", "Dredged"), Rep=c(1,2,3,1,2,3,1,2,3), LeafElongation=c(0, 20.6, 0, 29.1, 41.4, 45.9, 54.1, 22.3, 26.7))

I would like to use marker colors to group the data by Sediment and marker shapes to group data by Rep. This is my code so far:

dotplot(LeafElongation~Sediment, data=df, groups=Sediment, method="jitter", col = 
      c("darkolivegreen", "darkgoldenrod3", "sienna"), main= "Leaf Elongation: Ramet Plots", ylab="Average total plant leaf elongation (cm)", cex.main=1.7, cex.lab=1.2, cex.axis=1.7)

I have successfully grouped my data by sediment and this is generally how I would like the plot to appear, but I would like to have a different shape for each Rep (circles for 1, diamonds for 2, triangles for 3). I would also like the shapes to be filled rather than open. Additionally, I believe the jitter method is supposed to separate overlapping points so that both can be seen, but it does not appear to be working in this case.


Solution

  • The pch argument is what you are looking for. Just add it to your code and feed it directly with df$Rep. You can remove the argument groups, that now unnecessary.

    dotplot(LeafElongation~Sediment, data=df, 
        method="jitter", 
        col = c("darkolivegreen", "darkgoldenrod3", "sienna"), 
        main= "Leaf Elongation: Ramet Plots", 
        ylab="Average total plant leaf elongation (cm)", 
        cex.main=1.7, cex.lab=1.2, cex.axis=1.7,
        pch = df$Rep)