Home > Software design >  R scale_fill_manual how to indicate three colors
R scale_fill_manual how to indicate three colors

Time:11-02

I've been using the brewer.pal(11, "Spectral")[1:3] to color my bar plot. However, I'm not able to select non-contiguous colors on the scale e.g. I want the first, second and eighth colors on the Spectral scale but I don't know how to get them..., the option with the array only allows me to indicate two values.

This colors will then be the filling for my bars, see belowenter image description here Basically, I would like to keep the first two column bars of the same tone and change the third one using the 8th color from the scale Spectral (should be a greenish tone). Code below

library(readxl)
library(scales)
library(ggdark)
library(ggpubr)
library(ggplot2)
library(forcats)
library(reshape2)
library(RColorBrewer)

excel_pggb <- read_excel("/media/mat/Extreme SSD/TheUniversityOfFerrara/2°Year/pangenome_assembly_R.xlsx") # PGGB tab performance
## excel_mcCACTUS <- read_excel("/media/mat/Extreme SSD/TheUniversityOfFerrara/2°Year/pangenome_assembly_R.xlsx", 2) # mc-CACTUS tab performance

df_pggb <- data.frame(excel_pggb) # PGGB dataframe
## df_mcCACTUS <- data.frame(excel_HG005) # mc-CACTUS dataframe

df_pggb$tool_used <- factor(df_pggb$tool_used, levels=c('wfmash', 'seqwish', 'smoothxg')) # order of tools pggb
df_pggb$aspect <- factor(df_pggb$aspect, levels=c('real_timeH', 'cpu_timeH', 'peak_memGB')) # order of aspects pggb
## df_mcCACTUS$tool_used <- factor(df_mcCACTUS$tool_used, levels=c('', '', '', '')) # order of tools mc-CACTUS
## df_mcCACTUS$aspect <- factor(df_mcCACTUS$aspect, levels=c('real_timeH', 'cpu_timeH', 'peak_memGB')) # order of aspects pggb
df2 <- with(df_pggb, df_pggb[order(No_of_threads, tool_used, aspect),]) # collapse threads based on number used (PGGB)
## df3 <- with(df_mcCACTUS, df_mcCACTUS[order(No_of_threads, tool_used, aspect),]) # collapse threads based on number used (mc-CACTUS)

### PLOT performance of different tools of the PGGB builder
test_on_pggb <- 
  ggplot(df_pggb, aes(x=No_of_threads, y=value, fill=aspect))   geom_col(position='dodge', width=.6, alpha=.6, color='black')
  ggplot(df2, aes(x=tool_used, y=value, fill=aspect))   geom_col(position='dodge', width=.6, alpha=.6, color='black')   facet_wrap(~No_of_threads, scales='free', nrow=1)   theme_bw()  
    
    theme(plot.title=element_text(face='bold.italic', hjust=.5), legend.title=element_text(face='italic'), legend.position='bottom', legend.direction='horizontal')   
    scale_fill_manual(values=brewer.pal(11, "Spectral")[1:8], labels=c('real_time_hours', 'CPU/h', 'RAM in GB'))   ggtitle('PGGB_performance_by_tool')   
    guides(fill=guide_legend(title='execution_aspect', title.position='top', title.hjust=.5))

I tried several combination of numbers to fit in the array separated by comma or column but, unfortunately, none of them worked...

CodePudding user response:

Use c() to specify the indices in a vector:

brewer.pal(11, "Spectral")[c(1, 2, 8)]

[1] "#9E0142" "#D53E4F" "#ABDDA4"
  • Related