在r中更改X轴值

时间:2017-02-06 22:02:55

标签: r plot axis

我想改变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

enter image description here

我希望在x轴上有1到20的新值。我尝试使用axis(1, at=seq(1, 20))但它没有用。我该怎么做?

1 个答案:

答案 0 :(得分:0)

这是一个对您的示例进行最少更改的解决方案。基本上,我使用观察编号创建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)

enter image description here