Reputation: 55
I am attempting to plot a boxplot where my x-axis is a continuous time-scale which is growing degree days i.e. 0 to 2500. I would like to get a boxplot with x-axis values correctly spaced on a continuous time-scale rather than a discrete one. Normally if it were a regular time/date, I could have used a 'scale_x_date' with ggplot2 in R. However, since the numbers are outside date/time scale I am not sure how can we correctly space the x-axis values. Here is the dummy example:
library(ggplot2)
set.seed(1234)
#get data
df <- data.frame(y=abs(rnorm(8)),
x=as.factor(rep(c(0,100,200,500),times=2)))
ggplot(aes(y=y,x=x), data=df) +
geom_boxplot()
This gives me the plot
where my x-axis is not spaced based on its numeric values. Instead, I would like to get a boxplot where the spacing between 200 to 500 should be three times more than 100-200. My actual data has x-axis values ranging 0-2500 growing days. I am looking for ggplot2 specific solution preferably.
Upvotes: 4
Views: 5625
Reputation: 31452
df <- data.frame(y=abs(rnorm(8)),
x=rep(c(0,100,200,500),times=2))
ggplot(df, aes(x, y, group=x)) +
geom_boxplot()
This solution relies on two changes. First, to plot boxes positioned on a continuous x axis, we need to provide numeric rather than factor x values. However, this does not work by itself, because without x values being grouped by factor levels, ggplot no longer knows how to group the data into different boxes. So, we also need to provide an additional grouping variable.
Upvotes: 4