以下是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,
它只是忽略了填充参数并绘制输出,这是为什么呢?
最终似乎起作用的是:
ggplot(iris, aes(x=Sepal.Length, y=Species, fill=..x..)) +
geom_density_ridges_gradient()但我不确定为什么它会起作用。
基本上,我对与填充相对应的参数应该放在哪里感到困惑。
发布于 2018-05-07 19:27:27
我认为这里可能发生的事情是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分组,则可以(从here)添加以下内容:
ggplot(
data = iris,
aes(x=Sepal.Length, y=Species, fill=Species, group=Sepal.Length)
) +
geom_ridgeline(
stat = "binline",
bins = 20,
draw_baseline = FALSE
)..。这将生成以下内容:

https://stackoverflow.com/questions/50212608
复制相似问题