user119144 -4 years ago 136
R Question

# line between two graphics

I need to make the following type chart

I've been trying to use ggplot2, do two graphics, paste them and put the line. However, I can not place the line y = 10 . The y = 10 line must traverse the entire graph, as in the image.

``````    library(ggplot2)
require(gridExtra)

#test value

x=0:1:15
y=0:1:15
z=15:1:0

#join values

ma<-data.frame(x,y)
ma1<-data.frame(z,y)

#define graph

a<-ggplot(data=ma)+geom_line(mapping=aes(x,y))
b<-ggplot(data=ma1)+geom_line(mapping=aes(z,y))

#join graph
c<-grid.arrange(a, b, ncol=2)

#try insert line y=10

c + geom_hline(yintercept = 10)

[![enter image description here][2]][2]

Maybe using facets is possible, anyway All help is welcome
``````

UPDATE: Using the link in the comment, i arrive to this

``````library(ggplot2)
require(gridExtra)
library(gtable)

#test value

x=0:1:15
y=0:1:15
z=15:1:0

#join values

ma<-data.frame(x,y)
ma1<-data.frame(z,y)

#define graph

a<-ggplot(data=ma)+geom_line(color="red",mapping=aes(x,y))+ geom_hline(yintercept = 10,lty=3)
b<-ggplot(data=ma1)+geom_line(mapping=aes(z,y))+ geom_hline(yintercept = 10)

#Convert to gtable
a1<-ggplotGrob(a)
b1<-ggplotGrob(b)

#Work in lines, save line and remove line in before plots

line<-a1\$grobs[[6]]\$children[[4]]
a1\$grobs[[6]]\$children[[4]]<-NULL
b1\$grobs[[6]]\$children[[4]]<-NULL

# Merge gtables
c1<-cbind(a1,b1)

#Print plot
grid.newpage()
grid.draw(c1)
``````

However, I obtained the positions of l z r t testing.
Anyone know how to get those positions more accurately?
Thx everybody

You need to add the horizontal line to the ggplots not to grid.arrange.

``````a <- ggplot(data=ma)+geom_line(mapping=aes(x,y))
b <- ggplot(data=ma1)+geom_line(mapping=aes(z,y))
h <- geom_hline(yintercept = 10)

#join graph
grid.arrange(a+h, b+h, ncol=2)
``````

You can also set the line type to get dashed lines, and set the theme to bw to approximate your desired graph.

``````h2 <- geom_hline(yintercept = 10, lty=5)
bw <- theme_bw()

#join graph
grid.arrange(a+h2+bw, b+h2+bw, ncol=2)
``````

On principle, I would not have horizontal line traverse the plots, but the comment posted above provides an applicable link on how to do this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download