ggplot2:R中有scale_x_date轴的多个因素boxplot [英] ggplot2 : multiple factors boxplot with scale_x_date axis in R
问题描述
我创建了一个相互作用矩阵与NDVI和不同试验组相关的因素 Treatment x Date
的组合:
在这里,您可以找到一些最低限度的数据:
dat < - Treatment Trial.group日期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
Hig hN 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 $ g $
ggplot(data = NDVI_ts,aes(x = interact,y))
库(scale)
interactive< -interaction(NDVI_ts $ Treatment,NDVI_ts $ Date,sep =: = NDVI))+
geom_boxplot(aes(fill = Trial.group),width = 0.6)+
theme_bw()+ theme(axis.text.x = element_text(angle = 90,hjust = 1 ))
这段代码给了我以下boxplot,这很好,但x轴不是链接到日期:(NDVI〜Treatment + Date + Trial.group)
我知道我通常可以这样做:
q + scale_x_date(breaks =1 week,labels = date_format(%d-%b))
但交互
矩阵是一个因素,不能被定义为一个时间对象,这样就不用了k。我有以下错误:
错误:输入无效:date_trans与类Date的对象一起工作
我如何能够按日期定义多变量boxplot位置?
NDVI_ts $ Date已被定义为日期 - 对象在R中。
使用交互创建x轴
'治疗'和'日期'之间可能有助于安排分组变量不同值的方框。但是,正如你所注意到的那样,当原始 Date
轴被转换为复合因子时,控制轴的外观就更难了。
这是一个替代方案,可将x轴保留在 Date
格式中。 治疗的两个层次通过创建两种不同的颜色区别。 治疗中的组由不同的颜色阴影分开。盒子通过使用组
参数分组。
库(ggplot2)
库(比例)
库(RColorBrewer)
#convert Date to class'Date'
NDVI_ts $ date< - as.Date(NDVI_ts $ Date,format =%d /%m /%Y)
#为框创建合适的颜色的一种可能的方法
#为每个级别的处理
#创建一个颜色调色板HighN的蓝色,LowN的红色
#Trial.group的每个级别的一种颜色
#Trial.group
n_col <长度(唯一(NDVI_ts $ Trial.group))
#创建蓝色
蓝调< - brewer.pal(n = n_col,蓝调)
#警告消息:
#在brewer.pal(n = n_col,蓝调):
#n的最小值为3,返回请求的调色板与3个不同的级别
#创建红色
reds< - brewer.pal(n = n_col,Reds)
#这里我手动选择第一个和最后一个蓝色和红色
#从问题的阴谋,似乎你有两个以上的级别的Trial.group
#,所以你应该能够使用scale_fill_manual中的'蓝色'和'红色'向量。
#group box by date,Trial.group and Treatment
ggplot(data = NDVI_ts,aes(x = date,y = NDVI))+
geom_boxplot(aes =交互(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 =治疗
值= c(#FEE0D2,#DE2D26,#DEEBF7,#3182BD))
#scale_fill_manual(values = c(reds,blues))
I would like to create a multivariate boxplot time serie with ggplot2 and i need to have an x axis with boxplot position function of dates.
I created for that an interaction matrix with the combination of the factors Treatment x Date
that is plotted against NDVI and with different trial groups:
here you can find some minimal data :
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"
Here is the code to import and create the plot :
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))
This code give me the following boxplot, which is fine, but the x-axis is not linked to dates : (NDVI ~ Treatment + Date + Trial.group)
I know that I can normally do that with something like this :
q + scale_x_date(breaks="1 week", labels=date_format("%d-%b"))
but the interact
matrix is a factor and cannot be defined as a time object, so that doesn't work. I have the following error :
Error: Invalid input: date_trans works with objects of class Date only
How could I have multivariate boxplot positions defined by dates?
The NDVI_ts$Date is already defined as a date-object in R.
Creating an x axis with the interaction
between 'Treatment' and 'Date' may facilitate the arrangement of boxes of different value of the grouping variables. However, as you noticed, when the original Date
axis is converted to a 'composite' factor, it is much harder to control appearance of the axis.
Here is an alternative which keeps the x axis in Date
format. The two levels of 'Treatment' are distinguished by creating two different colours palettes. Groups within 'Treatment' are separated by different shades of the colour. Boxes are grouped by using the group
argument.
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))
这篇关于ggplot2:R中有scale_x_date轴的多个因素boxplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!