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

  • SEARCH
  • Home
  • 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 8325147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:20:09+00:00 2026-06-09T00:20:09+00:00

I am trying to write some automated unit tests for a series of functions

  • 0

I am trying to write some automated unit tests for a series of functions that generates ggplot graphics.

For example, I want to set a specific colour scale to a plot. Now I need a way to determine whether the correct colour scale was actually applied.

The background:

Here is some example code, that set the fill colour to use the ColourBrewer palette Dark2:

p <- ggplot(mtcars, aes(x=factor(cyl), y=mpg, fill=factor(gear))) + 
  geom_bar(stat="identity") + 
  facet_grid(~gear) +
  scale_fill_brewer(palette="Dark2")

print(p)

enter image description here

OK, so a visual inspection tells me the code worked.

What I’ve tried:

Now I want to confirm this by inspecting the object:

str(p, max.level=1)
List of 8
 $ data       :'data.frame':    32 obs. of  11 variables:
 $ layers     :List of 1
 $ scales     :Reference class 'Scales' [package "ggplot2"] with 1 fields
  ..and 20 methods, of which 9 are possibly relevant
 $ mapping    :List of 3
 $ options    :List of 1
 $ coordinates:List of 1
  ..- attr(*, "class")= chr [1:2] "cartesian" "coord"
 $ facet      :List of 9
  ..- attr(*, "class")= chr [1:2] "grid" "facet"
 $ plot_env   :<environment: R_GlobalEnv> 
 - attr(*, "class")= chr "ggplot"

Fine, the $scales object seems interesting. Let’s look at that in more detail:

str(p$scales)
Reference class 'Scales' [package "ggplot2"] with 1 fields
 $ scales:List of 1
  ..$ :List of 14
  .. ..$ call      : language discrete_scale(aesthetics = "fill", scale_name = "brewer", palette = brewer_pal(type, palette))
  .. ..$ aesthetics: chr "fill"
  .. ..$ scale_name: chr "brewer"
  .. ..$ palette   :function (n)  
  .. ..$ range     :Reference class 'DiscreteRange' [package "scales"] with 1 fields
  .. .. ..$ range: NULL
  .. .. ..and 14 methods, of which 3 are possibly relevant:
  .. .. ..  initialize, reset, train
  .. ..$ limits    : NULL
  .. ..$ na.value  : logi NA
  .. ..$ expand    : list()
  .. .. ..- attr(*, "class")= chr "waiver"
  .. ..$ name      : NULL
  .. ..$ breaks    : list()
  .. .. ..- attr(*, "class")= chr "waiver"
  .. ..$ labels    : list()
  .. .. ..- attr(*, "class")= chr "waiver"
  .. ..$ legend    : NULL
  .. ..$ drop      : logi TRUE
  .. ..$ guide     : chr "legend"
  .. ..- attr(*, "class")= chr [1:3] "brewer" "discrete" "scale"
 and 20 methods, of which 9 are possibly relevant:
   add, clone, find, get_scales, has_scale, initialize, input, n, non_position_scales

But here I draw a blank. There is nothing inside p$scales that looks like either my input palette, or in fact like colours.

What I expect to find:

The colours that I would expect are:

library(RColorBrewer)
brewer.pal(3, name="Dark2")
[1] "#1B9E77" "#D95F02" "#7570B3"

The question:

How do I interrogate a ggplot object for specific fill colours to use?

  • 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-09T00:20:11+00:00Added an answer on June 9, 2026 at 12:20 am

    Try building the plot,

    g <- ggplot_build(p)
    unique(g$data[[1]]["fill"])
    
          fill
    1  #1B9E77
    16 #D95F02
    28 #7570B3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to write some unit tests for a couple of merthods that are
I am currently trying to write some automated tests within instruments and am having
I am currently trying to write some code that will accept some FTP details,
I'm trying to write some convenience functions in Scala for reading in arrays of
I am trying to write some code that will generate accurate .proto files from
I'm trying to write automated test, to ensure that the installer for my program
I am trying to write Watin tests for an intranet application that uses Integrated
I've got a .NET 3.5 class lib that I am trying to write some
I am trying to write some simple code which will read a text file
I'm trying to write some html in the Html tab of TinyMCE while editing

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.