gpt4 book ai didi

r - 将额外的图插入多面网格,ggplot2

转载 作者:行者123 更新时间:2023-12-02 08:08:51 25 4
gpt4 key购买 nike

我想以 ggplot2 多面图格式显示一组参数。其中三个参数来自同一个数据集,可以很容易地进行分面。我现在想在网格中添加第四个图,其数据集的长度/值与其他图完全不同(但仍与一起查看相关)。

这是我拥有的最好的(糟糕)。有没有办法准确对齐坐标和主题?谢谢

library(ggplot2)

data(mtcars)
data(iris)

a_plot <- ggplot(mtcars, aes(mpg, disp))+
geom_line()+
facet_wrap(~cyl,nrow=2,scales='free')+
theme_bw()

b_plot<- ggplot(iris, aes(Sepal.Length,Petal.Length))+
geom_line()+
ggtitle('imposter')+
theme_bw()

vp <- viewport(width = 0.52, height = 0.5, x = 0.75, y = 0.25)

png("test.png")
print(a_plot)
print(b_plot, vp = vp)
dev.off()][1]][1]

enter image description here

最佳答案

一个建议是在数据中处理这个问题,让情节分面自然地工作。 (我在这里使用 dplyr 来组合数据集,但这只是为了方便。任何提供一组列来绘制的机制都应该有效。)

library(dplyr)
library(ggplot2)

bind_rows(
transmute(mtcars, x=mpg , y=disp , z=as.character(cyl)),
transmute(iris, x=Sepal.Length, y=Petal.Length, z="imposter")
) %>%
ggplot(aes(x, y)) +
geom_line() +
facet_wrap(~ z, nrow = 2, scales = "free") +
theme_bw()

faceted combo of mtcars and iris

关于r - 将额外的图插入多面网格,ggplot2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48940187/

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