2

I am trying to compare nutrient element values (N, P, K, etc.) from several trials, using the stripplot() plotting function. I want to see the individual values side by side for each nutrient, not in separate plots for each trial, so that readily-available comparisons can be made within each nutrient (for example, comparing nitrogen (N) values from "Trial A" to nitrogen values from "Trial B"). This code makes side by side plots, which are not easily-comparable between Trials:

foo <- data.frame(conc=rnorm(90),element=rep(c("N","P","K"),each=30),
                  trial=rep(c("Trial_A","Trial_B"),times=45))
stripplot(conc~element|trial,data=foo)

Is there a way to intersperse the nutrient values so that there is just one big stripplot, and then eventually I would just use plotting symbols (pch=) to delineate trials?

@ BlankUsername: I greatly appreciate the code suggestion. However, I am actually trying to plot the values side by side in separate "columns" of points... So in this case, for N, P, and K from 2 trials, along the x-axis I would have, in this order: N from Trial 1, N from Trial 2, P from trial 1, P from Trial 2, K from Trial 1, K from Trial 2... So, for 3 nutrients and 2 trials I would have 2x3 = 6 "columns" of values, not 3 columns where the values are combined for both trials and delineated with symbols. I hope this description makes sense. I would include a picture of the plot that I am trying to make if I could make it, but then I would already have the answer to this question I guess :) The reason that I'd like to do it this way is because I have many nutrients with many values for each trial, and yes, it gets a bit unwieldy when they are all plotted on top of one another and I am trying to distinguish the symbols!

Ezra Citron
  • 101
  • 2
  • 6

1 Answers1

0

From your description I guess that you want all the data into one stripplot where you use the pch setting to indicate the various trials. Below the code gives an example of how this could be achieved. Basically you just need to adjust the tilde formulation.

I have used some pretty basic settings, but you can customize at heart of course. I would advise against plotting all of the data into one plot as having 6 different types of points is not easy on the eyes, or for making comparisons between the data. But that choice is yours to make obviously.

require(lattice)
foo <- data.frame(conc=rnorm(90),element=rep(c("N","P","K"),each=30),
              trial=rep(c("Trial_A","Trial_B"),times=45))

stripplot(conc~element,data=foo,pch=as.numeric(factor(foo$trial)))

enter image description here

Update Based on the comment, A possible solution would the following using ggplot, instead of stripplot. You can customise this at will. See this post for more detail.

library(ggplot2)
p<-ggplot(foo, aes(x=element, y=conc, color=trial)) +
  geom_jitter(position=position_dodge(0.5))
p

enter image description here

If you want to use stripplot I think you have to transform the data a bit. You could add the following:

foo$group<-paste(foo$element,foo$trial)
stripplot(conc~group,data=foo,pch=as.numeric(factor(foo$trial)))

enter image description here

horseoftheyear
  • 917
  • 11
  • 23
  • I greatly appreciate the code suggestion. However, I am actually trying to plot the values side by side in separate "columns" of points... So in this case, for N, P, and K from 2 trials, along the x-axis I would have, in this order: N from Trial 1, N from Trial 2, P from trial 1, P from Trial 2, K from Trial 1, K from Trial 2... So, for 3 nutrients and 2 trials I would have 2x3 = 6 "columns" of values, not 3 columns where the values are combined within a nutrient for both trials and delineated with symbols. – Ezra Citron Nov 14 '15 at 05:23
  • See the updated answer. I did it using `ggplot`, see the referenced website for more details. I'm sure that there should be a way using `stripplot`, but I think it involves some more data transformation. – horseoftheyear Nov 14 '15 at 10:46
  • Thanks! I ended up doing some data transformation (reordering the columns) and figured it out in `stripplot`, but I used the `ggplot` code and that worked wonderfully as well. For my final paper I am going to figure out how to make it look nice in `ggplot`, as it seems that it would be very worthwhile to learn how to work with `ggplot2`. – Ezra Citron Nov 16 '15 at 20:00
  • Maybe useful to post your solution for others who have the same issue. – horseoftheyear Nov 16 '15 at 20:13
  • I am new to using this site so I wasn't really sure where to put the solution, because it wouldn't fit as a "comment," but now I realize that I can just add an answer to my own question at the bottom. If I am supposed to modify the original post instead, please let me know. Thanks! – Ezra Citron Nov 17 '15 at 21:24
  • Now I am realizing that for the moment I haven't really solved the issue beyond what you have posted, because I literally just made a `data.frame` where all of my variables were in the correct order, and plotted it that way in a basic `stripplot()`. Once I figure out how to do it more elegantly, I will post my solution. – Ezra Citron Nov 17 '15 at 21:32