H.Traver
H.Traver

Reputation: 171

How to plot a boxplot with correctly spaced continuous x-axis values and a grouping variable in ggplot2?

I am trying to make a boxplot with a continuous x-axis where the x-axis labels are correctly spaced by their numeric value. I found the answer from this question helpful, but my data also has a grouping variable within each year.

library(ggplot2)

x <- rep(c('1','1', '10', '10', '11', '11' ,'20', '20'), 2)
y <- runif(16, 1,20)
class <- rep(c('in', 'out'), 8)
df <- data.frame(x = x, y=y, class=class)
df$x_int <- as.integer(df$x)

If I don't use the variable class then I can use the group argument to achieve the right spacing on the x-axis.

ggplot(data=df, aes(x=x_int, y=y, group=Year_int))+
  geom_boxplot()

enter image description here

But when I add fill=class, it doesn't appear to do anything and produces the exact same plot.

I would like the boxplot to show the class grouping in each year (shown below), but have the correct x-axis spacing from the first plot.

ggplot(data=df, aes(x=x, y=y, fill=class))+
  geom_boxplot()

enter image description here

Upvotes: 2

Views: 1160

Answers (1)

AndrewGB
AndrewGB

Reputation: 16856

In your data, you have a discrete variable, i.e., class. However, you need the data to be grouped by class and x_int. So, we can specify this grouping by using interaction in the group argument for x_int and class. Then, fill with class.

library(tidyverse)

df %>% 
  ggplot(aes(x=x_int, y=y, group = interaction(x_int, class), fill = class)) +
  geom_boxplot()

Output

enter image description here

Upvotes: 3

Related Questions