Reputation: 6399
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?
Upvotes: 5
Views: 15221
Reputation: 8611
You can simply use plot.new()
.
plot.new()
axis(2, seq(0, 1, 0.2))
grid(led = 2)
# etc
Upvotes: 1
Reputation: 1878
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)
Upvotes: 3
Reputation: 60462
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)
Upvotes: 9