如何在折线图中以Sep-12格式显示数据并抑制网格线和灰色背景?

时间:2012-12-03 22:15:43

标签: r date ggplot2

我正在努力使日期格式正确。数据已经采用melt()格式。数据中有四个变量共享相同的数据。我只想绘制一条带有四条线的简单折线图(每个变量作为一条不同的线)并将Sep-12显示为最新的数据点?我正在使用较旧的ggplot。请随意我有两个问题。

第一个问题:如何每季度显示数据(日期间隔为9月11日,12月11日, 3月12日,6月12日和9月12日)?

第二个问题:如何抑制网格线和灰色背景?

  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","Iwantaphonethat2degreesdoesn'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")

  x4.1.m$YearQuarter <- format(as.Date(x4.1.m$YearQuarter),"%b-%y")

  x4.line <- ggplot(data=x4.1.m, aes(x=factor(YearQuarter), y=value,colour=Var.1)) + 
         geom_smooth(se=F, size=1.5)+labs(y="Percentage",x="Year Quarter")

  x4.line+geom_text(aes(label =paste(round(value*100,0), "%", sep=""),group=Var.1),
                    size = 3, hjust = 0.5, vjust =1.5) +

  opts(axis.line = theme_segment(colour = "black"),
            panel.grid.major = theme_blank(),
            panel.background=theme_blank(),
                panel.grid.minor = theme_blank(),
                panel.border = theme_blank()) +
        scale_y_continuous("Percentage",labels=percent, limits=c(0,0.5)) +
        ggtitle("Percentages:Main Reasons for Leaving 2degrees by Quarter") +
        theme(plot.title = element_text(size=rel(1.2)))

1 个答案:

答案 0 :(得分:1)

不幸的是,问题不明确,我无法回答问题。但最终的结果似乎很好。

我更改了您的代码,因为我使用的是ggplot2的最新版本。    我没有发现日期格式的问题,但你对ggplot2有点困惑。

注意我添加了 scale 包以进行格式化百分比。

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,group = Var.1))
x4.line <- x4.line + 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 2degrees 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"))+
  labs(y="Percentage",x="Year Quarter")

x4.line

enter image description here