How to display data in Sep-12 format in a line cha

2019-07-22 01:43发布

问题:

I am struggling to get the date format right. The data is already in the melt() format. There are four variables in the data which happened to share the same data. I just like to plot a simple line chart with four lines(each variable as an indidividual line) and to display Sep-12 as the latest data point? I am using the older ggplot. Feel free to I have two questions.

First question: How to display the data quarterly (the date intervals are Sep-11, Dec-11, Mar-12, Jun-12 and Sep-12)?

Second question: How to suppress the grid lines and the grey background?

  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:

Unfortunatly, the question is not clear , I can't be sur for the answer. but the final result seems good.

I change your code because I ma using last version of ggplot2. I don't find a problem with date format ,but you are a little bit confusing with ggplot2.

Notice I l added scales package for percent formatting.

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



标签: r date ggplot2