我正在尝试使用ggplot重新创建此基础生成的图,但是我想使用比直接依赖grid::viewport()
的一个demonstrated here更优雅的工作流程。
使用 ggsubplot ,我尝试了:
require(ggplot2)
require(ggsubplot)
d = data.frame(x = sort(rlnorm(300)), y = sort(rlnorm(300)), grp = 1)
ggplot(d, aes(x, y)) + geom_point() + theme_bw() +
scale_x_continuous(limits=c(0, 10)) + scale_y_continuous(limits=c(0, 10)) +
geom_subplot(data=d, aes(x=5, y=1, group = grp, subplot = geom_point(aes(x, y))), width=4, height=4)
产生了以下令人失望的结果:
显然需要做一些工作,但是如果将轴,标签和网格添加到子图中,则距离不远。任何想法如何做到这些?我找不到任何此类示例,并且ggsubplot默认情况下会删除这些元素。提前致谢。
最佳答案
您可能需要使用位置和颜色等,但是添加reference
似乎有效
ggplot(d, aes(x, y)) + geom_point() + theme_bw() +
scale_x_continuous(limits=c(0, 10)) + scale_y_continuous(limits=c(0, 10)) +
geom_subplot(data=d, aes(x=5, y=1, group = grp, subplot = geom_point(aes(x, y))), width=4, height=4,reference=ref_box(fill = "grey90", color = "black"))
作品
关于r - 在ggplot中嵌入子图(ggsubplot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20708012/