如何用 R 重现这个移动分布图? [英] How to reproduce this moving distribution plot with R?

查看:109
本文介绍了如何用 R 重现这个移动分布图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

G Elliot Moris 使用移动分布图显示了

来自这个问题:

但我希望分布像原始图中一样重叠.此类图是否有特定名称,是否可以用 R 重现它们?

这个全球温度分布图.

更新:用于创建此图的包现在命名为ggridges".

为此您需要 ggjoy 包.这是一个需要整理的粗略版本:


devtools::install_github("clauswilke/ggjoy")图书馆(ggjoy)图书馆(ggplot2)#创建数据框set.seed(101)dtf <- data.frame(variable = c(rnorm(1000),范数(1000)+代表(1:10/2,每个=100)),group = rep(c("a","b"), each = 1000),年 = 代表(2001:2010,每个=100))# 使用 ggplot2 和 ggjoy 包ggplot(dtf,aes(x = variable, y = as.factor(year), fill = group)) +geom_joy(scale = 2,alpha = .5,rel_min_height = 0.01) + theme_joy()#>0.347 的拾取关节带宽

G Elliot Moris showed political polarization through time using a moving distribution plot.

From this question: How to use 'facet' to create multiple density plot in GGPLOT, I managed to use facets to reproduce a similar plot using dummy data:

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

But I would like the distributions to overlap as in the original plot. Is there a specific name for such plots and is it possible to reproduce them with R?

[EDIT] A dynamic version of a similar plot is available in this global temperature distribution plot.

解决方案

Update: the package used to created this plot is now named "ggridges".

You'll need the ggjoy package for this. Here's a rough version that needs some tidying up:


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

这篇关于如何用 R 重现这个移动分布图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆