Как построить линию, охватывающую эти два графика? - PullRequest
0 голосов
/ 04 мая 2018

Рассмотрим следующий рисунок:

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 (или некоторого аналога) для изменения рисунка.

1 Ответ

0 голосов
/ 05 мая 2018

Не совсем решение, а обходной путь.

  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

...