I am attempting to make boxplots of some complex data. I have sorted the classes by one particular field (not the class field) and would now like to be able to label each box with the value of that sort-by field. I know from the way the data is structured that the value of this sort-by attribute will be the same for every observation within the class, and I would like to essentially annotate the chart with this additional piece of information.
I thought of trying to accomplish this by adding a point layer to the plot and then labeling those points. I attempted to do this using code like this example I mocked up using the mtcars
data set for reproducability. For the sake of this example pretend that the variable gears
would be the same for each distinct value of cyl
. The "gear/1000000" part is just to get the labels all near the axis.
mtcars %>% group_by(cyl) %>%
ggplot(aes(x = reorder(cyl, gear), y = mpg)) +
geom_point(show.legend = FALSE, aes(x = reorder(cyl, gear), y = gear/1000000)) +
geom_text(aes(label = gear)) +
geom_boxplot(aes(colour=carb),varwidth = TRUE)
I feel like this is close, but this code is putting the labels on the boxplots instead of on the points, which is the opposite of what I'm looking for. How can I ask ggplot to label only the points from geom_point()
? Or is there an easier way to accomplish my objective?
EDIT: Here is what my plot now looks like, thanks to the answer provided below. Boxplots of IRI distribution for various pavement segments