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 8639189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:56:23+00:00 2026-06-12T10:56:23+00:00

I have a data set that documents the energy use of three buildings. I

  • 0

I have a data set that documents the energy use of three buildings. I have a melted data frame that can be mimicked from the diamonds set:

data <- melt(diamonds[,c('depth','table','cut','color')],id=c('cut','color'))

Essentially, I have heating (‘depth’) and cooling (‘table’) data for each month(‘cut’) from three different buildings (7 ‘color’ factors). I would like to plot the three buildings (7 ‘color’ factors) side by side in a bar plot for each month (‘cut’).

I want the bars representing either cooling (‘table’) or heating (‘depth’) to vary their shade based on the building (‘color’ factor) while remaining grouped by month (‘cut’). This is a bad way to visualize the diamonds data, but should work well for buildings as their heating and cooling months typically don’t overlap. So far I have:

p <- ggplot(data,
        aes(color,value,group=cut))
p <- p + geom_bar(stat = 'identity',
              position = 'dodge',
              aes(fill = variable))
print(p)

I tried playing with scale_fill_manual, but couldn’t think of a strategy that works:

colours <- c('#0000FF', '#0033FF', '#0066FF', '#FF0000', '#FF3300', '#FF6600')

p <- p + scale_fill_manual(values = colours,
                           group = data$variable)
  • 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-12T10:56:25+00:00Added an answer on June 12, 2026 at 10:56 am

    With some trickery, it is possible. Deriving a data set based on diamonds was very good, but I want to work with a smaller data set

    set.seed(1234)
    data <-
    expand.grid(month = month.abb,
                building = c("Building A", "Building B", "Building C"),
                hc = c("Heating", "Cooling"))
    data$value <- rnorm(nrow(data), 60, 10)
    

    You want your fill colour to be based both on the variable (hc) and the building (building), so set it to that interaction.

    ggplot(data, aes(building,value,group=month)) + 
      geom_bar(stat = 'identity',
               position = 'dodge',
               aes(fill = interaction(building, hc)))
    

    enter image description here

    We can pick colors that represent different near shades to make these more like what you want. I used the middle of the “Blues” and “Reds” of the RColorBrewer palettes.

    colours <- c("#FC9272", "#FB6A4A", "#EF3B2C", "#9ECAE1", "#6BAED6", "#4292C6")
    # library("RColorBrewer")
    # colours <- c(brewer.pal(9,"Reds")[4:6], brewer.pal(9,"Blues")[4:6])
    

    and used scale_fill_manual to assign these colours.

    ggplot(data, aes(building,value,group=month)) + 
      geom_bar(stat = 'identity',
               position = 'dodge',
               aes(fill = interaction(building, hc))) +
      scale_fill_manual(values=colours)
    

    enter image description here

    The real trickery is in making the legend less convoluted. I just list 2 of the levels (the middle building in terms of color) and give them different names (and a different title for the legend).

    ggplot(data, aes(building,value,group=month)) + 
      geom_bar(stat = 'identity',
               position = 'dodge',
               aes(fill = interaction(building, hc))) +
      scale_fill_manual("Heating/cooling",
                        values=colours,
                        breaks=c("Building B.Heating", "Building B.Cooling"),
                        labels=c("Heating", "Cooling"))
    

    enter image description here

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

Sidebar

Related Questions

I have a large set of data that is generated from a web service
I have an app that builds a word document file from a data-set with
I have a data set that resembles this: id product_id size color price created_date
I have a data set that is around 700 rows with eight columns of
I have a data set that that I would like to call in a
I have the following data set that I am trying to plot with ggplot2,
I have a large data set that I'm working with in excel. About 1000+
I have a very large possible data set that I am trying to visualize
I have a data set of item difficulties that correspond to items on a
I have a set of data that contains garbled text fields because of encoding

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.