有没有办法改变 R 中数据重要的箱形图颜色
Is there a way to change the box plot color where data are significant in R
我的数据如下:
df1<-read.table(text = "time type
12 B88
19 B44
18 B44
13 B88
17 B44",header=TRUE)
我可以使用以下代码来获取我的情节:
ggplot(df1,aes(type,time)) + geom_boxplot(fill="green")+
stat_compare_means(method = "t.test")
我想为当 P 值小于 0.05% 时具有高介质的框获得不同的颜色,比方说蓝色。我们能做到吗?
注意: 我对 运行 ttest
不感兴趣
ggplot(df1,aes(type,time)) + geom_boxplot(fill="green") +
stat_compare_means(method = "t.test") -> p #save your plot as p
build <- ggplot_build(p) # build plot
build$data[[1]][,"fill"] <- ifelse(build$data[[2]][1,"p.format"] < 0.05, list(c("blue","green")),list(rep("green",2))) # changes fill to blue if p value is < 0.05
plot(ggplot_gtable(build)) # plot new formatted graph
可能不是最优雅的方法,但您可以在 ggplot2
之外计算 p 值并使用 ifelse
语句,为您可以使用 scale_fill_identity
调用的颜色模式添加属性.
这里是一个使用虚拟示例的示例:
df <- data.frame(Xval = rep(c("A","B"),each = 50),
Yval = c(sample(1:50,50), sample(50:100,50)))
我在这里使用了 dplyr
管道序列,但您可以在 base r
:
中很容易地做到这一点
library(dplyr)
library(ggplot2)
df %>% mutate(pval = t.test(Yval~Xval)$p.value) %>%
group_by(Xval) %>% mutate(Mean = mean(Yval)) %>%
ungroup() %>%
mutate(Color = ifelse(pval < 0.05 & Mean == max(Mean), "blue","green")) %>%
ggplot(aes(x = Xval, y = Yval, fill = Color))+
geom_boxplot()+
stat_compare_means(method = "t.test")+
scale_fill_identity()
使用你的例子:
df1 %>% mutate(pval = t.test(time~type)$p.value) %>%
group_by(type) %>% mutate(Mean = mean(time)) %>%
ungroup() %>%
mutate(Color = ifelse(pval < 0.05 & Mean == max(Mean), "blue","green")) %>%
ggplot(aes(x = type, y = time, fill = Color))+
geom_boxplot()+
stat_compare_means(method = "t.test")+
scale_fill_identity()
我的数据如下:
df1<-read.table(text = "time type
12 B88
19 B44
18 B44
13 B88
17 B44",header=TRUE)
我可以使用以下代码来获取我的情节:
ggplot(df1,aes(type,time)) + geom_boxplot(fill="green")+
stat_compare_means(method = "t.test")
我想为当 P 值小于 0.05% 时具有高介质的框获得不同的颜色,比方说蓝色。我们能做到吗? 注意: 我对 运行 ttest
不感兴趣ggplot(df1,aes(type,time)) + geom_boxplot(fill="green") +
stat_compare_means(method = "t.test") -> p #save your plot as p
build <- ggplot_build(p) # build plot
build$data[[1]][,"fill"] <- ifelse(build$data[[2]][1,"p.format"] < 0.05, list(c("blue","green")),list(rep("green",2))) # changes fill to blue if p value is < 0.05
plot(ggplot_gtable(build)) # plot new formatted graph
可能不是最优雅的方法,但您可以在 ggplot2
之外计算 p 值并使用 ifelse
语句,为您可以使用 scale_fill_identity
调用的颜色模式添加属性.
这里是一个使用虚拟示例的示例:
df <- data.frame(Xval = rep(c("A","B"),each = 50),
Yval = c(sample(1:50,50), sample(50:100,50)))
我在这里使用了 dplyr
管道序列,但您可以在 base r
:
library(dplyr)
library(ggplot2)
df %>% mutate(pval = t.test(Yval~Xval)$p.value) %>%
group_by(Xval) %>% mutate(Mean = mean(Yval)) %>%
ungroup() %>%
mutate(Color = ifelse(pval < 0.05 & Mean == max(Mean), "blue","green")) %>%
ggplot(aes(x = Xval, y = Yval, fill = Color))+
geom_boxplot()+
stat_compare_means(method = "t.test")+
scale_fill_identity()
使用你的例子:
df1 %>% mutate(pval = t.test(time~type)$p.value) %>%
group_by(type) %>% mutate(Mean = mean(time)) %>%
ungroup() %>%
mutate(Color = ifelse(pval < 0.05 & Mean == max(Mean), "blue","green")) %>%
ggplot(aes(x = type, y = time, fill = Color))+
geom_boxplot()+
stat_compare_means(method = "t.test")+
scale_fill_identity()