如何指定实际的 x 轴值绘制为 R 中的 x 轴刻度 [英] How to specify the actual x axis values to plot as x axis ticks in R

查看:25
本文介绍了如何指定实际的 x 轴值绘制为 R 中的 x 轴刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 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:

  1. 调用 plot()par(),指定参数 xaxt='n'
  2. 调用 axis() 例如axis(side = 1, at = seq(0, 10, by = 0.1), labels = FALSE, tcl = -0.2)
  1. call plot() or par(), specifying argument xaxt='n'
  2. 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:使用 atseq() 来定义标签:

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:

附注.由于您有大量标签,因此您必须使用其他参数来使文本适合绘图.我使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆