如何用R重现这个动态分布图?

时间:2017-07-25 09:21:54

标签: r plot ggridges

G Elliot Moris表示political polarization through time using a moving distribution plotPlot of polarization in US politics 1963-2013

从这个问题:How to use 'facet' to create multiple density plot in GGPLOT我设法使用facets使用虚拟数据重现类似的情节:

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 ~.)

enter image description here

但我希望这些分布在原始情节中重叠。这些图是否有特定的名称,是否可以用R?

重现它们

[编辑] this global temperature distribution plot中提供了类似情节的动态版本。

1 个答案:

答案 0 :(得分:9)

更新:用于创建此图的包现在名为“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