Reputation: 121
I have a graph generated by ggplot2 in R. In facet_wrap
I have two variables. I want to color them different. I tried to change the color but it changed for both. I want for every strip a color so I want to colors.
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")}
Upvotes: 1
Views: 411
Reputation: 24770
Suppose you've saved your plot in an object called 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")
Using the information from this excellent answer from @filups21, we can identify which grobs match the strip backgrounds and set the first (top) element to "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)
Upvotes: 2