首页 文章

在ggplot2中创建堆积密度图

提问于
浏览
5

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

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

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

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

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

这会创建一个密度图,但会堆叠它 .

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

这是一个堆叠密度图:

stacked density

非堆叠密度图:

enter image description here

原来的例子是here

1 回答

  • 6

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

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

    enter image description here

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

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

    enter image description here

相关问题