coolsv
coolsv

Reputation: 781

Remove blank plot in ggplot2

Another challenge I've dealing with, but no success... I have the following code:

x<-c(seq(1,10,0.5),seq(50,60,0.5))
y<-runif(40)
df<-data.frame(x,y)
df$gender<-c(rep(0,19),rep(1,21))
r<-c(runif(50,0,10),runif(20,50,60))
df_rug<-data.frame(r)
library(ggplot2)
ggplot(df,aes(x,y))+geom_line()+
  geom_rug(data = df_rug, aes(x = r, y = Inf), color = 'red', sides = 'b')+
  facet_grid(.~gender,scales = "free")

which produces the following plot:

enter image description here

I'm trying to remove the blank parts on the left panel (from x=10) and on the right panel (under x=50). I introduced the option scales=("free") that works perfectly without the geom_rug() option, but I need to keep the red ticks of geom_rug(). How can I cut the blank parts of each panel? I tried to explore the element.blank() option, with no success...

Upvotes: 1

Views: 735

Answers (1)

dc37
dc37

Reputation: 16178

In ggplot2, it's really hard to make discontinuous axis.

As, I understand your question, it seems that you have some rug values for gender = 0 and for gender = 1. If you add an extra column gender to your df_rug, you will be able to separate rug in function of gender as you are doing for df by using facet_grid:

df_rug<-data.frame(r, gender = c(rep(0,50),rep(1,20)))

library(ggplot2)
ggplot(df,aes(x,y))+geom_line()+
  geom_rug(data = df_rug, aes(x = r, y = Inf), color = 'red', sides = 'b')+
  facet_grid(.~gender,scales = "free")

enter image description here

Does it answer your question ?

Upvotes: 4

Related Questions