微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

隐藏基 R 图的轴的最小和最大限制的刻度线和刻度标签

如何解决隐藏基 R 图的轴的最小和最大限制的刻度线和刻度标签

假设我使用以下代码行制作条形图和散点图:

x <- c(1,2,3)
xlabs <- c("A","B","C")
y <- c(4,5,6)

barplot(x,xlab = "X",names.arg = xlabs,ylim = c(0,3),ylab = "Y")

plot(x,y,xlim = c(1,ylim = c(4,6),ylab = "Y")

如何隐藏轴刻度线和刻度线标签仅用于最小和最大限制?这是我打算完成的:

enter image description here

enter image description here

解决方法

有几个选项。首先,您通常希望使用 xaxt='n'yaxt='n' 删除默认轴标签。接下来,您可以手动指定 axis() 中刻度线的位置或使用 pretty() 函数删除第一个和最后一个值(在示例中使用 headtail)获得一些不错的刻度线位置。它可能需要一些手动微调。

x <- c(1,2,3)
xlabs <- c("A","B","C")
y <- c(4,5,6)

barplot(x,xlab = "X",names.arg = xlabs,ylim = c(0,3),ylab = "Y",yaxt='n' # drops the y axis and tick marks
        )
# draw a vertical line that will cover the whole vertical axis,if desired
abline(v=par('usr')[1],lwd=2)
# pretty will give nice tickmark locations,and head and tail drop first and last
# add the 0 to x vector because barplot starts at 0
axis(side=2,tail(pretty(c(0,x)),-1)) 

plot(x,y,xlim = c(1,ylim = c(4,6),xaxt='n',# no default x axis with ticks
     yaxt='n' # no default y axis with ticks
     )
# add tickmark and axis locations manually for each axis
# using the pretty function for where the tick marks normally would be,then head and tail to drop first and last
axis(side=1,tail(head(pretty(c(x)),-1),-1)) 
axis(side=2,tail(head(pretty(c(y)),-1))

scatterplot

barplot

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?