如何指定实际的x轴值以绘制为R中的x轴刻度 [英] How to specify the actual x axis values to plot as x axis ticks in R
问题描述
我正在R中创建图,但我不喜欢R绘制x轴值.
I am creating a plot in R and I dont like the x axis values being plotted by R.
例如:
x <- seq(10,200,10)
y <- runif(x)
plot(x,y)
这将在X轴上绘制具有以下值的图形:
This plots a graph with the following values on the X axis:
50, 100, 150, 200
但是,我想将存储在变量x中的20个值10,20, 30 ... 200
绘制为X轴值.我遍历了无数博客和简洁的手册-经过数小时的搜索,我发现以下最有用的信息是最近的(概述)说明:
However, I want to plot the 20 values 10,20, 30 ... 200
stored in variable x, as the X axis values. I have scoured through countless blogs and the terse manual - after hours of searching, the closest I've come to finding anything useful is the following (summarized) instructions:
- 调用
plot()
或par()
,并指定参数xaxt='n'
- 致电
axis()
,例如axis(side = 1, at = seq(0, 10, by = 0.1), labels = FALSE, tcl = -0.2)
- call
plot()
orpar()
, specifying argumentxaxt='n'
- call
axis()
e.g.axis(side = 1, at = seq(0, 10, by = 0.1), labels = FALSE, tcl = -0.2)
我尝试了一下,结果图完全没有x轴值.有可能有人在那里知道该怎么做吗?我不敢相信以前没有人尝试过这样做.
I tried it and the resulting plot had no x axis values at all. Is it possible that someone out there knows how to do this? I can't believe that no one has ever tried to do this before.
推荐答案
您将在?axis
的帮助页面中找到问题的答案.
You'll find the answer to your question in the help page for ?axis
.
以下是帮助页面示例之一,已根据您的数据进行了修改:
Here is one of the help page examples, modified with your data:
选项1:使用xaxp
定义轴标签
Option 1: use xaxp
to define the axis labels
plot(x,y, xaxt="n")
axis(1, xaxp=c(10, 200, 19), las=2)
选项2:使用at
和seq()
定义标签:
Option 2: Use at
and seq()
to define the labels:
plot(x,y, xaxt="n")
axis(1, at = seq(10, 200, by = 10), las=2)
这两个选项都产生相同的图形:
Both these options yield the same graphic:
PS.由于标签数量很多,因此必须使用其他参数来使文本适合绘图.我使用las
旋转标签.
PS. Since you have a large number of labels, you'll have to use additional arguments to get the text to fit in the plot. I use las
to rotate the labels.
这篇关于如何指定实际的x轴值以绘制为R中的x轴刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!