简体   繁体   中英

ggplot not plotting boxplot

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM