r - Multiple x-axis labels for time-series data -
i able plot time-series data using ggplot2
. however, want highlight seasonal information alongwith time-series data.
here's code:
library(zoo) library(ggplot2) <- read.table(text = " season quarter sales season1 2014q1 20 season1 2014q2 40 season1 2014q3 60 season1 2014q4 80 season2 2015q1 30 season2 2015q2 40 season2 2015q3 80 season3 2015q4 90 season3 2016q1 100 season3 2016q2 120 season3 2016q3 140 ", header = true, sep = "") a$quarter<-as.yearqtr(a$quarter) a$quarter<-as.date(a$quarter) ggplot(data=a,aes(x=quarter, y=sales)) + geom_line()
this works in able draw time-series data.
now, want label constitutes season 1, 2 etc. 1 way use color
or linetype
. however, doesn't seem work because breaks continuity of time-series.
# doesn't work... ggplot(data=a,aes(x=quarter, y=sales)) + geom_line(aes(linetype=season))
on other hand, how excel plots graph in just 2 clicks. creates beautiful graph shows seasonal information on x-axis along dates. creates 3-layered x-axis.
i have 2 questions on topic:
question 1: using ggplot
, how can use linetype
(or color
) in ggplot
create continuous graph (i.e. without breaks)? i'd prefer linetype
on color
. as example , answer comment: here's graph created using different set of data.
df <- data.frame(x = 1:3, y = 1:3, z = c(1,3,5)) ggplot(df, aes(x, y, color = factor(z))) + geom_line(aes(group = 1))
i unable replicate above behavior time-series data. here's graph got above code:
question 2: using ggplot
, how can create multi-level x-axis (similar excel did me) shows seasonal information dates? {please see excel graph created.} not want create legend using option. want clarify i'd appreciate if don't use hacking methods applying annotate
(or possibly geom_text
) methods put these multi-level labels adjusting , re-adjusting x- , y- values fit them. because defeats purpose of using programming language draw graph, , won't work data change.
if have questions, please let me know. i'd appreciate thoughts. absolute beginner ggplot2
. it's been 5 days since have transitioned excel , stata ggplot
. so, apologize if question basic.
i researched topic on , couldn't close enough. instance, this thread talks changing ticks, not looking for.
you can quite recreate intent of excel plot this:
library(plyr) ss <- ddply(a, .(season), summarize, date = min(quarter)) ss$date <- as.numeric(ss$date) ggplot(data=a,aes(x=quarter,y=sales)) + geom_line() + geom_vline(data = ss, aes(xintercept = date), colour = "grey50") + geom_text(data = ss, aes(x = as.date(date), y = inf, label = season), hjust = -0.1, vjust = 1.1)
one workaround break in line when using colours plot continuous grey line in addition colour lines:
ggplot(data=a,aes(x=quarter,y=sales)) + geom_line(colour = "grey80") + geom_line(aes(colour = season)) + geom_vline(data = ss, aes(xintercept = date), colour = "grey50") + geom_text(data = ss, aes(x = as.date(date), y = inf, label = season), hjust = -0.1, vjust = 1.1)
Comments
Post a Comment