I am trying to produce 2 graphs from different data sets in ggplot. I want the graphs to have the same x axis breaks and labels. One of the graphs has a scale_x_date axis and the other a scale_x_datetime axis.
Despite giving these functions the same arguments, the resulting axis are different. I cant figure out how to make them the same.
The 2 datasets "soil_N_summary.csv" and "weather_data.csv" can be downloaded here.
I have used the following code to produce the graphs shown below:
library(ggplot2)
library(dplyr)
### import data
soil_N_summary <- read.csv("soil_N_summary.csv", stringsAsFactors = FALSE)
weather_data <- read.csv("weather_data.csv", stringsAsFactors = FALSE)
### change to POSIXct and Date class
soil_N_summary <- soil_N_summary %>% mutate(Treatment = as.factor(Treatment),
Date = as.Date(Date))
weather_data <- weather_data %>% mutate(datetime = as.POSIXct(datetime, format = "%Y-%m-%d %H:%M:%S"))
### ammonium plot
ggplot(soil_N_summary, aes(Date, NH4_N_mean, fill = Treatment, colour = Treatment))+
geom_line() +geom_point() +
geom_errorbar(aes(ymin = NH4_N_mean-NH4_N_SEM, ymax = NH4_N_mean+NH4_N_SEM))+
ggtitle("Soil ammonium") + ylab("Soil NH4-N mg/kg") + xlab("Date") +
scale_x_date(date_breaks= "14 days", date_minor_breaks = "7 days", date_labels = "%d/%m",
limits = as.Date(c("2016-05-1", "2016-09-16"))) +
theme(legend.position = c(0.9,0.9))
### rainfall plot
ggplot(weather_data %>% filter(datetime > "2016-05-01 00:00:00"), aes(datetime, Rainfall_mm)) +
geom_step(direction = "vh") +
scale_x_datetime(date_breaks= "14 days", date_minor_breaks = "7 days",
date_labels = "%d/%m", limits = as.POSIXct(c("2016-05-01 00:00:00", "2016-09-16 00:00:00"))) +
xlab("Date") + ylab("Hourly rainfall (mm)")
As you can see the ammonium plot labels start at "05/05" when the rainfall plot starts at "07/07". There also x-axis on the rainfall plot also appears to start at an earlier date.
Can anyone help me to get these axis identical?
Thanks!
> sessionInfo(package = "ggplot2")
R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252
attached base packages:
character(0)
other attached packages:
[1] ggplot2_2.1.0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.5 XLConnectJars_0.2-12 grDevices_3.3.1
[4] tidyr_0.5.1 digest_0.6.9 dplyr_0.5.0
[7] assertthat_0.1 grid_3.3.1 plyr_1.8.4
[10] R6_2.1.2 gtable_0.2.0 DBI_0.4-1
[13] XLConnect_0.2-12 magrittr_1.5 datasets_3.3.1
[16] scales_0.4.0 utils_3.3.1 lazyeval_0.2.0
[19] graphics_3.3.1 labeling_0.3 base_3.3.1
[22] tools_3.3.1 munsell_0.4.3 colorspace_1.2-6
[25] stats_3.3.1 rJava_0.9-8 methods_3.3.1
[28] gridExtra_2.2.1 tibble_1.0
data1
? Also, when I run your code but changedata1
toweather_data
I get axis breaks that start at 08/05 rather than 07/05. – eipi10weather_data
. I still get axis breaks starting at 07/05 – Rory Shaw