ggplot2 将两个数据集绘制成一张图
ggplot2 plot two data sets into one picture
这一定是一个常见问题解答,但我在其他答案中找不到完全相似的示例(如果您可以指出类似的问答,请随时关闭此问题)。我仍然是 ggplot2 的新手,似乎无法轻易理解它。
我有 2 个 data.frames(来自不同的混合模型),我试图将它们绘制到同一个图中。 data.frames 是:
newdat
id Type pred SE
1 1 15.11285 0.6966029
2 1 13.68750 0.9756909
3 1 13.87565 0.6140860
4 1 14.61304 0.6187750
5 1 16.33315 0.6140860
6 1 16.19740 0.6140860
1 2 14.88805 0.6966029
2 2 13.46270 0.9756909
3 2 13.65085 0.6140860
4 2 14.38824 0.6187750
5 2 16.10835 0.6140860
6 2 15.97260 0.6140860
和
newdat2
id pred SE
1 14.98300 0.6960460
2 13.25893 0.9872502
3 13.67650 0.6150701
4 14.39590 0.6178266
5 16.37662 0.6171588
6 16.08426 0.6152017
如您所见,第二个 data.frame 没有 Type
,而第一个有,因此每个 id
.
有 2 个值
我可以用 ggplot 做的是绘制其中一个,如下所示:
图 1
图2
如您所见,在图 1 中 ids
在 x 轴上由 Type
堆叠形成两组 6 ids
。然而,在图 2 中没有 Type
,而是只有 6 ids
.
我想要完成的是将 fig2 绘制到具有类似分组的 fig1 的 left/right。所以结果图看起来像图 1,但有 3 组 6 ids
.
问题还在于,我需要标记和组织生成的图形,以便 newdat
的 x 轴将包含 "model1" 和 newdat2
的标签"model2" 的标签,或表明它们来自不同型号的一些类似指示符。更糟糕的是,我需要 newdat
.
中 Type
的一些标签
我的(希望)可重现(但显然非常糟糕)的图 1 代码:
library(ggplot2)
pd <- position_dodge(width=0.6)
ggplot(newdat,aes(x=Type,y=newdat$pred,colour=id))+
geom_point(position=pd, size=5)
geom_linerange(aes(ymin=newdat$pred-1.96*SE,ymax=newdat$pred+1.96*SE), position=pd, size=1.5, linetype=1) +
theme_bw() +
scale_colour_grey(start = 0, end = .8, name="id") +
coord_cartesian(ylim=c(11, 18)) +
scale_y_continuous(breaks=seq(10, 20, 1)) +
scale_x_discrete(name="Type", limits=c("1","2"))
图 2 的代码相同,但在最后一行没有 limits
,并且在 ggplot(aes())
中为 x 轴定义了 id
据我了解,在 ggplot()
处定义内容会使整个图表中的内容 "standard" ,并且我尝试删除常见内容并分别定义 geom_point
和geom_linerange
对于 newdat
和 newdat2
,但到目前为止没有运气......非常感谢任何帮助,因为我完全被困住了。
如何添加首先向每个数据集添加一些新变量然后组合它们:
newdat$model <- "model1"
newdat2$model <- "model2"
newdat2$Type <- 3
df <- rbind(newdat, newdat2)
# head(df)
然后我们可以绘制:
library(ggplot2)
ggplot(df, aes(x = interaction(model, factor(Type)), y = pred, color = factor(id))) +
geom_point(position = position_dodge(width = 0.6), size = 5) +
geom_linerange(aes(ymin = pred - 1.96 * SE, ymax = pred + 1.96 * SE),
position = position_dodge(width = 0.6),
size = 1.5, linetype = 1)
或者,您将额外的审美传递给 geom_linerange
以进一步描述模型类型:
ggplot(df, aes(x = interaction(model, factor(Type)), y = pred, color = factor(id))) +
geom_point(position = position_dodge(width = 0.6), size = 5) +
geom_linerange(aes(ymin = pred - 1.96 * SE, ymax = pred + 1.96 * SE, linetype = model),
position = position_dodge(width = 0.6),
size = 1.5)
最后,您可能要考虑 facets
:
ggplot(df, aes(x = interaction(model, factor(Type)), y = pred, color = factor(id))) +
geom_point(position = position_dodge(width = 0.6), size = 5) +
geom_linerange(aes(ymin = pred - 1.96 * SE, ymax = pred + 1.96 * SE),
position = position_dodge(width = 0.6),
size = 1.5) +
facet_wrap(~ id)
这一定是一个常见问题解答,但我在其他答案中找不到完全相似的示例(如果您可以指出类似的问答,请随时关闭此问题)。我仍然是 ggplot2 的新手,似乎无法轻易理解它。
我有 2 个 data.frames(来自不同的混合模型),我试图将它们绘制到同一个图中。 data.frames 是:
newdat
id Type pred SE
1 1 15.11285 0.6966029
2 1 13.68750 0.9756909
3 1 13.87565 0.6140860
4 1 14.61304 0.6187750
5 1 16.33315 0.6140860
6 1 16.19740 0.6140860
1 2 14.88805 0.6966029
2 2 13.46270 0.9756909
3 2 13.65085 0.6140860
4 2 14.38824 0.6187750
5 2 16.10835 0.6140860
6 2 15.97260 0.6140860
和
newdat2
id pred SE
1 14.98300 0.6960460
2 13.25893 0.9872502
3 13.67650 0.6150701
4 14.39590 0.6178266
5 16.37662 0.6171588
6 16.08426 0.6152017
如您所见,第二个 data.frame 没有 Type
,而第一个有,因此每个 id
.
我可以用 ggplot 做的是绘制其中一个,如下所示:
图 1
图2
如您所见,在图 1 中 ids
在 x 轴上由 Type
堆叠形成两组 6 ids
。然而,在图 2 中没有 Type
,而是只有 6 ids
.
我想要完成的是将 fig2 绘制到具有类似分组的 fig1 的 left/right。所以结果图看起来像图 1,但有 3 组 6 ids
.
问题还在于,我需要标记和组织生成的图形,以便 newdat
的 x 轴将包含 "model1" 和 newdat2
的标签"model2" 的标签,或表明它们来自不同型号的一些类似指示符。更糟糕的是,我需要 newdat
.
Type
的一些标签
我的(希望)可重现(但显然非常糟糕)的图 1 代码:
library(ggplot2)
pd <- position_dodge(width=0.6)
ggplot(newdat,aes(x=Type,y=newdat$pred,colour=id))+
geom_point(position=pd, size=5)
geom_linerange(aes(ymin=newdat$pred-1.96*SE,ymax=newdat$pred+1.96*SE), position=pd, size=1.5, linetype=1) +
theme_bw() +
scale_colour_grey(start = 0, end = .8, name="id") +
coord_cartesian(ylim=c(11, 18)) +
scale_y_continuous(breaks=seq(10, 20, 1)) +
scale_x_discrete(name="Type", limits=c("1","2"))
图 2 的代码相同,但在最后一行没有 limits
,并且在 ggplot(aes())
id
据我了解,在 ggplot()
处定义内容会使整个图表中的内容 "standard" ,并且我尝试删除常见内容并分别定义 geom_point
和geom_linerange
对于 newdat
和 newdat2
,但到目前为止没有运气......非常感谢任何帮助,因为我完全被困住了。
如何添加首先向每个数据集添加一些新变量然后组合它们:
newdat$model <- "model1"
newdat2$model <- "model2"
newdat2$Type <- 3
df <- rbind(newdat, newdat2)
# head(df)
然后我们可以绘制:
library(ggplot2)
ggplot(df, aes(x = interaction(model, factor(Type)), y = pred, color = factor(id))) +
geom_point(position = position_dodge(width = 0.6), size = 5) +
geom_linerange(aes(ymin = pred - 1.96 * SE, ymax = pred + 1.96 * SE),
position = position_dodge(width = 0.6),
size = 1.5, linetype = 1)
或者,您将额外的审美传递给 geom_linerange
以进一步描述模型类型:
ggplot(df, aes(x = interaction(model, factor(Type)), y = pred, color = factor(id))) +
geom_point(position = position_dodge(width = 0.6), size = 5) +
geom_linerange(aes(ymin = pred - 1.96 * SE, ymax = pred + 1.96 * SE, linetype = model),
position = position_dodge(width = 0.6),
size = 1.5)
最后,您可能要考虑 facets
:
ggplot(df, aes(x = interaction(model, factor(Type)), y = pred, color = factor(id))) +
geom_point(position = position_dodge(width = 0.6), size = 5) +
geom_linerange(aes(ymin = pred - 1.96 * SE, ymax = pred + 1.96 * SE),
position = position_dodge(width = 0.6),
size = 1.5) +
facet_wrap(~ id)