如何绘制跨越这两个图的标志?

问题描述 投票:0回答:1

考虑下图:

mainplot = ggplot(mtcars, aes(y=mpg,x=wt)) + geom_point() + theme_classic(15) + ylim(c(5,40)) + geom_hline(yintercept=c(15,25), color="red")

gg = ggplot(data.frame(mpg=0), aes(x=mpg))
f = function(mpg,center) {exp(-(mpg - center)^2/(20))}
f15 = function(mpg) {f(mpg,15)}
f25 = function(mpg) {f(mpg,25)}

sideplot = gg + stat_function(fun = f15, linetype="dashed") + stat_function(fun = f25, linetype="dashed") + theme_classic(15) + scale_x_continuous(name=NULL,limits=c(5,40)) + coord_flip() + ylab("f") + theme(axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank()) + geom_vline(xintercept=c(15,25), color="red")

multiplot(mainplot, sideplot, layout=matrix(c(1,1,1,2),nrow=1))

enter image description here

由于该图由两个独立的图形组成,红色水平线被中断。有什么方法可以让它成为一条连续的线?

最简单的解决方案可能是使用Adobe Illustrator(或某些等效的)来修改图形。

r plot ggplot2 graph graphics
1个回答
2
投票

不是一个解决方案,而是一个解决方案。

  1. 减少边距以将两个图形粘在一起
  2. 让你的线条成虚线
  3. 删除侧图的y轴线

mainplot = ggplot(mtcars, aes(y=mpg,x=wt)) + geom_point() + theme_classic(15) + ylim(c(5,40)) + geom_hline(yintercept=c(15,25), color="red", linetype="dashed") + theme(plot.margin = unit(c(1,0,1,1), "cm"))

sideplot = gg + stat_function(fun = f15, linetype="dashed") + stat_function(fun = f25, linetype="dashed") + theme_classic(15) + scale_x_continuous(name=NULL,limits=c(5,40)) + coord_flip() + ylab("f") + theme(axis.line.y=element_blank(),axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank()) + geom_vline(xintercept=c(15,25), color="red", linetype="dashed") + theme(plot.margin = unit(c(1,1,1,0), "cm"))

multiplot(mainplot, sideplot, layout=matrix(c(1,1,1,2),nrow=1))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.