2017-07-25 69 views
1

G Elliot Moris显示political polarization through time using a moving distribution plotPlot of polarization in US politics 1963-2013如何用R重现此移动分布图?

从这个问题: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 ~.) 

enter image description here

但我想分布在原作情节重叠。是否有这样的情节的具体名称,是否有可能用R重现它们?

[编辑]类似图的动态版本可在this global temperature distribution plot中找到。

+0

嘿保罗,那是overlaping部分的transparancy你想达到什么目的?如果是这样,只需在'geom_density'函数中加入'alpha = value'。或者这也是每组之间的垂直重叠? –

+0

那么,它在ggplot中看起来像一个艰难的情节,但是你可能会发现这篇文章很有用。 https://www.r-bloggers.com/3d-density-plot-in-r-with-plotly/ – zielinskipp

+0

@PaulEndymion我想重现垂直重叠。 –

回答

7

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

+0

不错!只是一个建议,你的alpha参数应该在'aes'之外。 –

+0

谢谢,这接近我正在寻找。我怎样才能删除水平的黑线?我尝试过'theme(axis.line = element_blank())',但它不会删除这些行。 –

+0

嗨保罗,我已经添加了删除水平黑线的rel_min_height参数。正如@PaulEndymion所建议的那样,我也在aes之外移动了alpha参数。 – Ian