3

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

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

Which looks like this: enter image description here

Since the angle is a continuous variable, how can I make boxplots of the wind speeds vs angle? Is there a way of splitting it into bins of say 10º?

In the meantime this looks nice but no binning:

boxplot(x=expt$iso.xs[,8],y=expt$iso.xs[,2],col = "51",horizontal = T)

enter image description here

ggplot

I tried to put the data into a data.frame:

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

> dput(head(df,4))
structure(list(X1 = c(736105.427083333, 736105.447916667, 736105.46875, 
736105.489583333), X2 = c(16.6272942088244, 18.9670495788553, 
20.520886525004, 19.9444891323463), X3 = c(2.18373958682272, 
2.48966826448766, 2.68763649372061, 2.61155634303594), X4 = c(7.23477401700351, 
7.0362340804356, 10.3553936916397, 10.3069117034676), X5 = c(0.979163231089235, 
0.964036556743421, 1.36984543807235, 1.36553014658159), X6 = c(4.01, 
3.82, 4.99, 4.85), X7 = c(4.2, 4, 5.14, 5.12), X8 = c(238.62, 
243.14, 247.02, 249.57)), .Names = c("X1", "X2", "X3", "X4", 
"X5", "X6", "X7", "X8"), row.names = c(NA, 4L), class = "data.frame")

ggplot(df, aes(y = X2, x = factor(X8))) +
  geom_boxplot( outlier.shape = NA,outlier.colour = NA)

Produces the right idea but not controlled enough:

enter image description here

INCLUDING Comments below:

df<-data.frame(expt$iso.xs)
df$X8 <- cut(df$X8, breaks = seq(0,360,10))
ggplot(df, aes(y = X2/45, x = factor(X8))) +
  geom_boxplot( outlier.shape = NA,outlier.colour = NA)+
  geom_point(color="red")+
  labs(x = "Angle", y = "Normalised ACh" )+
  scale_x_discrete(breaks=seq(0,360,15))

The new graphs looks nicely like:

enter image description here

But can I fix the xticks?

HCAI
  • 2,213
  • 8
  • 33
  • 65
  • 1
    It's more likely that we will be able to help you if you make a minimal reproducible example to go along with your question. Something we can work from and use to show you how it might be possible to solve your problem. I take you are already familiar with [this SO post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to make a great reproducible example in R. – Eric Fail Dec 29 '15 at 17:39
  • 6
    see the `cut` function. you probably want something like `df$X8 <- cut(df$X8, breaks = c(0, 10, 20, 30, 40, 50, Inf)` where you can play with breaks, depending on what you want to bin in the boxes. – jeremycg Dec 29 '15 at 17:46
  • Hi Eric happy holidays., Thanks for looking at this so quickly. I have added a dput for this. – HCAI Dec 29 '15 at 17:46
  • @jeremycg Great, thank you so much! I'm always amazed how easy some things are in R. Just out of curiosity is it possible to superimpose the plot with the red dots underneath the boxplot? – HCAI Dec 29 '15 at 17:53
  • 2
    after the cut, using ggplot, you can do something like `ggplot(df, aes(x = X8, y = X2)) +geom_boxplot() + geom_point(colour = "red")` – jeremycg Dec 29 '15 at 17:57
  • @jeremycg Thanks very much, it looks very nice. Only question is how to make the xtickmarks readable? + scale_x_discrete(breaks=seq(0,360,15)) makes it disappear altogether... (see edit) – HCAI Dec 29 '15 at 18:08
  • It says: Error: Discrete value supplied to continuous scale – HCAI Dec 29 '15 at 18:36
  • 2
    try: `scale_x_discrete(labels = seq(0,360,10))` – jeremycg Dec 29 '15 at 18:48
  • @jeremycg I understand thank you. I tried using scale_x_discrete(labels = seq(0,360,15)) but this doesn't work without cycling round. Is there any way of using a wider bin for a label than when we cut df$X8? ie label every 15th angle instead of every 10th? – HCAI Dec 29 '15 at 20:36
  • Why not post an answer with a plot so the rest of us can see what is happening? – Mike Wise Dec 30 '15 at 15:39

0 Answers0