I'm working on creating some heat maps in ggplot2
using geom_tile()
. The data is of peak discharge measurements in two rivers. The x-axis is the day of the year, the y-axis is the year, and the colour is the discharge value:
Notice that in River 1, the data ranges from 1957 - 2019, while in River 2, the data ranges from 1964 - 2019. That makes the y-axis in the River 1 heat_map more compressed. Is there a way to stretch the River 1 plot and have the two y-axes aligned? That would make the River 1 heat map extend lower in the overall figure. Currently, they are displayed using ggarrange()
The data is in two csvs with one column for Date and one column for Discharge.
Here is my code:
River1 <- read.csv("~/Desktop/River1.csv")
River2 <- read.csv("~/Desktop/River2.csv")
###format dates
River1_1 <- River1 %>%
mutate(date = as_date(parse_date_time(Date,"ymd")),
water_year = year(date),
water_month = month(date),
water_yday = yday(date))
River2_1 <- River2 %>%
mutate(date = as_date(parse_date_time(Date,"ymd")),
water_year = year(date),
water_month = month(date),
water_yday = yday(date))
River1_2 <- River1_1 %>%
group_by(water_year)
River1_2$groups <- cut(River1_2$Discharge, breaks=c(0,0.1,0.2,0.5,1,2,5,10,20,50),
labels=c("0-0.1", "0.1-0.2", "0.2-0.5", "0.5-1", "1-2", "2-5", "5-10", "10-20", ">20"))
River2_2 <- River2_1 %>%
group_by(water_year)
River2_2$groups <- cut(River2_2$Discharge, breaks=c(0,0.1,0.2,0.5,1,2,5,10,20,50))
River1plot <- ggplot(River1_2, aes(x=water_yday, y=water_year, fill=groups)) +
geom_tile(aes(height=1)) +
labs(title="River 1", x="Month", y="Year") +
annotate("text", x=182, y=2002,
label="8 YEAR DATA GAP", size=3.5) +
scale_fill_manual(breaks=rev(levels(River1_2$groups)),
values=c("red", "#4B0055", "#3C3777", "#006290", "#008A98", "#00AC8E", "#25C771", "#A6DA42", "#FDE333")) +
scale_y_continuous(breaks=seq(1957, 2020, by=5), expand=c(0,0)) +
scale_x_continuous(breaks=c(1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335),
labels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"),
expand=c(0,0)) +
guides(fill=guide_legend(title=expression(paste("Discharge (",m^3,"/s)"))))+
theme_bw(base_size=12) +
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank())
River2plot <- ggplot(River2_2, aes(x=water_yday, y=water_year, fill=groups)) +
geom_tile() +
labs(title="River 2", x="Month", y="Year") +
scale_fill_manual(breaks=rev(levels(LR2$groups)),
values=c("red", "#4B0055", "#3C3777", "#006290", "#008A98", "#00AC8E", "#25C771", "#A6DA42", "#FDE333")) +
scale_y_continuous(breaks=seq(1957, 2020, by=5), expand=c(0,0)) +
scale_x_continuous(breaks=c(1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335),
labels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"),
expand=c(0,0)) +
guides(fill=guide_legend(title=expression(paste("Discharge (",m^3,"/s)"))))+
theme_bw(base_size=12) +
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank())
ggarrange(River1plot, River2plot, ncol=2, common.legend=TRUE, legend="right")
I guess you have to provide the
limits = c(1957,2020)
argument to the scale_y_continuous() function. As of now, ggplot only knows the axis breaks, but not how much of it it should show.