Midpoint for Date axis in ggplot - r

I want to add some labels to a plot on which the x-axis is a Date. I want the label to be centered around the middle. How do I find the midpoint on the x-axis?
Example:
example <- data.frame(time = c("02/26/11", "05/26/10", "05/27/10", "05/28/10",
"05/29/10", "06/27/10", "06/30/10", "10/27/10",
"10/27/10", "12/26/12"),
value = c(5, 1, 7, 8, 11, 20, 14, 1, 20, 12))
example$time <- as.Date(example$time, format = "%m/%d/%Y")
ggplot(example, aes(x = time, y = value)) + geom_point() +
scale_x_date(labels = date_format("%b%Y"),
breaks = "3 month",
minor_breaks = "1 month")
Now, I want to use geom_text to add a text lable that has x coordinate positioned int he middle of the x-axis, and y at the middle of the y-axis.

It sounds like you just want something like
xx<-data.frame(
time=mean(range(example$time)),
value=mean(range(example$value))
)
ggplot(example, aes(x = time, y = value)) + geom_point() +
geom_text(data=xx, label="midtext") +
scale_x_date(labels = date_format("%b%Y"),
breaks = "3 month",
minor_breaks = "1 month")
We find the center of the plot by finding the center of the ranges of each of the axes. Then we use theses values in the call to geom_text.
That will produce this picture.
PS. I also changed your date formatting line to
example$time <- as.Date(example$time, format = "%m/%d/%y")
since you only have two-digit years and not 4 digit years.

Related

time axis settings for histogram in ggplot in R

I am trying to understand how the limit of axis works in ggplot histogram. When I tried to set the limit, the histogram shifted.
Below is a script to generate histogram of kWh for each month. On the first graph (p1) I did not set the axis limit. On the second graph (p2) I set the limit, which is the same as the time span of the dataset.
The histograms are attached. On the first histogram, the first bar is correct, for Jan and the value is above 20000.
The problem is: on the second histogram, the first bar is actually for Feb (see the value is below 20000, which is wrong), but the bar in the histogram says Jan.
My question: what causes this time shift? Furthermore, two bars are missing in the second histogram.
Any help would be appreciated. Thanks
Lines <- "year,month,kWh
2014,1,21535
2014,2,19826
2014,3,20796
2014,4,21249
2014,5,21442
2014,6,22685
2014,7,20569
2014,8,23346
2014,9,23440
2014,10,20148
2014,11,24415
2014,12,21628"
con <- textConnection(Lines)
df = read.csv(con)
df$timestamp = as.POSIXct(paste(df$year,df$month,"01", sep="-"),
format="%Y-%m-%d", tz="GMT")
library(ggplot2)
png(filename = "p1.png", width = 800, height = 400, units = "px", pointsize = 24 )
p1 = ggplot(data = df, aes(x = timestamp, kWh)) +
geom_bar(stat = "identity") +
scale_x_datetime(date_breaks = "1 month",
date_labels = "%m\n%Y")
p1
png(filename = "p2.png", width = 800, height = 400, units = "px", pointsize = 24 )
time.start=as.POSIXct("2014-01-01",format="%Y-%m-%d", tz="GMT")
time.end=as.POSIXct("2014-12-01",format="%Y-%m-%d", tz="GMT")
p2 = ggplot(data = df, aes(x = timestamp, kWh)) +
geom_bar(stat = "identity") +
scale_x_datetime(limits = c(time.start,time.end),
date_breaks = "1 month",
date_labels = "%m\n%Y")
p2

date_minor_breaks in ggplot2

I am a beginner in ggplot2. I am unable to use date_minor_breaks to show quarterly "ticks" on x-axis.
Here's my code:
x<-c(seq(1:12))
time<-c("2010Q1","2010Q2","2010Q3","2010Q4","2011Q1","2011Q2", "2011Q3","2011Q4","2012Q1","2012Q2","2012Q3","2012Q4")
z<-data.frame(type = x,time = time)
z$time = as.yearqtr(z$time)
z$time = as.Date(z$time)
ggplot(data = z, aes(x=time,y=type)) +
geom_point() +
scale_x_date(date_labels = "%Y",date_minor_breaks = "3 months",name = "Year") +
theme_tufte() +
theme(legend.position = "none")
I researched this topic on SO Formatting dates with scale_x_date in ggplot2 and on https://github.com/hadley/ggplot2/issues/542, and found that there were some issues reported on this topic. However, I didn't quite follow the conversation about changes to ggplot2 because it's been only 6 days since I started using ggplot2.
Here's the graph I got (it doesn't have any ticks)...
Here's a sample graph with "tick marks" generated from Excel. Please ignore values because my point of creating this Excel chart is to demonstrate what I am looking for--i.e. "quarterly ticks". I'd appreciate your help.
You may have to make major breaks every three months and then pad your labels with blanks to give the illusion of major (labeled) and minor (unlabeled) ticks. See this answer for another example.
First manually make the breaks for the tick marks at every quarter.
breaks_qtr = seq(from = min(z$time), to = max(z$time), by = "3 months")
Then make the year labels and pad these labels with three blanks after each number.
labels_year = format(seq(from = min(z$time), to = max(z$time), by = "1 year"), "%Y")
labs = c(sapply(labels_year, function(x) {
c(x, rep("", 3))
}))
Now use the breaks and the labels with the labels and breaks arguments in scale_x_date. Notice that I'm not using date_labels and date_breaks for this.
ggplot(data = z, aes(x=time,y=type)) +
geom_point() +
scale_x_date(labels = labs, breaks = breaks_qtr, name = "Year") +
theme_tufte() +
theme(legend.position = "none")
You should also define your (major) date breaks:
ggplot(data = z, aes(x=time, y=type)) +
geom_point() +
scale_x_date(date_breaks = "1 year", name = "Year", date_minor_breaks="3 months",
limits = c(as.Date(as.yearqtr("2009Q4")),
as.Date(as.yearqtr("2013Q2"))),
expand=c(0,0), date_labels = "%Y") +
theme(legend.position = "none")
And some other "fancy" stuff to align the minor ticks with the major ticks (I guess there a better ways to do this, but this works).

How to move the ticks of x axis (date in years) in ggplot2?

I want to check the boxplots for each year for variable a in the df data.frame. I used the code below to create the data.frame
set.seed(123)
date <- as.Date(seq(as.Date("1990-01-01"), as.Date("2015-12-31"), by = 1), format="%Y-%m-%d")
a <- runif(9496, 3000, 120000)
df <- data.frame(date, a)
df[c(1:151,9313:9496), 2]<-NA
and using this code
library(ggplot2)
ggplot(df, aes(x=date, y=a, group=years(date)))+
geom_boxplot()+
scale_x_date(breaks = date_breaks("1 year"),
labels = date_format("%Y"))
I got this figure
The years on x axis are shown before and after the boxplot. How can I align the ticks of x axis and axis.text with the boxplots?
You can set the date breaks to be in the middle of each year:
scale_x_date(breaks = seq(as.Date("1990-06-30"), as.Date("2015-06-30"), by="1 year"),
labels = date_format("%Y"))

ggplot: assign manual break points to date scale

I have some time series data and I would like to customize the x-axis (dates) to show the date labels where I obtain measurements, as opposed to having regular breaks per week/month/year.
Sample data:
dates <- as.Date("2011/01/01") + sample(0:365, 5, replace=F)
number <- sample(1:100, 5)
df <- data.frame(
dates = dates,
number = number
)
This way I can plot my df with regular breaks every month...
ggplot(df, aes(as.Date(dates), number)) +
geom_point(size=6) +
geom_segment(aes(x = dates, y = 0, xend = dates, yend = number),
size=0.5, linetype=2) +
scale_x_date(breaks = date_breaks("1 month"), labels = date_format("%d-%b-%Y")) +
theme(axis.text.x = element_text(angle=90, hjust=1, vjust=0.5))
... but I would like to set the major breaks to the actual 5 dates in df$dates. It works with a normal continuous scale (scale_x_continuous(breaks = c(1, 3, 7, 9))) but I can't figure out how to do it for a continuous date scale.
I am looking to do something like...
scale_dates_continuous(breaks = df$dates)
...but that doesn't exist unfortunately. Thanks lot for your help!
Please read ?scale_x_date, about the breaks argument: you can use a "vector of breaks". Thus, try
scale_x_date(breaks = df$dates, labels = date_format("%d-%b-%Y"))

How do I plot a bar graph in ggplot where my bins are year-months

mydates<-data.frame(effectiveDate=as.Date(c("2012-1-1","2012-1-1","2012-2-1","2012-10-1","2012-4-1","2012-8-1","2013-1-1")))
How do I plot a bar graph using ggplot where the bars are year-months? In this case, there'd be 13 bars: "Jan 2012" through "Jan 2013" and each bar height would represent the count of each date that falls in the month.
I think this works:
Edit: Per the comments below
mydates$months <- month.abb[as.numeric(format(mydates[, 1], format = "%m"))]
mydates$Months <- paste(mydates$months, format(mydates[, 1], format = "%y"))
mydates$Months <- factor(mydates$Months, levels = paste(month.abb, c(rep(12, 12), 13)))
ggplot(mydates, aes(Months)) + geom_bar()+ scale_x_discrete(drop=FALSE) +
theme(axis.text.x = element_text(angle=90, vjust=.3))
The labels could be adjusted by making the bars horizontal with coord_flip() or using theme(axis.text.x = element_text(angle=90, vjust=.3))
This works fine:
ggplot(data = mydates, aes(x = effectiveDate)) + geom_bar()
ggsave(filename = "~/temp/temp.png", width = 6, height = 3, dpi = 150)

Resources