r - ggridges 中的计算变量
问题描述
这是一个MWE:
library(ggplot2)
library(ggridges)
ggplot(iris, aes(x=Sepal.Length, y=Species, fill=..x..)) +
geom_density_ridges_gradient()
我的疑问是:我们可以不说fill = Sepal.Length
吗?
我知道它..x..
指的是一个计算变量x
,并且调用geom_density_ridges_gradient
可能不会在 ggplot 术语中看到该变量,但是在我们可以参考 ggplot 咒语时Sepal.Length
,可以吗?
有人可以澄清为什么我们需要说..x..
而不是Sepal.Length
在这种情况下吗?我不完全确定这里的推理。
更准确地说,为什么这不起作用:
ggplot(iris, aes(x=Sepal.Length, y=Species,
fill=Sepal.Length)) + geom_density_ridges_gradient()
<------------我想从这里开始改写我的查询------------>
如果我做 :
ggplot(iris, aes(x = Sepal.Length, y = Species)) +
geom_density_ridges_gradient(fill = Sepal.Length)
geom_density_ridges_gradient 将无法找到变量 Sepal.Length 并给出错误。
所以,正确的咒语应该是:
ggplot(iris, aes(x = Sepal.Length, y = Species)) +
geom_density_ridges_gradient(fill = ..x..)
geom_density_ridges_gradient 应该能够找到计算变量 ..x.. ,但它不起作用。有人可以解释这是为什么吗?
另一个查询:
如果我做 :
ggplot(iris, aes(x = Sepal.Length, y = Species,fill = Sepal.Length)) +
geom_density_ridges_gradient()
为什么它没有给我一条错误消息说 Sepal.Length not found,
它只是忽略了 fill 参数并绘制了输出,这是为什么呢?
最终似乎起作用的是:
ggplot(iris, aes(x=Sepal.Length, y=Species, fill=..x..)) +
geom_density_ridges_gradient()
但我不确定为什么它会起作用。
基本上,我对对应于填充的参数应该放置在哪里感到困惑。
解决方案
我认为这里可能发生的事情Sepal.Length
不是一个类别。
考虑到这一点,我可以假设这就是您所追求的吗?(如果没有,请告诉我,我会尽力回答您需要什么。)
library(tidyverse)
library(ggridges)
ggplot(
data = iris,
aes(x=Sepal.Length, y=Species, fill=Species)
) +
geom_ridgeline(
stat = "binline",
bins = 20,
draw_baseline = FALSE
)
运行此代码会生成此图表:
但是,如果您想Sepal.Length
按照您的建议进行分组,您可以添加以下内容(从此处):
ggplot(
data = iris,
aes(x=Sepal.Length, y=Species, fill=Species, group=Sepal.Length)
) +
geom_ridgeline(
stat = "binline",
bins = 20,
draw_baseline = FALSE
)
...这将产生以下内容:
推荐阅读
- python - Python 不会像字典格式那样从文本文件中读取提取的行
- azure - 自动备份 Azure SQL 数据库
- php - 组合不同长度的 GIF
- php - Wordpress 自定义帖子父 slug
- javascript - 逗号是否仅适用于javascript中的console.log以将变量与字符串连接起来?
- python-3.x - 如何根据第二个参数对输出文件进行排序?
- database - Laravel BD::rollBack 不要在我的代码中工作
- python - Jupyter Notebook Pycharm 2019 IDE中的Python导入错误
- angular7 - 在 ag-grid 中使用自定义单元格渲染器时无法读取未定义的属性“导航”
- umbraco - 获取子项的图像 ID