在ggplot2中创建堆积密度图 [英] Create a stacked density graph in ggplot2
问题描述
我试图在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创建的不同? p>
以下是堆积密度图:
非堆积密度图:
< img src =https://i.stack.imgur。 com / cwRFc.pngalt =在这里输入图片描述>
原始示例是 here
来自@ kohske的评论,不是美学的,所以不应该在 aes
调用中:
ggplot(钻石,aes(x = depth,y = .. density ..))+
geom_density(aes(fill = cut),position =stack)
或使用电影数据(您的示例图使用):
ggplot(aes(fill = mpaa),position =stack)
$ c $(aes(x = rating,y = .. density ..))+
geg_density c>
I'm trying to create a stacked density graph in ggplot2, and I am also trying to understand how qplot works relative to 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(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?
Here is a stacked density graph:
Non-stacked density graph:
Original example is here
From @kohske's comment, the position is not an aesthetic, and so should not be inside the aes
call:
ggplot(diamonds, aes(x=depth, y=..density..)) +
geom_density(aes(fill=cut), position="stack")
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中创建堆积密度图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!