LY27
LY27

Reputation: 31

adding limits to categorical boxplot, ggplot2

Hi there ggplot2 community-

I can't seem to figure out how to resolve this problem and I'd love your help!

I have constituent concentration data taken at individual sites, and what I'd like to do is construct a boxplot showing the distribution of samples and the local limits at each site. However, in my set of data, some sites have limits, and others don't. I'd like to place a horizontal line above my boxplot, which shows the local limit, but I'd like for lines to appear only over those sites that have them.

I'm running into errors using geom_segment and geom_hline and geom_errorbar and even geom_crossbar when trying to place a line over only a couple of the sites and not the whole dataset. I'd also like to avoid unnecessary lines.

Here's some sample iris data to work with. Analogous to my situation, I would like to add a horizontal line at sepal length=8 to appear above "versicolor" and "virginica" species only.

Is there a way to do this?

library(ggplot2)

sepal_leng<-ggplot(iris, aes(factor(Species), y=Sepal.Length))
sepal_leng<-sepal_leng+geom_boxplot()
sepal_leng

Upvotes: 1

Views: 187

Answers (1)

aosmith
aosmith

Reputation: 36084

I would do this by creating a separate dataset that contains the groups and their limits.

limdat = data.frame(Species = c("versicolor", "virginica"), limits = 8)

You could then use this dataset to add a horizontal line via geom_crossbar or geom_errorbar or something. Notice that the ymax and ymin are set to y to make the line horizontal.

ggplot(iris, aes(factor(Species), y=Sepal.Length)) +
    geom_boxplot() +
    geom_errorbar(data = limdat, aes(y = limits, ymin = ..y.., ymax = ..y..))

enter image description here

You may end up wanting a legend to indicate what the lines are, which you can do by mapping a constant to linetype.

ggplot(iris, aes(factor(Species), y=Sepal.Length)) +
    geom_boxplot() +
    geom_errorbar(data = limdat, 
                aes(y = limits, ymin = ..y.., ymax = ..y.., linetype = "Local limit")) +
    scale_linetype_discrete(name = NULL)

enter image description here

Upvotes: 3

Related Questions