在轴上显示正确的值

时间:2016-09-04 05:56:39

标签: r plot

我有Data.Framemydf):

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而不试图推断序列?

2 个答案:

答案 0 :(得分:2)

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))

enter image description here

答案 1 :(得分: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()

enter image description here

数据

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))
相关问题