在ggplot2中创建堆积密度图

时间:2021-02-04 14:57:14

I'm trying to create a stacked density graph in ggplot2, and I am also trying to understand how qplot works relative to ggplot.

我正在尝试在ggplot2中创建堆叠密度图,我也试图了解qplot如何相对于ggplot工作。

I found the following example online:

我在网上找到了以下示例:

qplot(depth, ..density.., data=diamonds, geom="density", 
  fill=cut, position="stack")

I tried translating this into a call to ggplot because I want to understand how it works:

我尝试将其转换为对ggplot的调用,因为我想了解它是如何工作的:

ggplot(diamonds, aes(x=depth, y=..density..)) + 
  geom_density(aes(fill=cut, position="stack"))

This creates a density graph, but does not stack it.

这会创建密度图,但不会堆叠它。

What is the different between what qplot is creating and what ggplot is creating?

qplot创建的内容与ggplot创建的内容有何不同?

Here is a stacked density graph:

这是一个堆叠密度图:

在ggplot2中创建堆积密度图

Non-stacked density graph:

非堆叠密度图:

在ggplot2中创建堆积密度图

Original example is here

原始的例子在这里

1 个解决方案

#1


6  

From @kohske's comment, the position is not an aesthetic, and so should not be inside the aes call:

从@ kohske的评论来看,这个位置不是审美,因此不应该在aes调用中:

ggplot(diamonds, aes(x=depth, y=..density..)) + 
  geom_density(aes(fill=cut), position="stack")

在ggplot2中创建堆积密度图

or using the movies data (which your example graphs use):

或使用电影数据(示例图表使用):

ggplot(movies, aes(x=rating, y=..density..)) + 
  geom_density(aes(fill=mpaa), position="stack")

在ggplot2中创建堆积密度图

#1


6  

From @kohske's comment, the position is not an aesthetic, and so should not be inside the aes call:

从@ kohske的评论来看,这个位置不是审美,因此不应该在aes调用中:

ggplot(diamonds, aes(x=depth, y=..density..)) + 
  geom_density(aes(fill=cut), position="stack")

在ggplot2中创建堆积密度图

or using the movies data (which your example graphs use):

或使用电影数据(示例图表使用):

ggplot(movies, aes(x=rating, y=..density..)) + 
  geom_density(aes(fill=mpaa), position="stack")

在ggplot2中创建堆积密度图