0

I'd like to plot two graphs ontop of each other like in this post.

Experimental data: I have continuous variable displaying the angle of wind on a given day in a list called expt$iso_xs[,8], I then have the wind speed corresponding to that angle in expt$iso_xs[,2].

df<-data.frame(expt$iso.xs)

head(expt$iso.xs)
         [,1]     [,2]     [,3]      [,4]      [,5] [,6] [,7]   [,8]
 736105.4 16.62729 2.183740  7.234774 0.9791632 4.01 4.20 238.62
 736105.4 18.96705 2.489668  7.036234 0.9640366 3.82 4.00 243.14
 736105.5 20.52089 2.687636 10.355394 1.3698454 4.99 5.14 247.02
 736105.5 19.94449 2.611556 10.306912 1.3655301 4.85 5.12 249.57
 736105.5 19.43309 2.551787 11.098302 1.4646251 4.83 5.12 243.89
 736105.5 20.48259 2.689075 11.928011 1.5710530 4.89 5.09 254.23

Which looks like this: enter image description here

Simulation data: I have a data.frame z that contains predictions for a subset of the above angles (0-90º).

head(z,15)
   Tracer angle treatment bigangle
  71.101     0         S      150
  71.101     0         S      150
  71.105     15         S      165
  71.105     15         S      165
  71.098     30         S      180
  71.098     45         S      195
  71.114     60         S      210
  71.114     80         S      230
  71.110     90         S      240

Plotting it using bigangle as factor and Tracer as :

ggplot() +
  geom_boxplot(data=z, aes(y = (3600/Tracer/93.241), x = factor(bigangle)),outlier.shape = NA,outlier.colour = NA)+
  coord_cartesian(ylim=c(0, 1))+
  labs(x = "Angle", y = "Normalised ACh" )+
  scale_x_discrete(labels=seq(0,360,10))+
  theme_classic()

looks like this:

enter image description here

I'd like to superimpose the boxplot ontop of the portion of red points (between 150º and 240º) but the following doesn't work:

ggplot() +
      geom_boxplot(data=z, aes(y = (3600/Tracer/93.241), x = factor(bigangle)),outlier.shape = NA,outlier.colour = NA)+
      geom_point(data=df, aes(y = X2/45, x = X8),color="red")+
      coord_cartesian(ylim=c(0, 1))+
      labs(x = "Angle", y = "Normalised ACh" )+
      scale_x_discrete(labels=seq(0,360,10))+
      theme_classic()

enter image description here

Any thoughts would be much appreciated, Cheers

2
  • Note this is a different question to stackoverflow.com/posts/34515961/edit but using the same data Commented Dec 30, 2015 at 0:20
  • 1
    Could you please make a minimal reproducible example with copy-pasteable data? Either use dput() to share data, use built-in data, or simulate data (with a random seed). I don't think the 6 rows of expt and 15 rows of z are sufficient to demo code on. Commented Dec 30, 2015 at 0:24

1 Answer 1

5

I think your only problem is trying to specify a discrete x scale for continuous data. That and you need a group for your boxplot geom.

As an illustrative example:

mt = mtcars
mt$wt_bin = cut(mt$wt, breaks = c(1, 3, 4.5, 6))
ggplot(mt, aes(x = wt, y = mpg)) +
    geom_point() +
    geom_boxplot(aes(group = wt_bin, x = wt), alpha = 0.4)

enter image description here

As the geom_boxplot help says:

You can also use boxplots with continuous x, as long as you supply a grouping variable. cut_width is particularly useful

The example in the help shows this code:

ggplot(diamonds, aes(carat, price)) +
  geom_boxplot(aes(group = cut_width(carat, 0.25)))

You can, of course, add a geom_point layer (though in the diamonds data there are too many points for that to be a nice plot).

For your scale, don't use a discrete scale unless you have factors on the axis. You probably want scale_x_continuous(breaks = seq(0, 360, 10)).

Different data sets can be used in the usual way, with the data argument. Continuing the previous example but using different data for the geom_point layer:

similar_to_mt = data.frame(wt = runif(100, 1, 6), mpg = rnorm(100, 20, 4))
ggplot(mt, aes(x = wt, y = mpg)) +
    geom_point(data = similar_to_mt) +
    geom_boxplot(data = mt, aes(group = wt_bin, x = wt), alpha = 0.4)

enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you Gregor. Could you for example superimpose the diamond data onto the mt plots? This is what im attempting...
Next time you ask a question, please do share data reproducibly. It would make this much faster to answer.
Superb! Thank you so much Gregor, I really appreciate it. I'm trying learn how to make data represent my own for next time. Just out of curiosity, the scale_x_continuous(breaks = seq(0, 360, 10)) argument gives an error saying: Error: Discrete value supplied to continuous scale. Do you think that's right?
Since you didn't share data it's your job to convert your data to match what I simulated. In my example data, I always map the x aesthetic to columns of class numeric. Sounds like in your code you map x to a column of class factor. Don't do that. Convert it to numeric.
Convert it to numeric (using as.numeric(as.factor()) as we said in this question. The only factor I use in the example is the column I map to group, which determines which points go into which boxplot.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.