scale_x_date和scale_x_datetime中的ggplot2差异 [英] ggplot2 differences in scale_x_date and scale_x_datetime
问题描述
其中一个图形有一个scale_x_date轴,另一个是scale_x_datetime轴。
尽管赋予这些函数相同的参数,但所得到的轴是不同的。我不能弄清楚如何使它们相同。
可以下载2个数据集soil_N_summary.csv和weather_data.csv
当雨量曲线从07/07开始时,您可以看到铵土标签从05/05开始。降雨图上的x轴也似乎从较早的日期开始。
任何人都可以帮我让这些轴相同吗?
谢谢!
> sessionInfo(package =ggplot2)
R版本3.3.1(2016-06-21)
平台:x86_64-w64-mingw32 / x64(64位)
运行于: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
附加基本包:
字符(0)
其他附加包:
[1] ggplot2_2.1.0
通过命名空间加载(并未附加):
[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 labels_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
保持缩放/数据类型相同。你也有一个错字在你的一个限制。我在第二张图上添加了y轴的手动标签,只是为了显示所有内容)
library(ggplot2)
库(dplyr)
库(readr)
库(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,color = Treatment))+
geom_line()+ geom_point()+
geom_errorbar(aes(ymin = NH4_N_mean-NH4_N_SEM,ymax = NH4_N_mean + NH4_N_SEM))+
ggtitle(Soil铵)+ ylab(Soil NH4-N mg / kg)+ xlab(Date)+
scale_x_datetime expand = c(0,0),
date_breaks =14天,
date_minor_breaks =7天,
date_labels =%d /%m,
= as.POSIXct(c(grill-01 00:00:00, (b)
,
### rainfall plot
ggplot(weather_data%>%filter(datetime> \"-01-01 00:00:00),aes(datetime,Rainfall_mm))+
geom_step(direction =vh)+
scale_x_datetime(expand = c(0,0)
date_breaks =14天,
date_minor_breaks =7天,
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
)
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
Keep the scale/data types the same. You also had a typo in one of your limits. I added the manual labels for the y axis on the second plot just to show everything lines up).
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
)
这篇关于scale_x_date和scale_x_datetime中的ggplot2差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!