ggplot 中连续变量的颜色方面
Color facets in ggplot by continuous variable
** 使用可重复数据进行编辑 **
我有一个 data.frame,其中包含 50 次实验处理随时间的增长图。我将它们绘制成多面 5x10 绘图网格。考虑到我的实验性治疗,我还以一种有意义的方式订购了它们。
我 运行 一个回归函数来找到每个处理的增长率,并将斜率值保存在另一个数据框中。我已经绘制了数据、回归线和增长率值,但我想根据该回归斜率值为各个分面图的背景着色,但我不知道如何设置要调用的颜色一个连续变量,尤其是来自具有不同行数的不同 df 的变量(原始 df 有 300 行,我想调用的 df 有 50 - 每个处理一个)。
我的代码如下:
Df:
df <- data.frame(matrix(ncol = 3,nrow=300))
colnames(df) <- c("Trt", "Day", "Size")
df$Trt <- rep(1:50, each=6)
df$Day <- rep_len(1:6, length.out=300)
df$Size <- rep_len(c(3,5,8,9,12,12,3,7,10,16,17,20),length.out = 300)
回归函数和输出数据帧:
regression=function(df){
reg_fun<-lm(formula=df$Size~df$Day)
slope<-round(coef(reg_fun)[2],3)
intercept<-round(coef(reg_fun)[1],3)
R2<-round(as.numeric(summary(reg_fun)[8]),3)
R2.Adj<-round(as.numeric(summary(reg_fun)[9]),3)
c(slope,intercept,R2,R2.Adj)
}
library(plyr)
slopevalues<-ddply(df,"Trt",regression)
colnames(slopevalues)<-c ("Trt","slope","intercept","R2","R2.Adj")
剧情:
ggplot(data=df, aes(x=Day, y=Size))+
geom_line() +
geom_point() +
xlab("Day") + ylab("Size (μm)")+
geom_smooth(method="lm",size=.5,se=FALSE)+
geom_text(data=slopevalues,
inherit.aes=FALSE,
aes(x =1, y = 16,hjust=0,
label=paste(slope)))+
facet_wrap(~ Trt, nrow=5)
我想要做的是根据梯度上的斜率值 (slopevalues$slope) 为各个图形的背景着色。我的真实数据不仅仅是重复的 2 个值,所以我想根据该值在颜色渐变上执行此操作。
欢迎任何建议。
enter image description here
你可以使用无限坐标的geom_rect
来做到这一点:
ggplot(data=df, aes(x=Day, y=Size))+
## This is the only new bit
geom_rect(
aes(fill = slope, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf),
slopevalues,
inherit.aes = FALSE
) +
## New bit ends here
geom_line() +
geom_point() +
xlab("Day") + ylab("Size (μm)")+
geom_smooth(method="lm",size=.5,se=FALSE)+
geom_text(data=slopevalues,
inherit.aes=FALSE,
aes(x =1, y = 16,hjust=0,
label=paste(slope)))+
facet_wrap(~ Trt, nrow=5)
** 使用可重复数据进行编辑 **
我有一个 data.frame,其中包含 50 次实验处理随时间的增长图。我将它们绘制成多面 5x10 绘图网格。考虑到我的实验性治疗,我还以一种有意义的方式订购了它们。
我 运行 一个回归函数来找到每个处理的增长率,并将斜率值保存在另一个数据框中。我已经绘制了数据、回归线和增长率值,但我想根据该回归斜率值为各个分面图的背景着色,但我不知道如何设置要调用的颜色一个连续变量,尤其是来自具有不同行数的不同 df 的变量(原始 df 有 300 行,我想调用的 df 有 50 - 每个处理一个)。
我的代码如下:
Df:
df <- data.frame(matrix(ncol = 3,nrow=300))
colnames(df) <- c("Trt", "Day", "Size")
df$Trt <- rep(1:50, each=6)
df$Day <- rep_len(1:6, length.out=300)
df$Size <- rep_len(c(3,5,8,9,12,12,3,7,10,16,17,20),length.out = 300)
回归函数和输出数据帧:
regression=function(df){
reg_fun<-lm(formula=df$Size~df$Day)
slope<-round(coef(reg_fun)[2],3)
intercept<-round(coef(reg_fun)[1],3)
R2<-round(as.numeric(summary(reg_fun)[8]),3)
R2.Adj<-round(as.numeric(summary(reg_fun)[9]),3)
c(slope,intercept,R2,R2.Adj)
}
library(plyr)
slopevalues<-ddply(df,"Trt",regression)
colnames(slopevalues)<-c ("Trt","slope","intercept","R2","R2.Adj")
剧情:
ggplot(data=df, aes(x=Day, y=Size))+
geom_line() +
geom_point() +
xlab("Day") + ylab("Size (μm)")+
geom_smooth(method="lm",size=.5,se=FALSE)+
geom_text(data=slopevalues,
inherit.aes=FALSE,
aes(x =1, y = 16,hjust=0,
label=paste(slope)))+
facet_wrap(~ Trt, nrow=5)
我想要做的是根据梯度上的斜率值 (slopevalues$slope) 为各个图形的背景着色。我的真实数据不仅仅是重复的 2 个值,所以我想根据该值在颜色渐变上执行此操作。
欢迎任何建议。
enter image description here
你可以使用无限坐标的geom_rect
来做到这一点:
ggplot(data=df, aes(x=Day, y=Size))+
## This is the only new bit
geom_rect(
aes(fill = slope, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf),
slopevalues,
inherit.aes = FALSE
) +
## New bit ends here
geom_line() +
geom_point() +
xlab("Day") + ylab("Size (μm)")+
geom_smooth(method="lm",size=.5,se=FALSE)+
geom_text(data=slopevalues,
inherit.aes=FALSE,
aes(x =1, y = 16,hjust=0,
label=paste(slope)))+
facet_wrap(~ Trt, nrow=5)