gpt4 book ai didi

r - ggplot2 : multiple factors boxplot with scale_x_date axis in R

转载 作者:行者123 更新时间:2023-12-01 02:21:36 24 4
gpt4 key购买 nike

我想用 ggplot2 创建一个多变量 boxplot 时间序列,我需要一个 x 轴和日期的 boxplot 位置函数。

我为此创建了一个具有因子组合的交互矩阵 Treatment x Date这是针对 NDVI 和不同试验组绘制的:

在这里你可以找到一些最少的数据:

dat<-"Treatment  Trial.group    Date    NDVI
HighN A 14/06/2013 0.27522123
HighN A 14/06/2013 0.259781926
HighN A 14/06/2013 0.175982276
LowN A 14/06/2013 0.193604644
LowN A 14/06/2013 0.261191793
LowN A 14/06/2013 0.273672853
HighN B 14/06/2013 0.192144884
HighN B 14/06/2013 0.283013594
HighN B 14/06/2013 0.230556973
LowN B 14/06/2013 0.233952974
LowN B 14/06/2013 0.261718465
LowN B 14/06/2013 0.216450145
HighN A 22/06/2013 0.37522123
HighN A 22/06/2013 0.359781926
HighN A 22/06/2013 0.275982276
LowN A 22/06/2013 0.293604644
LowN A 22/06/2013 0.361191793
LowN A 22/06/2013 0.373672853
HighN B 22/06/2013 0.292144884
HighN B 22/06/2013 0.383013594
HighN B 22/06/2013 0.330556973
LowN B 22/06/2013 0.333952974
LowN B 22/06/2013 0.361718465
LowN B 22/06/2013 0.316450145
HighN A 24/06/2013 0.47522123
HighN A 24/06/2013 0.459781926
HighN A 24/06/2013 0.375982276
LowN A 24/06/2013 0.393604644
LowN A 24/06/2013 0.461191793
LowN A 24/06/2013 0.473672853
HighN B 24/06/2013 0.392144884
HighN B 24/06/2013 0.483013594
HighN B 24/06/2013 0.430556973
LowN B 24/06/2013 0.433952974
LowN B 24/06/2013 0.461718465
LowN B 24/06/2013 0.416450145"

这是导入和创建绘图的代码:
NDVI_ts <- read.table(text=dat, header = TRUE)
library(ggplot2)
library(scales)
interact<-interaction(NDVI_ts$Treatment, NDVI_ts$Date, sep=" : ")
ggplot(data=NDVI_ts, aes(x=interact, y=NDVI)) +
geom_boxplot(aes(fill = Trial.group), width = 0.6) +
theme_bw() + theme(axis.text.x = element_text(angle = 90, hjust = 1))

这段代码给了我下面的箱线图,很好,但 x 轴没有链接到日期: (NDVI ~ Treatment + Date + Trial.group)
enter image description here

我知道我通常可以用这样的方法做到这一点:
q + scale_x_date(breaks="1 week", labels=date_format("%d-%b"))

interact matrix 是一个因素,不能定义为时间对象,所以这是行不通的。我有以下错误:

Error: Invalid input: date_trans works with objects of class Date only



我怎么能有由日期定义的多变量箱线图位置?

NDVI_ts$Date 已在 R 中定义为日期对象。

最佳答案

使用 interaction 创建 x 轴在“治疗”和“日期”之间可以方便地排列分组变量的不同值的框。但是,正如您所注意到的,当原始 Date轴被转换为“复合”因子,控制轴的外观要困难得多。

这是将 x 轴保持在 Date 中的替代方法格式。通过创建两个不同的调色板来区分“治疗”的两个级别。 “治疗”中的组由不同深浅的颜色分隔。使用 group 对框进行分组争论。

library(ggplot2)
library(scales)
library(RColorBrewer)

# convert Date to class 'Date'
NDVI_ts$date <- as.Date(NDVI_ts$Date, format = "%d/%m/%Y")

# A possible way to create suitable colours for the boxes
# create one palette of colours for each level of Treatment
# e.g. blue colour for 'HighN', red for 'LowN'
# one colour for each level of Trial.group

# number of levels of Trial.group
n_col <- length(unique(NDVI_ts$Trial.group))

# create blue colours
blues <- brewer.pal(n = n_col, "Blues")
# Warning message:
# In brewer.pal(n = n_col, "Blues") :
# minimal value for n is 3, returning requested palette with 3 different levels

# create red
reds <- brewer.pal(n = n_col, "Reds")

# Here I manually pick the first and the last 'blue' and 'red'
# From the plot in the question, it seems like you have more than two levels of Trial.group
# so you should be able to use the 'blues' and 'reds' vectors in scale_fill_manual.

# group boxes by date, Trial.group and Treatment
ggplot(data = NDVI_ts, aes(x = date, y = NDVI)) +
geom_boxplot(aes(fill = interaction(Trial.group, Treatment),
group = interaction(factor(date), Trial.group, Treatment))) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_x_date(breaks = "1 week", labels = date_format("%d-%b")) +
scale_fill_manual(name = "Treatment",
values = c("#FEE0D2", "#DE2D26", "#DEEBF7", "#3182BD"))
# scale_fill_manual(values = c(reds, blues))

enter image description here

关于r - ggplot2 : multiple factors boxplot with scale_x_date axis in R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20074061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com