【发布时间】:2012-12-04 23:06:47
【问题描述】:
我不确定 ggplot 中的默认日期间隔是多少。我的数据有五个数据点,包括 2011 年 9 月、2011 年 12 月、2012 年 3 月、2012 年 6 月和 2012 年 9 月。
ggplot 显示与我的数据不同的数据点,我觉得这有点烦人。我错过了什么吗?
您能帮忙显示“2011 年 9 月、2011 年 12 月、2012 年 3 月、2012 年 6 月和 2012 年 9 月
x4.1.m<- structure(list(Var.1=structure(c(1L,2L,3L,4L,5L,6L,1L,2L,3L,4L,5L,6L,1L,2L,3L,4L,5L,6L,1L,2L,3L,4L,5L,6L,1L,2L,3L,4L,5L,6L),.Label=c("I'vechangedforwork/anewjob/goneonaworkplan","IwantaphonethatVodafonedoesn'toffer","IwantBestMates/Favourites","Iwasofferedorsawabetterofferonanothernetwork","Issueswiththe2degreesnetwork(poorcoverage)","Other"),class="factor"),YearQuarter=structure(c(1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,2L,3L,3L,3L,3L,3L,3L,4L,4L,4L,4L,4L,4L,5L,5L,5L,5L,5L,5L),.Label=c("2011-09-01","2011-12-01","2012-03-01","2012-06-01","2012-09-01"),class="factor"),value=c(0.23,0.23,0.121,0.25,0.223,0.14,0.39,0.22,0.05,0.37,0.25,0.2,0.09,0.14,0.05,0.3,0.4,0.12,0.13,0.1,0.26,0.38,0.28,0.15,0.33,0.05,0.06,0.44,0.32,0.43)),.Names=c("Var.1","YearQuarter","value"),row.names=c(NA,-30L),class="data.frame")
library(scales)
library(ggplot2)
###data
x4.1.m$YearQuarter <- as.Date(x4.1.m$YearQuarter)
x4.1.m$label <- paste(round(x4.1.m$value*100,0), "%", sep="")
### plot
x4.line <- ggplot(data=x4.1.m, aes(x=YearQuarter, y=value,colour=Var.1)) +
geom_smooth(se=F, size=1.5)
x4.line <- x4.line + geom_text(aes(label = label),size = 3, hjust = 0.5, vjust =1.5)
### theme
x4.line <- x4.line + theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.background=element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank())
x4.line <- x4.line + ggtitle("Percentages:Main Reasons for Leaving Vodafone by Quarter") +
theme(plot.title = element_text(size=rel(1.2)))+
scale_y_continuous(labels=percent, limits=c(0,0.5)) +
scale_x_date(labels = date_format("%b-%y"),breaks = date_breaks("3 months"))+
labs(y="Percentage",x="Year Quarter")
x4.line
【问题讨论】:
-
我建议您阅读
scale_x_date的文档,这是您似乎已经知道的功能。请注意,它有一个 break 参数。 -
我建议你像@joran 一样投资一点来学习 R 和 ggplot2,我们不是来做你的工作的。我在上一个问题中为你写了这段代码stackoverflow.com/questions/13692739/… 最低限度是理解它!
-
我刚刚切换到 R 2.15.2 和最新版本的 ggplot。我发现它与这些变化混淆了。如果我像你一样聪明,我就不会问这个问题了?感谢您之前的回复。但是,您的图表没有显示我想要的顺序......因此我问了这个问题。想一想你第一次学习 R 的时候。你有多少次遇到小问题?呵呵.....
-
我认为人们试图提出的观点是,在提出 SO 之前,您似乎很少自己研究或解决问题。因此,在这种情况下,这意味着阅读
?scale_x_date并尝试执行breaks 参数下明确描述的内容。如果它仍然不起作用,请询问并解释您尝试了什么。