Home > database >  How to break x-axis while two y axes on each side with different scales?
How to break x-axis while two y axes on each side with different scales?

Time:02-20

I have below provided an example of the problem that I am trying to solve. As you can see that I have the y-axes names in the middle of the graph. Is there any way to remove those y-axes names? Thanks in advance.

library(ggbreak) 
library(patchwork)
library(ggplot2)
x = c(0,0.1,0.5, 2, 3, 5, 7, 10, 100)
y1 = c(892, 1059, 1004, 1020, 1097,  903,  881,1096,  910)
y2 = c(2070, 2183, 2381, 2406, 2675, 2639, 2662, 2523, 2453)

data = data.frame(x=x, y1 = y1, y2 = y2)
ggplot(data=data,aes(x = x ,y=y1)) 
  geom_point(aes(y=y1), color = "black") 
  geom_point(data = data,aes(x=x,y=y2),colour="blue") 
  scale_y_continuous("Y1", sec.axis = sec_axis(y2~ .*(5)  , name = "Y2")) 
  xlab("x")  
  ylab("") 
  expand_limits(x = 0, y = 0) 
  scale_x_break(c(12, 90))

enter image description here

CodePudding user response:

Try:

 theme(axis.title.y = element_blank())

CodePudding user response:

Instead of using ggbreak one option would be to use just ggplot2 and patchwork which gives you "full" control to style the axes, to add and remove axis labels, ... :

library(patchwork)
library(ggplot2)

p <- ggplot(data = data, aes(x = x, y = y1))  
  geom_point(aes(y = y1), color = "black")  
  geom_point(data = data, aes(x = x, y = y2), colour = "blue")  
  labs(x = "x", y = NULL)  
  expand_limits(x = 0, y = 0)

p1 <- p   
  scale_y_continuous("Y1", sec.axis = sec_axis(y2 ~ . * (5), name = NULL))  
  coord_cartesian(xlim = c(NA, 12))

p2 <- p   
  scale_y_continuous(name = NULL, sec.axis = sec_axis(y2 ~ . * (5), name = "Y2"))  
  coord_cartesian(xlim = c(90, NA))  
  theme(axis.text.y = element_blank(), axis.ticks.y = element_blank())

p1   p2

  • Related