2

I'm a little new to R and very new to ggplot but I've encountered an issue with geom_boxplot():

I'm trying to draw boxplots of sequence length data, separated by sample type. The sequence lengths are all integers and the sample types have been stored as factors. The command:

> ggplot(database, aes(x = SampleType, Y = Length), geom_boxplot())

draws a blank graph, with the appropriate X and Y axes, but no plots at all! So clearly it knows the limits of the Length variable (axes cutoffs are appropriate given my data) but is absolutely refusing to put boxplots on the graph!

Strangely, the command:

> ggplot(database) + geom_boxplot(aes(x = SampleType, Y = Length))

works!

I've tested ggplot on other datasets and both commands work fine, so its clearly just a problem with me!

I was hoping someone could tell me the difference between the two syntaxes, and potentially why one works when the other doesn't?

Thanks!

2
  • 3
    your syntax is wrong in your first example - you want ggplot(df, aes(...)) + geom_boxplot() Commented Apr 22, 2017 at 18:58
  • 1
    Please provide a reproducible example. Commented Apr 22, 2017 at 19:30

1 Answer 1

2

One way to think about the construction of ggplot2 graphics is to think of an overhead projector and layering (as each geom is a layer) geoms as transparency sheets.

ggplot() turns on the projector but sets no defaults for any layer (transparency sheet)

ggplot(data = <some.data.frame>) would turn on the projector and set some.data.frame as the default data source for the forthcoming layers.

ggplot(data = <some.data.frame>, mapping = aes()) would turn on the projector and set the default data set and aesthetics for each layer.

At this point, no layers (geoms) have been created or plotted. In the ggplot call, the ... allows for additional arguments to be passed, however, they are ignored. This is why your

ggplot(database, aes(x = SampleType, Y = Length), geom_boxplot())

did not error, nor produce expected results.

The code block

ggplot(database, aes(x = SampleType, y = Length)) +
geom_boxplot()

will turn on the project, set the default data set to database and the default aesthetics. We then add on the layer geom_boxplot,as if a transparency sheet was placed on the overhead projector, to show the boxplots.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! This is extremely helpful!! Not only did you answer my question but also helped me understand the machinations of ggplot! :) Appreciate it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.