Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9028643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:02:22+00:00 2026-06-16T07:02:22+00:00

I have a customized function that I use for all my plots instead of

  • 0

I have a customized function that I use for all my plots instead of the plain ggplot:

my_ggplot <- function(...){
    ggplot(...) +
    theme_bw() +
    scale_colour_manual(values=get_palette(20)) +
    theme(axis.text.x=element_text(size=15),
          axis.text.y=element_text(size=15),
          axis.title.y=element_text(vjust=-.5, size=15, face="bold"),
          axis.title.x=element_text(vjust=-.5, size=15, face="bold"),
          plot.title=element_text(vjust=1, size=18, face="bold"),
          legend.title=element_text(size=15, face="bold"),
          plot.margin=unit(c(2,1,2,2), "lines")) # T R B L
}

Now I can do something like:

my_ggplot(molten_df, aes(timepoint,count,group=gene_symbol)) +
    geom_line(aes(color=gene_symbol), lwd=1.5)

This gives me decent label font sizes and my own color scale. Unfortunately I have to manually encode the palette size (20) to my get_palette function, which returns a different palette depending on the number I pass it.

My question: is there a way to infer the number of levels that the color aesthetic will have so my_ggplot doesn’t crash when I need to plot 21 colors? I guess this is the way ggplot does it, but I can’t seem to find the relevant function in their source code.

Answered by @user946850
This is what I ended up doing:

my_ggplot <- function(...){
    ggplot(...) +
    theme_bw() +
    discrete_scale("colour", "my_scale", dyn_palette()) +
    theme(axis.text.x=element_text(size=15),
          axis.text.y=element_text(size=15),
          axis.title.y=element_text(vjust=-.5, size=15, face="bold"),
          axis.title.x=element_text(vjust=-.5, size=15, face="bold"),
          plot.title=element_text(vjust=1, size=18, face="bold"),
          legend.title=element_text(size=15, face="bold"),
          plot.margin=unit(c(2,1,2,2), "lines")) # T R B L
}

dyn_palette <- function(){
  function(n){
    get_palette(n)
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:02:24+00:00Added an answer on June 16, 2026 at 7:02 am

    I’m afraid scale_colour_manual isn’t the way to go here. Let’s take a look behind the scenes: How does it work for, say, the Brewer scales?

    If you look at the definition of scale_colour_brewer, you will see that it is just a call to

    discrete_scale("colour", "brewer", brewer_pal(type, palette), ...)
    

    Now let’s take a look at brewer_pal (in the scales package):

    > brewer_pal
    function (type = "seq", palette = 1) 
    {
        pal <- pal_name(palette, type)
        function(n) {
            brewer.pal(n, pal)[seq_len(n)]
        }
    }
    

    Interesting, right? A function that returns a (bound) function with one argument n — that’s where the number of levels is going to be passed when the plot is created. In turn, brewer.pal (in RColorBrewer) just returns a list of colors:

    > brewer.pal(3, 'YlOrRd')
    [1] "#FFEDA0" "#FEB24C" "#F03B20"
    

    You should be able to achieve just the same with your custom palette by following this pattern.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have customized jqGrid's edit form to have some fields that use jQuery's DatePicker
I have a customized .profile that I use in ksh and below is a
I have a customized show/hide toggle script that I'm using along with CSS3 transitions
I have a customized version of SDL 1.2 and pygame 1.9.1 that implement SDL2's
I am using SharePoint 2010 and have a customized newform.aspx and editform.aspx that I
Is is possible ot configure Chainsaw to read log files that have been customized
I've been looking at a lot of iOS user interfaces that have been customized.
So that you don't have to read all of the below, the crux of
I'm working with an MS-SQL database with tables that use a customized date/time format
I have a form with two places that use ajax to submit the information

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.