R:绘图时 x 轴上的字符而不是级别 #? [英] R: Character instead of level # on x axis when plotting?

查看:64
本文介绍了R:绘图时 x 轴上的字符而不是级别 #?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助在 x 轴上绘制月份名称,而不是指定月份的级别.我正在处理一个水年",所以 10 月被指定为 1 级,9 月结束时为 12 级.我相信这很容易,只是我不经常使用因子.谢谢!

I need help plotting month names on the x axis, instead of the level the months are assigned. I am working with a "water year", so October is assigned to be level 1, and ending with September being level 12. I'm sure this is easy, I just don't often work with factors. Thanks!

研究完成:根据 R 中的数字向量绘制字符向量

R 绘图,x 轴上的日期

这是我的数据的简化示例

Here is a simplified example of my data

    Months<-c("Jan"=4,"Feb"=5,"Mar"=6,"Apr"=7,"May"=8,"Jun"=9,"Jul"=10,
    "Aug"=11,"Sep"=12,"Oct"=1,"Nov"=2,"Dec"=3)

    Data<-c(1,2,3,4,5,6,7,8,9,10,11,12)

    df<-data.frame(Months,Data)

  >df
      Months Data
  Jan      4    1
  Feb      5    2
  Mar      6    3
  Apr      7    4
  May      8    5
  Jun      9    6
  Jul     10    7
  Aug     11    8
  Sep     12    9
  Oct      1   10
  Nov      2   11
  Dec      3   12

    plot(Data~factor(Months), df,las=2)

这会将具有正确月份的数据放在正确的位置,只是错误的标签.

This puts the data in the correct place with the correct month, just the wrong labels.

推荐答案

我们可以在 plot 中使用 xaxt = "n" 然后使用 axiscode> 更改 xaxis 刻度标签

We can use xaxt = "n" in plot and then with axis change the xaxis tick labels

plot(Data ~ Months, transform(df, Months = match(row.names(df), 
                       month.abb)), las = 2, xaxt = "n", type = "b", col = "blue")
axis(1, at = seq_len(nrow(df)), row.names(df))

这篇关于R:绘图时 x 轴上的字符而不是级别 #?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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