gpt4 book ai didi

r - 如何用 R 重现这个移动分布图?

转载 作者:行者123 更新时间:2023-12-01 08:49:42 24 4
gpt4 key购买 nike

G Elliot Moris 显示 political polarization through time using a moving distribution plot .
Plot 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 中提供了类似图的动态版本.

最佳答案

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

关于r - 如何用 R 重现这个移动分布图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45299043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com