按因子水平的抖动箱线图加上组合水平的箱线图
Jitter Boxplot by factor level plus boxplot of combined levels
我有三个重复的实验样本 (F) 和三个重复的对照样本 (C)。每个实验样本有 100 个数据点,而每个对照有 70 个数据点。对于实验数据点,有 4E、5E、7E、8E 等子类别,而对于控制,只有一个类别 CE。
下面是生成一些模拟数据的代码:
library(ggplot2)
set.seed(12345)
evals <- c( rep("4E",20), rep("5E",20), rep("7E",40), rep("8E",20))
subE <- c(sample(evals),sample(evals),sample(evals),rep("CE",70),rep("CE",70),rep("CE",70))
pwvr <- c(rnorm(100),rnorm(100),rnorm(100),rnorm(70,1.0),rnorm(70,1.1),rnorm(70,1.2))
expT <- c(rep("F",100*3),rep("C",70*3))
repX <- c(rep(1,100),rep(2,100),rep(3,100),rep(1,70),rep(2,70),rep(3,70))
myData.df <- data.frame(subE=as.factor(subE), pwvr = pwvr, expT= as.factor(expT), repX= as.factor(repX))
dim(myData.df)
我想做的是绘制因子水平 4E、5E、7E、8E 的箱线图和抖动图,以及这四个水平的组合值的箱线图。我不知道该怎么做。我是否需要创建另一个包含所有相应值的关卡?
其次,谁能告诉我如何沿 X 轴重新组织订单的外观,例如如何绘制以下订单 8E、7E、5E、4E 等。
以下代码为各个级别生成图,但我还需要为组合级别生成 box/jitter。
myGreen <- "forestgreen"
myBlue <- "dodgerblue2"
allboxCol <- c(rep(myGreen,1),rep(myBlue,1))
pw.boxplot <- ggplot(myData.df, aes(x=subE,y=pwvr, fill= expT)) +
geom_jitter(position=position_jitter(width=.2, height=0),alpha=0.15, aes(col= expT)) + scale_color_manual(values=allboxCol) +
geom_boxplot(outlier.shape = NA, fatten = 0.01, lwd=1.0,alpha=0.5,width=0.6) +
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
pw.boxplot + scale_fill_manual(values=allboxCol) + facet_grid( .~ expT + repX , scales="free", space = "free" )
你是说吗?
myGreen <- "forestgreen"
myBlue <- "dodgerblue2"
allboxCol <- c(rep(myGreen,1),rep(myBlue,1))
allboxCol <- c(alpha(allboxCol, 0.2),allboxCol[2])
library(tidyverse)
myData.df %>%
as_tibble() %>%
mutate(subE = paste0("total_", expT, repX)) %>%
filter(expT != "C") %>%
bind_rows(myData.df) %>%
mutate(fill = ifelse(grepl("total", subE), paste0("total_",expT), expT)) %>%
ggplot(aes(x=subE,y=pwvr, fill= fill)) +
geom_jitter(position=position_jitter(width=.2, height=0),alpha=0.15, aes(col= fill), show.legend = F) +
geom_boxplot(outlier.shape = NA, fatten = 0.01, lwd=1.0,width=0.6, show.legend = F) +
scale_fill_manual(values=allboxCol) +
scale_color_manual(values=allboxCol) +
theme_bw() +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
facet_grid( .~ expT + repX , scales="free", space = "free" )
只是重复数据但分配一个新级别确实是一个简单的解决方案,我称之为all
。您可以明确指定级别的顺序:
library(ggplot2)
library(dplyr)
set.seed(12345)
evals <- c( rep("4E",20), rep("5E",20), rep("7E",40), rep("8E",20))
subE <- c(sample(evals),sample(evals),sample(evals),rep("CE",70),rep("CE",70),rep("CE",70))
pwvr <- c(rnorm(100),rnorm(100),rnorm(100),rnorm(70,1.0),rnorm(70,1.1),rnorm(70,1.2))
expT <- c(rep("F",100*3),rep("C",70*3))
repX <- c(rep(1,100),rep(2,100),rep(3,100),rep(1,70),rep(2,70),rep(3,70))
myData.df <- data.frame(subE=subE, pwvr = pwvr, expT= expT, repX= repX,
stringsAsFactors = FALSE)
add_data <- myData.df %>%
filter(subE != "CE") %>%
mutate(subE = "all")
myData.df <- bind_rows(myData.df, add_data)
myData.df <- myData.df %>%
mutate(subE = as.factor(subE),
subE = factor(subE, levels = levels(subE)[c(4, 3, 2, 1, 5, 6)]))
myGreen <- "forestgreen"
myBlue <- "dodgerblue2"
allboxCol <- c(rep(myGreen,1),rep(myBlue,1))
pw.boxplot <- ggplot(myData.df, aes(x=subE,y=pwvr, fill= expT)) +
geom_jitter(position=position_jitter(width=.2, height=0),alpha=0.15, aes(col= expT)) + scale_color_manual(values=allboxCol) +
geom_boxplot(outlier.shape = NA, fatten = 0.01, lwd=1.0,alpha=0.5,width=0.6) +
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
pw.boxplot + scale_fill_manual(values=allboxCol) + facet_grid( .~ expT + repX , scales="free", space = "free" )
我有三个重复的实验样本 (F) 和三个重复的对照样本 (C)。每个实验样本有 100 个数据点,而每个对照有 70 个数据点。对于实验数据点,有 4E、5E、7E、8E 等子类别,而对于控制,只有一个类别 CE。
下面是生成一些模拟数据的代码:
library(ggplot2)
set.seed(12345)
evals <- c( rep("4E",20), rep("5E",20), rep("7E",40), rep("8E",20))
subE <- c(sample(evals),sample(evals),sample(evals),rep("CE",70),rep("CE",70),rep("CE",70))
pwvr <- c(rnorm(100),rnorm(100),rnorm(100),rnorm(70,1.0),rnorm(70,1.1),rnorm(70,1.2))
expT <- c(rep("F",100*3),rep("C",70*3))
repX <- c(rep(1,100),rep(2,100),rep(3,100),rep(1,70),rep(2,70),rep(3,70))
myData.df <- data.frame(subE=as.factor(subE), pwvr = pwvr, expT= as.factor(expT), repX= as.factor(repX))
dim(myData.df)
我想做的是绘制因子水平 4E、5E、7E、8E 的箱线图和抖动图,以及这四个水平的组合值的箱线图。我不知道该怎么做。我是否需要创建另一个包含所有相应值的关卡?
其次,谁能告诉我如何沿 X 轴重新组织订单的外观,例如如何绘制以下订单 8E、7E、5E、4E 等。
以下代码为各个级别生成图,但我还需要为组合级别生成 box/jitter。
myGreen <- "forestgreen"
myBlue <- "dodgerblue2"
allboxCol <- c(rep(myGreen,1),rep(myBlue,1))
pw.boxplot <- ggplot(myData.df, aes(x=subE,y=pwvr, fill= expT)) +
geom_jitter(position=position_jitter(width=.2, height=0),alpha=0.15, aes(col= expT)) + scale_color_manual(values=allboxCol) +
geom_boxplot(outlier.shape = NA, fatten = 0.01, lwd=1.0,alpha=0.5,width=0.6) +
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
pw.boxplot + scale_fill_manual(values=allboxCol) + facet_grid( .~ expT + repX , scales="free", space = "free" )
你是说吗?
myGreen <- "forestgreen"
myBlue <- "dodgerblue2"
allboxCol <- c(rep(myGreen,1),rep(myBlue,1))
allboxCol <- c(alpha(allboxCol, 0.2),allboxCol[2])
library(tidyverse)
myData.df %>%
as_tibble() %>%
mutate(subE = paste0("total_", expT, repX)) %>%
filter(expT != "C") %>%
bind_rows(myData.df) %>%
mutate(fill = ifelse(grepl("total", subE), paste0("total_",expT), expT)) %>%
ggplot(aes(x=subE,y=pwvr, fill= fill)) +
geom_jitter(position=position_jitter(width=.2, height=0),alpha=0.15, aes(col= fill), show.legend = F) +
geom_boxplot(outlier.shape = NA, fatten = 0.01, lwd=1.0,width=0.6, show.legend = F) +
scale_fill_manual(values=allboxCol) +
scale_color_manual(values=allboxCol) +
theme_bw() +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
facet_grid( .~ expT + repX , scales="free", space = "free" )
只是重复数据但分配一个新级别确实是一个简单的解决方案,我称之为all
。您可以明确指定级别的顺序:
library(ggplot2)
library(dplyr)
set.seed(12345)
evals <- c( rep("4E",20), rep("5E",20), rep("7E",40), rep("8E",20))
subE <- c(sample(evals),sample(evals),sample(evals),rep("CE",70),rep("CE",70),rep("CE",70))
pwvr <- c(rnorm(100),rnorm(100),rnorm(100),rnorm(70,1.0),rnorm(70,1.1),rnorm(70,1.2))
expT <- c(rep("F",100*3),rep("C",70*3))
repX <- c(rep(1,100),rep(2,100),rep(3,100),rep(1,70),rep(2,70),rep(3,70))
myData.df <- data.frame(subE=subE, pwvr = pwvr, expT= expT, repX= repX,
stringsAsFactors = FALSE)
add_data <- myData.df %>%
filter(subE != "CE") %>%
mutate(subE = "all")
myData.df <- bind_rows(myData.df, add_data)
myData.df <- myData.df %>%
mutate(subE = as.factor(subE),
subE = factor(subE, levels = levels(subE)[c(4, 3, 2, 1, 5, 6)]))
myGreen <- "forestgreen"
myBlue <- "dodgerblue2"
allboxCol <- c(rep(myGreen,1),rep(myBlue,1))
pw.boxplot <- ggplot(myData.df, aes(x=subE,y=pwvr, fill= expT)) +
geom_jitter(position=position_jitter(width=.2, height=0),alpha=0.15, aes(col= expT)) + scale_color_manual(values=allboxCol) +
geom_boxplot(outlier.shape = NA, fatten = 0.01, lwd=1.0,alpha=0.5,width=0.6) +
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
pw.boxplot + scale_fill_manual(values=allboxCol) + facet_grid( .~ expT + repX , scales="free", space = "free" )