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?
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With