【问题标题】:Changing X-Axis Values in r更改 r 中的 X 轴值
【发布时间】:2017-02-06 22:02:55
【问题描述】:

我想更改 R 中绘图的 x 轴。这是我的示例:

plot(cbind(result, result),xlim=c(max(result),min(result)),
     ylim=c(min(result),max(result)), xaxt="n")
axis(1, at=result)

result
## [1] 0.6256767 0.6833695 0.7671350 0.5205373 0.4932262 0.5852338 0.5088692 0.3379572
## [9] 0.3420370 0.3029084 0.4677624 0.4822537 0.3047485 0.3852572 0.3186014 0.2009436
## [17] 0.1882227 0.2090007 0.2654110 0.3334744

我想在 x 轴上设置从 1 到 20 的新值。我尝试使用 axis(1, at=seq(1, 20)),但它不起作用。我该怎么办?

【问题讨论】:

  • 您在 plot 调用中明确设置了 x 轴的限制。您将它们设置为从result 的最大值到最小值,因此大致从 0.76 开始并下降到 0.18。那么你想为整数 1 到 20 添加标签吗?它们“工作”得很好,它们离你定义的情节边界很远。
  • 如果你想要用 1:20 标记点,无论它们出现什么 x 值,那么你应该使用 at 来指定标签的位置(可能是 sort(result)? ) 和labels = 1:20。有关详细信息,请参阅?axis 文档。
  • 我想要 x 轴而不是值 0.76, ..., 0.20, ..., 使用从 1 到 20 的编号

标签: r plot axis


【解决方案1】:

这是一个对您的示例进行最小更改的解决方案。基本上,我用观察号创建了一个data.frame。由于该图将所有观察结果按降序排列,因此我也对新创建的data.frame 执行此操作。这是我在axis 调用中使用的。

result <-c(0.6256767,0.6833695,0.7671350,0.5205373,0.4932262,0.5852338,0.5088692,0.3379572,
  0.3420370,0.3029084,0.4677624,0.4822537,0.3047485,0.3852572,0.3186014,0.2009436,
  0.1882227,0.2090007,0.2654110,0.3334744)

result_df <-data.frame(my_order=1:length(result),result=result) #add column with initial observation number
result_df <-result_df[order(result_df$result, decreasing = TRUE),] #decreasing order

plot(cbind(result, result),xlim=c(max(result),min(result)),
     ylim=c(min(result),max(result)), xaxt="n")
axis(1, at=result_df$result,labels=result_df$my_order,cex.axis=0.6)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    相关资源
    最近更新 更多