更改ggplot2中两条带的颜色
Change the colour of two strips in ggplot2
我有一个由 ggplot2 在 R 中生成的图表。在 facet_wrap
中我有两个变量。我想给它们涂上不同的颜色。我试图改变颜色,但两者都改变了。我想要每个条带都有一种颜色,所以我想要颜色。
ggplot(trial, aes(x =total , y = power, colour = Levels)) +
geom_line()+ facet_wrap(~P1+P2) + geom_point() + theme(legend.position = "right",plot.title = element_text(color="darkblue", size=16, face="bold.italic", hjust = 0.5),axis.title.x = element_text(color="black", size=12, face="bold"),
axis.title.y = element_text(color="black", size=12, face="bold"), strip.background = element_rect(fill = "blue") ) +
ggtitle("Plot")
有什么想法吗?
谢谢
library(statmod)
trial=expand.grid(n1=c(10,20,30,40,50),n2=c(10,20,30,40,50),
p1=c(0.1,0.2,0.3,0.5,0.8,0.9),p2=c(0.1,0.2,0.5,0.8,0.9), alpha=0.05)
trial$P1=as.factor(trial$p1)
trial$P2=as.factor(trial$p2)
trial$total = trial$n1 + trial$n2
for(row in 1:nrow(trial)){
trial$power[row]=power.fisher.test(trial$p1[row], trial$p2[row],
trial$n1[row],trial$n2[row],
alpha=0.05, nsim=100,
alternative="two.sided")}
假设您已将绘图保存在名为 plot
的对象中。
library(statmod)
library(ggplot)
plot <- ggplot(trial, aes(x =total , y = power, colour = as.factor(n1))) +
geom_line()+ facet_wrap(~P1+P2) + geom_point() + theme(legend.position = "right",plot.title = element_text(color="darkblue", size=16, face="bold.italic", hjust = 0.5),axis.title.x = element_text(color="black", size=12, face="bold"),
axis.title.y = element_text(color="black", size=12, face="bold"), strip.background = element_rect(fill = "blue") ) +
ggtitle("Plot")
使用来自@filups21 的信息,我们可以识别哪些 grobs 匹配条带背景并将第一个(顶部)元素设置为 "red"
:
library(grid)
library(gridExtra)
g <- ggplot_gtable(ggplot_build(plot))
stript <- which(grepl('strip-t', g$layout$name))
for(i in stript) g$grobs[[i]]$grobs[[1]]$children[[1]]$gp$fill <- "red"
grid::grid.draw(g)
我有一个由 ggplot2 在 R 中生成的图表。在 facet_wrap
中我有两个变量。我想给它们涂上不同的颜色。我试图改变颜色,但两者都改变了。我想要每个条带都有一种颜色,所以我想要颜色。
ggplot(trial, aes(x =total , y = power, colour = Levels)) +
geom_line()+ facet_wrap(~P1+P2) + geom_point() + theme(legend.position = "right",plot.title = element_text(color="darkblue", size=16, face="bold.italic", hjust = 0.5),axis.title.x = element_text(color="black", size=12, face="bold"),
axis.title.y = element_text(color="black", size=12, face="bold"), strip.background = element_rect(fill = "blue") ) +
ggtitle("Plot")
有什么想法吗?
谢谢
library(statmod)
trial=expand.grid(n1=c(10,20,30,40,50),n2=c(10,20,30,40,50),
p1=c(0.1,0.2,0.3,0.5,0.8,0.9),p2=c(0.1,0.2,0.5,0.8,0.9), alpha=0.05)
trial$P1=as.factor(trial$p1)
trial$P2=as.factor(trial$p2)
trial$total = trial$n1 + trial$n2
for(row in 1:nrow(trial)){
trial$power[row]=power.fisher.test(trial$p1[row], trial$p2[row],
trial$n1[row],trial$n2[row],
alpha=0.05, nsim=100,
alternative="two.sided")}
假设您已将绘图保存在名为 plot
的对象中。
library(statmod)
library(ggplot)
plot <- ggplot(trial, aes(x =total , y = power, colour = as.factor(n1))) +
geom_line()+ facet_wrap(~P1+P2) + geom_point() + theme(legend.position = "right",plot.title = element_text(color="darkblue", size=16, face="bold.italic", hjust = 0.5),axis.title.x = element_text(color="black", size=12, face="bold"),
axis.title.y = element_text(color="black", size=12, face="bold"), strip.background = element_rect(fill = "blue") ) +
ggtitle("Plot")
使用来自@filups21 "red"
:
library(grid)
library(gridExtra)
g <- ggplot_gtable(ggplot_build(plot))
stript <- which(grepl('strip-t', g$layout$name))
for(i in stript) g$grobs[[i]]$grobs[[1]]$children[[1]]$gp$fill <- "red"
grid::grid.draw(g)