Hi,
I have a dataframe where I have binned diameters of pipe for plotting into a boxplot. The plot will correctly place the boxes in order except for any diameters at 10 inches, for some reason it will place it first on the x-axis.
I have reproduced the error using the iris
dataframe; is there any way to manipulate the plot so that it starts at the smallest diameter, up to 10 and then "over 10 in"?
I have poked around scale_x_discrete
put cannot see where I would change the output. I have also tried mutate_if(Bin, is.character, as.factor)
to see if changing to a factor would help but it did not.
d <- iris
boxplot_d <- d %>%
mutate(Bin = case_when(Petal.Length > 6.5 ~ 'Over 10 in',
Petal.Length >= 6 ~ '10 in.',
Petal.Length >= 5 ~ '5 in.',
Petal.Length >= 4 ~ '4 in. ',
Petal.Length >= 3 ~ '3 in. ',
Petal.Length < 3 ~ '2 in to 3 in'))
p1 <- ggplot(data=boxplot_d, aes(x=Bin, y=Sepal.Length)) +
geom_boxplot(color = "black", alpha = 0.2)
p1
Colin