【问题标题】:Show right values on axis在轴上显示正确的值
【发布时间】:2016-09-04 05:56:39
【问题描述】:

我有这个Data.Frame (mydf):

year   total
1999   3967
2002   580
2005   5203
2008   2406

为了显示它,我只是运行:

plot(mydf)

但是我可以在 x 轴 (year) 上看到标签:

2000 2002 2004 2006 2008

如何告诉plot/axis 只显示四个预期值:1999,2002,2005,2008,而不尝试推断顺序?

【问题讨论】:

  • 使用xaxt="n",然后使用axis,您可以更改标签

标签: r plot


【解决方案1】:

正如akrun 建议的那样,您可以使用:

plot(mydf, xaxt = "n")
axis(1, at = mydf$year)

数据:

mydf <- structure(list(year = c(1999L, 2002L, 2005L, 2008L), total = c(3967L, 
580L, 5203L, 2406L)), .Names = c("year", "total"), class = "data.frame",
row.names = c(NA, -4L))

【讨论】:

    【解决方案2】:

    如果我们用ggplot,也可以试试

    library(ggplot2)
    library(dplyr)
    mydf %>% 
        mutate(year = as.character(year)) %>% 
        ggplot(., aes(x=year, y = total)) + 
                 geom_point() + 
                 scale_x_discrete(labels = mydf$year) +
                 theme_bw()
    

    数据

    mydf <- structure(list(year = c(1999L, 2002L, 2005L, 2008L), total = c(3967L, 
    580L, 5203L, 2406L)), .Names = c("year", "total"), class = "data.frame", row.names = c(NA, 
    -4L))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 2022-07-06
      • 2017-12-19
      相关资源
      最近更新 更多