简体   繁体   English

如何在R中将基于不同数据集的图层与ggplot2合并

[英]How to combine layers based on different datasets with ggplot2 in R

I'd like to produce a plot that shows a time series followed by a set of forecast distributions, each represented as a violin plot. 我想制作一个显示时间序列的图,然后显示一组预测分布,每个分布都以小提琴图表示。 The example code below creates and plots a time series (as a line plot) and two violin plots separately. 下面的示例代码分别创建和绘制一个时间序列(作为线图)和两个小提琴图。

set.seed(12345)
x <- data.frame(time=1:50, dat=rnorm(50))

y1 <- rnorm(500)
y2 <- rnorm(500, sd=5)
y <- data.frame(time=as.factor(c(rep(51,500),rep(52,500))), dat=c(y1,y2)) 

ggplot(x, aes(x=time, y=dat)) +
  geom_line()

ggplot(y, aes(x=time, y=dat)) +
  geom_violin()

How can I combine these into a single chart with a line plot from time points 1 to 50 (along the x axis) followed by the two violin plots at time points 51 and 52, respectively? 如何将它们组合成一个图表,并具有从时间点1到50(沿x轴)的线图,然后分别是在时间点51和52的两个小提琴图?

I'm not sure you can plot discrete and continuous variables on the same axis. 我不确定您是否可以在同一轴上绘制离散变量和连续变量。 So you'll have to compromise. 因此,您必须妥协。 Markus has opted to discretize the x variables while I prefer to make y variable into continuous. Markus选择离散化x变量,而我更喜欢使y变量连续。 Notice that I've changed the way y is generated (removed the factor). 注意,我已经更改了y的生成方式(删除了因子)。

library(ggplot2)

set.seed(12345)
x <- data.frame(time=1:50, dat=rnorm(50))

y1 <- rnorm(500)
y2 <- rnorm(500, sd=5)
y <- data.frame(time=c(rep(51, 500), rep(52, 500)), dat=c(y1,y2)) 

ggplot(x, aes(x = time, y = dat)) +
  theme_bw() +
  scale_x_continuous(limits = c(0, 52)) +
  geom_line() + 
  geom_violin(data = y, aes(group = as.factor(time)))

在此处输入图片说明

You need to convert your y$time factor levels to integer , add a grouping variable, and move your data = ... to the specific geoms. 您需要将y$time因子级别转换为integer ,添加分组变量,然后将data = ...移动到特定几何。 1

# Transform your factor variable to its factor levels
y$time <- as.integer(levels(y$time))[y$time]

# Plot with grouping for the violin plot (as x is not a factor anymore) 
ggplot() + 
    geom_line(data = x, aes(x = time, y = dat)) + 
    geom_violin(data = y, aes(x = time, y = dat, group = time))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM