ggplot failed to color strip.background in facet

--Hi,

i try to generate a plot with ggplot using facet_wrap function but it failed to color
the barplot (i have the same color for all the barplot) and again for the strip.background of each group of facet.

my code here:

library(ggplot2)
library(tidyverse)
library(extrafont)
library(reshape2)
library(ggbreak)

Data <- data.frame(sample=as.character(unlist(lapply(c("Ref","EB 14j","HF11"),function(x) {rep(x,12)}))),
target=as.character(unlist(rep(c("EN1","MAP2","NEFL","PAX6","AFP","FOXA2","SOX17","SST","CDH5","HAND1","MYOG","TBXT"),3))),
ratio=as.numeric(unlist(c(1,1,1,1,1,1,1,1,1,1,1,1,4.24137,0.406923398,1.5932461,18.1362,4.977376,4.059928,7.260578,1.529779,2.704227,434.6353,6.443263,
17.46668,2.28697,0.001868545,0.1174228,0,210.767971,27.189933,19.108250,29.692244,1.290268,6193.3215,2.692950,40.94221))),
color=as.character(unlist(lapply(c("#38C0DC","#dcdcdc","#b4b4b4"),function(x) {rep(x,12)}))))

theme_set(theme_bw(base_size=12, base_family='Roboto Condensed')+
    theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()))
    
theme_set(theme_grey())

target <- c("EN1","MAP2","NEFL","PAX6","AFP","FOXA2","SOX17","SST","CDH5","HAND1","MYOG","TBXT")
FM <- c("#FFFFCC","#FFFFCC","#FFFFCC","#FFFFCC","#FFCCFF","#FFCCFF","#FFCCFF","#FFCCFF","#CCFFFF","#CCFFFF","#CCFFFF","#CCFFFF")  # 3 groups of colors need to be affected to each 3 groups of targets

p <- ggplot(Data, aes(x=fct_relevel(sample, "Ref"), y=as.numeric(ratio))) +
geom_col(position = position_dodge2(width = 0.4, preserve = "single"), colour = "black", alpha=.5, width=.5, size=0.1) +
scale_y_continuous(breaks = c(0,1,6500), expand = expansion(mult = c(0, .1))) +
scale_fill_manual(Data$sample,values = Data$color) +
labs(x = 'Samples',y='Fold-Induction') +
facet_wrap(~target, scales='free', drop = TRUE) +
theme_bw() +
theme(axis.text.x = element_text(vjust = 1, hjust=1, color="black", size=5, angle = 80),plot.margin = margin(5, 5, 5, 5, "mm"),legend.position = 'none',panel.spacing = unit(0.2, "lines")) +
theme(axis.text.y = element_text(vjust = 0.2 ,color="black", size=5),legend.position = 'none',panel.spacing = unit(0.5, "lines")) +
theme(axis.title.y = element_text(vjust = 0.1, color="black", margin = margin(t = 0, r = 6, b = 0, l = 0), size=6, family="Roboto Condensed", face="plain")) +
theme(axis.title.x = element_text(vjust = 0.5, color="black", margin = margin(t = 10, r = 6, b = 0, l = 0), size=6, family="Roboto Condensed", face="plain")) +
theme(
axis.ticks = element_line(color = "black", size = 0.1),
axis.ticks.length = unit(2,"pt"),
axis.ticks.x=element_blank(),
panel.grid.major.y=element_line(color="gray91",size = 0.20, linetype = 3),
panel.grid.minor.y=element_blank(),
panel.grid.major.x=element_blank(),
panel.grid.minor.x=element_blank(),
panel.spacing = unit(1, "lines"),
panel.spacing.x = unit(0.2, "lines"), # control space between facet
strip.text = element_text(face="bold", size=6,colour="black",lineheight=3.0),
panel.border = element_rect(color = "#38C0DC"),
strip.background = element_rect(fill=FM, colour="#38C0DC",size=0.5)
)

# One facet need to have a break in Y axis because we have high values
# p + scale_y_cut(breaks=c(20), which=c(1, 2), scales=c(3, 0.5), space=.001)

also i need to create a break on y axis only in one facet but it failed

To set the bar colors, I change scale_fill_manual() and I added a fill aesthetic to the ggplot() call. I left out all the theme() calls to make the code easier to read in this example. I don't know how to vary the color of
the facet strips.

library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.3.3
#library(extrafont)
#library(reshape2)
#library(ggbreak)

Data <- data.frame(sample=as.character(unlist(lapply(c("Ref","EB 14j","HF11"),function(x) {rep(x,12)}))),
                   target=as.character(unlist(rep(c("EN1","MAP2","NEFL","PAX6","AFP","FOXA2","SOX17","SST","CDH5","HAND1","MYOG","TBXT"),3))),
                   ratio=as.numeric(unlist(c(1,1,1,1,1,1,1,1,1,1,1,1,4.24137,0.406923398,1.5932461,18.1362,4.977376,4.059928,7.260578,1.529779,2.704227,434.6353,6.443263,
                                             17.46668,2.28697,0.001868545,0.1174228,0,210.767971,27.189933,19.108250,29.692244,1.290268,6193.3215,2.692950,40.94221))),
                   color=as.character(unlist(lapply(c("#38C0DC","#dcdcdc","#b4b4b4"),function(x) {rep(x,12)}))))

theme_set(theme_bw(base_size=12, base_family='Roboto Condensed')+
            theme(panel.grid.major = element_blank(),
                  panel.grid.minor = element_blank()))

theme_set(theme_grey())

target <- c("EN1","MAP2","NEFL","PAX6","AFP","FOXA2","SOX17","SST","CDH5","HAND1","MYOG","TBXT")
FM <- c("#FFFFCC","#FFFFCC","#FFFFCC","#FFFFCC","#FFCCFF","#FFCCFF","#FFCCFF","#FFCCFF","#CCFFFF","#CCFFFF","#CCFFFF","#CCFFFF")  # 3 groups of colors need to be affected to each 3 groups of targets

 ggplot(Data, aes(x=fct_relevel(sample, "Ref"), y=as.numeric(ratio),fill=sample)) +
  geom_col(position = position_dodge2(width = 0.4, preserve = "single"), colour = "black", alpha=.5, width=.5, size=0.1) +
  scale_y_continuous(breaks = c(0,1,6500), expand = expansion(mult = c(0, .1))) +
  scale_fill_manual(values = c(Ref="#38C0DC",`EB 14j`="#dcdcdc",HF11="#b4b4b4")) +
  labs(x = 'Samples',y='Fold-Induction') +
  facet_wrap(~target, scales='free', drop = TRUE)
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

Created on 2024-08-31 with reprex v2.0.2

Here you hard coded the color directly passing arguments to scale_fill_manual, but the problem is that barplot colors must be able to change dynamically according to the colors indicated in the color column of Dataframe.
The facet strips colors have 3 colors corresponding to 3 groups, such as the picture here:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.