我有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
而不试图推断序列?
答案 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))
答案 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()
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))