Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot only y-axis but nothing else

Tags:

r

ggplot2

I would like to create a plot where only the y-axis (including grids,numbers and label) is displayed. But I do not want to display the plot or the x-axes.

Is this possible to do?

like image 894
user1723765 Avatar asked Dec 16 '22 01:12

user1723765


2 Answers

When creating your plot, you just need to specify a few options. In particular, note axes, type and xlab:

plot(runif(10), runif(10), 
     xlim=c(0, 1), ylim=c(0,1), 
     axes=FALSE, #Don't plot the axis 
     type="n",  #hide the points
     ylab="", xlab="") #No axis labels

You can then manually add the y-axis:

axis(2, seq(0, 1, 0.2))

and add an grid if you desire

grid(lwd=2)
like image 96
csgillespie Avatar answered Dec 28 '22 22:12

csgillespie


You can use geom_blank() and theme adjustment to switch off unwanted elements:

p <- ggplot(mtcars, aes(disp, mpg)) + geom_blank()

p + theme(axis.line.x=element_blank(),
          axis.text.x=element_blank(),
          axis.ticks.x=element_blank(),
          axis.title.x=element_blank(),
          panel.grid.minor.x=element_blank(),
          panel.grid.major.x=element_blank())

Alternatively, if you already have a plot, you can extract the axis part with gtable:

library(gtable)
g <- ggplotGrob(p)
s <- gtable_filter(g, 'axis-l|ylab', trim=F)  # use trim depending on need
grid.newpage()
grid.draw(s)
like image 23
Rosen Matev Avatar answered Dec 28 '22 23:12

Rosen Matev