teunbrand / ggh4x

ggplot extension: options for tailored facets, multiple colourscales and miscellaneous
https://teunbrand.github.io/ggh4x/
Other
534 stars 32 forks source link

match facet data with ggbarplot #145

Closed star9926 closed 4 months ago

star9926 commented 5 months ago

Hello, I appreciate your work on the package - it’s extremely useful! I’m encountering a problem with the facets in ggbarplot. I have three facets using ggh4x::facet_grid2(response ~ cellType + Cytokines, scales = 'free_y'), which works well with ggplot2 without specifying facet information. However, when I use ggbarplot, I’m unable to use these three facets to correspond with the data in each facet. Is there a way to modify the code to achieve this? For additional context, I have 4 types of Cytokines released from 3 different cell types, and I’d like to maintain Cytokines as a subgroup within cell types. Thank you so much for your help! Bets, Na

teunbrand commented 5 months ago

Hi there, can you post a reproducible example with, if your data are private, mocked up data or a standard dataset?

star9926 commented 5 months ago

Super! thanks for your quick response. I uploaded the file and code for data visualization. ### I failed with barplot, but succeed with boxplot.

for barplot

pi_cf_blo <- ggbarplot(cytoflex_p1_blood, x='Group',y='Statistic',nrow=1, color = 'Group',fill = 'Group',alpha=.6,

facet.by = c('cellCytokine'),

                   #facet.by = c('response','cellCytokine'),#'cellType','ICytokine'
                   palette = c(#'#868686FF',
                     "#CD534CFF",'#0073C2FF','#EFC000FF'),
                   add = c('mean_se','point'),
                   add.params = list(color='response',binwidth=1,
                                     fill='response' ))+

scale_color_manual(values = c("High IL-6" = "black", "Low IL-6" = 'gray'))+ ggh4x::facet_grid2(response ~ cellType + ICytokine,#Type, # cellCytokine, # scales = 'free_y')+ scale_y_continuous(labels = scales::label_scientific(digits = 2))+ stat_pvalue_manual(stat_pvalue,label = 'p.adj.signif',tip.length = 0, remove.bracket = F,hide.ns = T,size=2,#, step.increase = 0.02,label.size = 2)+ theme(legend.position = 'top', strip.placement = "outside", panel.border = element_rect(color = NA, fill=NA), axis.line = element_line(colour = "black"), legend.key.size = unit(.5,units = 'cm'), axis.text.x = element_blank(), axis.text.y = element_text(face = 'bold',size = 7), axis.title.y = element_text(face = 'bold',size = 13), axis.title.x = element_blank(),

legend.text = element_text(face = 'bold',size = 14),

    legend.text = element_text(face = 'bold',size = 12),
    legend.title = element_blank(),
    strip.text.y = element_text(size = 10,face = 'bold'),
    strip.text.x = element_text(size = 7,face = 'bold'))+

labs(x='Group',y='Blood Internal cytokine MFI') pi_cf_blo

for boxplot

ggboxplot(cytoflex_p1_blood, x='Group',y='Statistic',nrow=1, color = 'Group',fill = 'Group',alpha=.6, palette = c(#'#868686FF', "#CD534CFF",'#0073C2FF','#EFC000FF'), add = 'point', add.params = list(color='response',binwidth=.2,#size=.5, fill='response'))+ scale_color_manual(values = c("High IL-6" = "black", "Low IL-6" = 'gray'))+ ggh4x::facet_grid2(response ~ cellType + ICytokine, scales = 'free_y', independent = 'y'#,nrow = 1 )+

facet_wrap(vars(cellType,ICytokine), scales = 'free_y',nrow = 1)+

scale_y_continuous(labels = scales::label_scientific(digits = 2))+ stat_pvalue_manual(stat_pvalue,label = 'p.adj.signif',tip.length = 0, remove.bracket = F,hide.ns = T,size=2,#, step.increase = 0.02,label.size = 2)+ theme(legend.position = 'top', axis.text.x = element_blank(), axis.text.y = element_text(face = 'bold',size = 7), axis.title.y = element_text(face = 'bold',size = 13), axis.title.x = element_blank(),

legend.text = element_text(face = 'bold',size = 14),

    legend.text = element_text(face = 'bold',size = 12),
    legend.title = element_blank(),
    strip.text.y = element_text(size = 10,face = 'bold'),
    strip.text.x = element_text(size = 7,face = 'bold'))+

labs(x='Group',y='Blood Internal cytokine MFI')

240202_dt.csv

teunbrand commented 5 months ago

I can't reproduce either of these plots as there seems to be a response variable missing. In general, my advise for doing any sort of advanced plotting is to build plots from scratch instead of relying on wrappers like ggbarplot(). You can go layer-by-layer and identify more easily when something is going wrong.

star9926 commented 4 months ago

Thanks for your advice, my apologies for the delay. I have attached an updated CSV file, I still have no idea how to have 3 facets in ggbarplot, will try with scratch as you suggested. 240208_dt.csv

Anyhow, I tried to merged the 'cellType' and 'cytokines' into a single variable, and presented it within one facet. ggbarplot(cytoflex_p1_blood, x='Group',y='Statistic',nrow=1, facet.by = c('response','Type'), color = 'Group',fill = 'Group',alpha=.6, palette = c(#'#868686FF', "#CD534CFF",'#0073C2FF','#EFC000FF'), add = c('mean_se','point'),#'point',# add.params = list(color='response', binwidth=.1,#size=.5, fill='response' ))+ scale_color_manual(values = c("High IL-6" = "black", "Low IL-6" = 'gray'))+ ggh4x::facet_grid2(response ~ Type, #cellType + ICytokine, scales = 'free_y', independent = 'y'#,nrow = 1 )+ scale_y_continuous(labels = scales::label_scientific(digits = 2))+ theme(legend.position = 'top', strip.placement = "outside", panel.border = element_rect(color = NA, fill=NA), axis.line = element_line(colour = "black"), legend.key.size = unit(.5,units = 'cm'), axis.text.x = element_blank(),#element_text(face = 'bold',size = 8),

angle = 90),

    axis.text.y = element_text(face = 'bold',size = 5),
    axis.title.y = element_text(face = 'bold',size = 13),
    axis.title.x = element_blank(),
    #legend.text = element_text(face = 'bold',size = 14),
    legend.text = element_text(face = 'bold',size = 12),
    legend.title = element_blank(),
    strip.text.y = element_text(size = 10,face = 'bold'),
    strip.text.x = element_text(size = 5,face = 'bold',
                                colour = 'black'))+

labs(x='Group',y='Blood Internal cytokine MFI')