gmteunisse / fantaxtic

Fantaxtic - Nested Bar Plots for Phyloseq Data
26 stars 3 forks source link

Need help with formatting plot #39

Closed bark9299 closed 4 months ago

bark9299 commented 4 months ago

Hello, I am somewhat new to R plotting and need some help on choosing another palette and repositioning the legend. I tried to use your code on 'advanced usage' but can't seem to edit it correctly to do what I want. First, to change the palette, I tried to use a pre-made palette from dutchmasters image with the following code: pal <- taxon_colours(ps_tmp, tax_level = top_level, palette = dutchmasters$milkmaid) and then run all of the following commands:

# Get the top taxa
top_level <- "Phylum"
nested_level <- "Class"
sample_order <- NULL 
top_asv_class <- top_taxa(normalized_t, n_taxa = 10, tax_level = "Class")

# Create names for NA taxa
ps_tmp <- top_asv_class$ps_obj %>%
  name_na_taxa()

# Add labels to taxa with the same names
ps_tmp <- ps_tmp %>%
  label_duplicate_taxa(tax_level = nested_level)

# Generate a palette based on the phyloseq object
pal <- taxon_colours(ps_tmp,
                     tax_level = top_level,
                     palette = dutchmasters$milkmaid)
# Convert physeq to df
psdf <- psmelt(ps_tmp)

# Move the merged labels to the appropriate positions in the plot:
# Top merged labels need to be at the top of the plot,
# nested merged labels at the bottom of each group
psdf <- move_label(psdf = psdf,
                   col_name = top_level,
                   label = "Other",
                   pos = 16)
psdf <- move_nested_labels(psdf,
                           top_level = top_level,
                           nested_level = nested_level,
                           top_merged_label = "Other",
                           nested_label = "Other",
                           pos = Inf)

# Reorder samples
if(!is.null(sample_order)){
  if(all(sample_order %in% unique(psdf$Sample))){
    psdf <- psdf %>%
      mutate(Sample = factor(Sample, levels = sample_order))
  } else {
    stop("Error: not all(sample_order %in% sample_names(ps_obj)).")
  }

}

# Generate a base plot
library(ggnested)
p <- ggnested(psdf,
              aes_string(main_group = top_level,
                         sub_group = nested_level,
                         x = "Sample",
                         y = "Abundance"),
              legend_title  = "Phylum and Class",
                         main_palette = pal) +
  scale_y_continuous(expand = c(0, 0)) +
  theme_nested(theme_light) +
  theme(axis.text.x = element_text(hjust = 1, vjust = 0.5, angle = 90, face = "bold"), legend.key.size = unit(12, "points"))

#Add relative abundances
p <- p + geom_col(position = position_fill())
p

When I run this command, it gives me the following plot: plot_zoom_png

The colors obviously didn't change to be like the palette I chose. How do I fix this? I also tried to do it manually and the specific colors I chose do not show exactly the way they should: pal <- taxon_colours(top_nested_fam$ps_obj, tax_level = "Phylum", palette = c("chocolate", "firebrick", "gold", "forestgreen", "dodgerblue", "darkviolet", "darkseagreen", "navy", "aquamarine", "magenta"))

Also, as you can see in the plot, the legend is 3 columns wide. I would like to somehow just make it 2 columns and make the legend longer so, for example, Cyanobacteria and Desulfobacterota_I are moved to the first column and is spanning the length of the plot more.

I apologize about the confusion. E

gmteunisse commented 4 months ago

Thanks for using fantaxtic. You won’t need the advanced example, you can simply pass your palette to the palette option (see the example here https://github.com/gmteunisse/fantaxtic?tab=readme-ov-file#palette). Bear in mind that the palette only provides the base colours from which the shades and tints are generated.

To alter the number of columns in the legend, you can just treat the plot like any ggplot object. I think that this question on stack overflow should allow you to play with the number of columns: https://stackoverflow.com/questions/18400432/creating-multi-column-legend-in-ggplot. Please be aware that it is going to be very difficult to avoid a single taxonomic level being split over multiple columns.

I hope that answers your questions.


From: bark9299 @.> Sent: Monday, July 15, 2024 11:24:06 PM To: gmteunisse/fantaxtic @.> Cc: Subscribed @.***> Subject: [gmteunisse/fantaxtic] Need help with formatting plot (Issue #39)

Hello, I am somewhat new to R plotting and need some help on choosing another palette and repositioning the legend. I tried to use your code on 'advanced usage' but can't seem to edit it correctly to do what I want. First, to change the palette, I tried to use a pre-made palette from dutchmasters image.png (view on web)https://github.com/user-attachments/assets/9bf2a9a1-21fb-4a02-8853-1721c7aa667f with the following code: pal <- taxon_colours(ps_tmp, tax_level = top_level, palette = dutchmasters$milkmaid) and then run all of the following commands:

Get the top taxa

top_level <- "Phylum" nested_level <- "Class" sample_order <- NULL top_asv_class <- top_taxa(normalized_t, n_taxa = 10, tax_level = "Class")

Create names for NA taxa

ps_tmp <- top_asv_class$ps_obj %>% name_na_taxa()

Add labels to taxa with the same names

ps_tmp <- ps_tmp %>% label_duplicate_taxa(tax_level = nested_level)

Generate a palette based on the phyloseq object

pal <- taxon_colours(ps_tmp, tax_level = top_level, palette = dutchmasters$milkmaid)

Convert physeq to df

psdf <- psmelt(ps_tmp)

Move the merged labels to the appropriate positions in the plot:

Top merged labels need to be at the top of the plot,

nested merged labels at the bottom of each group

psdf <- move_label(psdf = psdf, col_name = top_level, label = "Other", pos = 16) psdf <- move_nested_labels(psdf, top_level = top_level, nested_level = nested_level, top_merged_label = "Other", nested_label = "Other", pos = Inf)

Reorder samples

if(!is.null(sample_order)){ if(all(sample_order %in% unique(psdf$Sample))){ psdf <- psdf %>% mutate(Sample = factor(Sample, levels = sample_order)) } else { stop("Error: not all(sample_order %in% sample_names(ps_obj)).") }

}

Generate a base plot

library(ggnested) p <- ggnested(psdf, aes_string(main_group = top_level, sub_group = nested_level, x = "Sample", y = "Abundance"), legend_title = "Phylum and Class", main_palette = pal) + scale_y_continuous(expand = c(0, 0)) + theme_nested(theme_light) + theme(axis.text.x = element_text(hjust = 1, vjust = 0.5, angle = 90, face = "bold"), legend.key.size = unit(12, "points"))

Add relative abundances

p <- p + geom_col(position = position_fill()) p

When I run this command, it gives me the following plot: plot_zoom_png.png (view on web)https://github.com/user-attachments/assets/4507d0e9-f41b-4bf2-a68e-5a75136beb9f

The colors obviously didn't change to be like the palette I chose. How do I fix this? I also tried to do it manually and the specific colors I chose do not show exactly the way they should: pal <- taxon_colours(top_nested_fam$ps_obj, tax_level = "Phylum", palette = c("chocolate", "firebrick", "gold", "forestgreen", "dodgerblue", "darkviolet", "darkseagreen", "navy", "aquamarine", "magenta"))

Also, as you can see in the plot, the legend is 3 columns wide. I would like to somehow just make it 2 columns and make the legend longer so, for example, Cyanobacteria and Desulfobacterota_I are moved to the first column and is spanning the length of the plot more.

I apologize about the confusion. E

— Reply to this email directly, view it on GitHubhttps://github.com/gmteunisse/fantaxtic/issues/39, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AHBWS3XEFZTBQRVXOM3FONDZMQ4XNAVCNFSM6AAAAABK5KJHUKVHI2DSMVQWIX3LMV43ASLTON2WKOZSGQYDSNRWHA3TKNI. You are receiving this because you are subscribed to this thread.Message ID: @.***>

bark9299 commented 4 months ago

Thank you for your quick reply @gmteunisse.

Just to clarify, When I make a custom palette for example: pal <- taxon_colours(top_nested_fam$ps_obj, tax_level = "Phylum", palette = c("#f0008c", "#ff8e3e", "#FDF950", "forestgreen", "dodgerblue", "darkviolet", "#ff6f69", "navy", "magenta", "#00f064"))

I get the following plot: c6fc11ed-4c15-4bbe-a939-877fe5812c7a

The color I choose for Bacteroidota is a bright orange, however, when it is plotted it looks brown. But, When I choose the color "#ff6f69" for Firmicutes_D, it is showing the correct color. Why is this? Is this something I cant change? I apologize for the confusion, I am new to plotting with R :)

gmteunisse commented 4 months ago

The package needs to generate gradients for each colour that you provide. To maximise distinctiveness, it attempts to evenly space shades and tints within a range around your provided colour. Unfortunately, the base colour itself is not necessarily one of the colours that is sampled. If you want to have more control, you have to follow the advanced usage section, and alter the ggnested parameters using the example here https://github.com/gmteunisse/ggnested?tab=readme-ov-file#gradients.


From: bark9299 @.> Sent: Tuesday, July 16, 2024 7:58 PM To: gmteunisse/fantaxtic @.> Cc: gmteunisse @.>; Mention @.> Subject: Re: [gmteunisse/fantaxtic] Need help with formatting plot (Issue #39)

Thank you for your quick reply @gmteunissehttps://github.com/gmteunisse.

Just to clarify, When I make a custom palette for example: pal <- taxon_colours(top_nested_fam$ps_obj, tax_level = "Phylum", palette = c("#f0008c", "#ff8e3e", "#FDF950", "forestgreen", "dodgerblue", "darkviolet", "#ff6f69", "navy", "magenta", "#00f064"))

I get the following plot: c6fc11ed-4c15-4bbe-a939-877fe5812c7a.png (view on web)https://github.com/user-attachments/assets/d1b1aa3c-c9e7-487a-ad99-444bc6c8cfa4

The color I choose for Bacteroidota is a bright orange, however, when it is plotted it looks brown. But, When I choose the color "#ff6f69" for Firmicutes_D, it is showing the correct color. Why is this? Is this something I cant change? I apologize for the confusion, I am new to plotting with R :)

— Reply to this email directly, view it on GitHubhttps://github.com/gmteunisse/fantaxtic/issues/39#issuecomment-2231497862, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AHBWS3UQMH7VJRHV6VEP523ZMVNLBAVCNFSM6AAAAABK5KJHUKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMZRGQ4TOOBWGI. You are receiving this because you were mentioned.Message ID: @.***>

gmteunisse commented 4 months ago

I've just updated ggnested so that you can have more control over the look of the gradients. You will have to specify type = 'fancy' in your call to ggnested. Please see the examples here for a quick insight into what is possible: https://github.com/gmteunisse/ggnested?tab=readme-ov-file#fancy-gradients