简体   繁体   English

ggplot不绘制boxplot

[英]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(): 我对R有点陌生,对ggplot非常陌生,但是我遇到了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! 用适当的X和Y轴绘制一个空白图形,但完全没有图! 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! 因此,很明显它知道Length变量的限制(给定我的数据,轴截止是适当的),但是绝对拒绝在图表上放箱线图!

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! 我已经在其他数据集上测试了ggplot,并且两个命令都可以正常工作,所以显然这只是我的问题!

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. 考虑ggplot2图形构造的一种方法是将投影仪和分层(因为每个几何图形是一个层)几何图形视为透明表。

ggplot() turns on the projector but sets no defaults for any layer (transparency sheet) ggplot()打开投影机,但未设置任何默认值(透明胶片)

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>)将打开投影仪,并将some.data.frame设置为即将到来的图层的默认数据源。

ggplot(data = <some.data.frame>, mapping = aes()) would turn on the projector and set the default data set and aesthetics for each layer. ggplot(data = <some.data.frame>, mapping = aes())将打开投影仪并设置默认数据集和每一层的美观度。

At this point, no layers (geoms) have been created or plotted. 此时,尚未创建或绘制任何图层(geom)。 In the ggplot call, the ... allows for additional arguments to be passed, however, they are ignored. ggplot调用中, ...允许传递其他参数,但是,它们将被忽略。 This is why your 这就是为什么你的

ggplot(database, aes(x = SampleType, Y = Length), geom_boxplot()) ggplot(数据库,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. 将打开项目,将默认数据集设置为database和默认外观。 We then add on the layer geom_boxplot ,as if a transparency sheet was placed on the overhead projector, to show the boxplots. 然后,我们在图层geom_boxplot上添加,就像在投影仪上放置了透明纸一样,显示了箱形​​图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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