使用facet_grid和多个数据点时,将geom_point和geom_line结合在一起(R ggplot2) [英] Combining geom_point and geom_line when using facet_grid and multiple data points (R ggplot2)

查看:111
本文介绍了使用facet_grid和多个数据点时,将geom_point和geom_line结合在一起(R ggplot2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将具有一个类似于以下的数据集,我想使用facet_grid函数绘制该数据集:

I'd have a data set similar to the following that I would like to plot using facet_grid function:

IV1<-c('DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO',
       'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO',
       'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO',
       'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO')
IV2<-c('DF', 'DF', 'SN', 'SN', 'SV', 'SV', 'DF', 'DF', 'SN', 'SN', 'SV', 'SV',
       'DF', 'DF', 'SN', 'SN', 'SV', 'SV', 'DF', 'DF', 'SN', 'SN', 'SV', 'SV',
       'DF', 'DF', 'SN', 'SN', 'SV', 'SV', 'DF', 'DF', 'SN', 'SN', 'SV', 'SV',
       'DF', 'DF', 'SN', 'SN', 'SV', 'SV', 'DF', 'DF', 'SN', 'SN', 'SV', 'SV')
IV3<-c('Adult', 'Adult', 'Adult', 'Adult', 'Adult', 'Adult', 'Child', 'Child', 'Child', 'Child', 'Child', 'Child', 
       'Adult', 'Adult', 'Adult', 'Adult', 'Adult', 'Adult', 'Child', 'Child', 'Child', 'Child', 'Child', 'Child',
       'Adult', 'Adult', 'Adult', 'Adult', 'Adult', 'Adult', 'Child', 'Child', 'Child', 'Child', 'Child', 'Child',
       'Adult', 'Adult', 'Adult', 'Adult', 'Adult', 'Adult', 'Child', 'Child', 'Child', 'Child', 'Child', 'Child')
Subj<-as.character(c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,
                     5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8))
Value<-runif(48)
data<-data.frame(Subj, IV1, IV2, IV3, Value)

我可以像这样成功地绘制均值数据:

I can successfully plot the mean data like this:

library(ggplot2)
agg<-aggregate(Value~IV1*IV2*IV3, data=data, FUN="mean")

(P1<-ggplot(agg, aes(x=IV1, y=Value)) + theme_bw() + facet_grid(IV3~IV2) + 
  geom_point(aes(size=1.5, colour=factor(IV2), shape=factor(IV1))))

这看起来像这样:

我还可以像这样成功绘制各个主题数据:

I can also successfully plot the individual subject data like so:

(P2<-ggplot(data, aes(x=IV1, y=Value, group=Subj)) + theme_bw() + 
facet_grid(IV3~IV2)+ 
geom_point(aes(group=Subj, colour=factor(IV2), shape=factor(IV1))))

看起来像这样:

但是,我理想地希望(a)聚合均值和Subj数据都在同一图上(均值数据点较大),并且(b)对于要合并的每个构面内的数据点在DO和PO点之间(对于总计均值和各个Subj数据点).

However, I would ideally like for (a) both the aggregate mean and the Subj data to be on the same plot (with the mean data points being larger) and (b) for the data points within each facet to be joined between the DO and PO points (for both the aggregate mean and the individual Subj data points).

有点像这里的模型(油漆):

A bit like this mock-up here (make in paint):

提前谢谢!

推荐答案

agg$Subj <- rep(1:2, each = nrow(agg) / 2)
ggplot(data, aes(x=IV1, y=Value, colour=factor(IV2), shape=factor(IV1), group = Subj)) +  
    facet_grid(IV3~IV2)+ 
    geom_point() + 
    geom_point(data = agg, size = 5) +
    geom_line() +
    geom_line(data = agg, linetype = "dashed") +
    theme_bw()

我正在使用另一个linetype在视觉上区分实际观察值和合计值(只是要考虑的一个想法).

I'm using another linetype to visually distinguish between actual observations and aggregate values (just an idea to consider).

这篇关于使用facet_grid和多个数据点时,将geom_point和geom_line结合在一起(R ggplot2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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