简体   繁体   English

仅绘制y轴,但没有别的

[英]Plot only y-axis but nothing else

I would like to create a plot where only the y-axis (including grids,numbers and label) is displayed. 我想创建一个只显示y轴(包括网格,数字和标签)的图。 But I do not want to display the plot or the x-axes. 但我不想显示图或x轴。

Is this possible to do? 这可能吗?

When creating your plot, you just need to specify a few options. 创建绘图时,只需指定几个选项即可。 In particular, note axes , type and xlab : 特别是,音符axestypexlab

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: 然后,您可以手动添加y轴:

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: 您可以使用geom_blank()和主题调整来关闭不需要的元素:

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 : 或者,如果您已经有一个绘图,则可以使用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)

You can simply use plot.new() . 你可以简单地使用plot.new()

plot.new()
axis(2, seq(0, 1, 0.2))
grid(led = 2)
# etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM