G Elliot Moris showed political polarization through time using a moving distribution plot https://twitter.com/gelliottmorris/status/888904322326638592.
从这个问题来看:如何使用“facet”在 GGPLOT 中创建多重密度图 https://stackoverflow.com/questions/14623348/how-to-use-facet-to-create-multiple-density-plot-in-ggplot,我设法使用分面使用虚拟数据重现类似的图:
library(ggplot2)
set.seed(101)
dtf <- data.frame(variable = c(rnorm(1000),
rnorm(1000) + rep(1:10/2,each =100)),
group = rep(c("a","b"), each = 1000),
year = rep(2001:2010, each=100))
ggplot(dtf) +
geom_density(aes(x = variable, fill = group)) +
facet_grid(year ~.)
但我希望分布能够像原始图中一样重叠。这些图有具体的名称吗?是否可以用 R 重现它们?
[编辑] 类似情节的动态版本可在这个全球温度分布图 https://twitter.com/ClimateOfGavin/status/887522165196820480.
更新:用于创建此图的包现在名为“ggridges”。
为此,您需要 ggjoy 包。这是一个需要一些整理的粗略版本:
devtools::install_github("clauswilke/ggjoy")
library(ggjoy)
library(ggplot2)
#Create data frame
set.seed(101)
dtf <- data.frame(variable = c(rnorm(1000),
rnorm(1000) + rep(1:10/2,each =100)),
group = rep(c("a","b"), each = 1000),
year = rep(2001:2010, each=100))
# Use ggplot2 and ggjoy packages
ggplot(dtf,aes(x = variable, y = as.factor(year), fill = group)) +
geom_joy(scale = 2,alpha = .5,rel_min_height = 0.01) + theme_joy()
#> Picking joint bandwidth of 0.347
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)