ggplot2在scale_x_date和scale_x_datetime中的差异

时间:2016-09-20 16:43:23

标签: r date datetime ggplot2 axis

我正在尝试从ggplot中的不同数据集生成2个图表。我希望图表具有相同的x轴断点和标签。 其中一个图表有一个scale_x_date轴,另一个图表有一个scale_x_datetime轴。

尽管给这些函数赋予相同的参数,但结果轴是不同的。我无法弄清楚如何使它们变得一样。

可以下载2个数据集“soil_N_summary.csv”和“weather_data.csv”here

我使用以下代码生成如下所示的图表:

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)")

Ammonium plot

Rainfall plot

正如您所见,当降雨量图从“07/07”开始时,铵图标签从“05/05”开始。降雨量图上的x轴似乎也在较早的日期开始。

任何人都可以帮助我让这些轴相同吗?

谢谢!

> 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     

3 个答案:

答案 0 :(得分:3)

保持比例/数据类型相同。你的一个限制中也有一个拼写错误。我在第二个图上添加了y轴的手动标签,只是为了显示所有行。)

library(ggplot2)
library(dplyr)
library(readr)
library(gridExtra)

soil_N_summary <- read_csv("so/soil_N_summary.csv")
weather_data <- read_csv("so/weather_data.csv")

soil_N_summary <- mutate(soil_N_summary, Date=as.POSIXct(Date))

grid.arrange(

  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_datetime(expand=c(0,0),
                     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"))) +
    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(expand=c(0,0),
                     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"))) +
    scale_y_continuous(label=c("000", "002", "004", "006", "008")) +
    xlab("Date") + ylab("Hourly rainfall (mm)")

  ,

  ncol=1

)

enter image description here

答案 1 :(得分:0)

这个怎么样?

weather_data$date <- as.Date(as.character(weather_data$datetime))
### rainfall plot
ggplot(weather_data %>% filter(date > "2016-05-01"), aes(date, Rainfall_mm)) +
  geom_step(direction = "vh") +
  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"))) +
  xlab("Date") + ylab("Hourly rainfall (mm)")

enter image description here

答案 2 :(得分:0)

另一种选择是在第二个图中设置你想要的精确断点,使它们与第一个匹配,而不是设置date_breaks= "14 days"并让ggplot决定从哪里开始(当然,你可以在两个情节)。例如:

date_range=as.Date(c("2016-05-01", "2016-09-16"))

ggplot(weather_data %>% filter(datetime > "2016-05-01 00:00:00"), aes(datetime, Rainfall_mm)) +
  geom_step(direction = "vh") +
  scale_x_datetime(breaks=seq(as.POSIXct("2016-05-05"), as.POSIXct(date_range[2]), 
                              by="14 days"), 
                   minor_breaks=seq(as.POSIXct("2016-05-05"), as.POSIXct(date_range[2]), 
                                    by="7 days"), 
                   date_labels = "%d/%m", limits = as.POSIXct(date_range)) +
  xlab("Date") + ylab("Hourly rainfall (mm)")