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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:26:35+00:00 2026-06-06T19:26:35+00:00

I need to assign a specific color to a bar in ggplot bar plots.

  • 0

I need to assign a specific color to a bar in ggplot bar plots. By default I am getting the below ggplot bar plot. Here, what I wanted is to assign a different color (say red) to the higuest value bars. That is for the bars under 64 items with type D for the names X and Z. And also red color for the bar under 16 items with type C for the name Y. Currently I am using the R ggplot2 code shown below the figure. Please help me to get this.

Data:

data <- structure(list(type = structure(c(1L, 2L, 3L, 4L, 2L, 3L, 4L, 
    3L, 4L, 4L, 1L, 2L, 3L, 4L, 2L, 3L, 4L, 3L, 4L, 4L, 1L, 2L, 3L, 
    4L, 2L, 3L, 4L, 3L, 4L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"), 
    items = c(16L, 16L, 16L, 16L, 32L, 32L, 32L, 48L, 48L, 64L, 
    16L, 16L, 16L, 16L, 32L, 32L, 32L, 48L, 48L, 64L, 16L, 16L, 
    16L, 16L, 32L, 32L, 32L, 48L, 48L, 64L), name = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("X", 
    "Y", "Z"), class = "factor"), value = c(6.3, 7.2, 7.1, 7, 
    10, 10.9, 11, 11.6, 11.5, 12, 8.9, 10.6, 11.5, 7.9, 12.6, 
    13.5, 12.4, 13, 14, 15.4, 4.8, 7, 7.2, 6.1, 4.1, 4.4, 4.2, 
    3.2, 2.7, 1.6)), .Names = c("type", "items", "name", "value"
    ), class = "data.frame", row.names = c(NA, -30L))

enter image description here

data$name = factor(data$name, levels(data$name)[c(1,3,2)])
p1 = ggplot(data, aes(type, value, fill=type, group=items, facets=items)) + geom_bar(stat="identity") 
p1 = p1 +  scale_fill_manual(values = rep("grey60",4), labels = c("type-A", "type-B", "type-C", "type-D"))
p1 = p1 + facet_grid(name~items,scales="free", space="free") + opts(strip.text.y = theme_text(size=14, face="bold"))
p1 = p1 + opts(legend.position="top", legend.text=theme_text(size=15), legend.title=theme_text(size=0,colour="white"), legend.key = theme_rect(colour = NA))

UPDATE

Although the code provided by mnei is providing almost what I need, I am not getting the legend shown in the Figure. Please let me know how can I get that.

  • 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-06T19:26:36+00:00Added an answer on June 6, 2026 at 7:26 pm

    Calculate which type is the maximum for each combination

     library(plyr)
     data <- ddply(data, .( items, name), mutate, is_max = type == type[which.max(value)])
    
    ## the plot
    ggplot(data, aes(x = type, y = value, fill = is_max, group = items)) + 
      facet_grid(name ~ items, scale = 'free', space = 'free') + 
      geom_bar(stat = 'identity') +  
      scale_fill_manual(values = c('black', 'red'), labels = c('other', 'maximum')) +
      opts(legend.position = "top", legend.text = theme_text(size = 15),  
           legend.title = theme_text(size = 0,colour = "white"), 
           legend.key = theme_rect(colour = NA))
    

    Or with the legend as requested

     ggplot(data, aes(x=type, y= value, group = items, fill = type)) +  
      facet_grid(name~items, scale = 'free', space = 'free') + 
      geom_bar(stat= 'identity')  +
      geom_bar(data = data[data$is_max,],stat = 'identity', fill = 'red' ) +
      scale_fill_manual(values = rep("grey60",4), 
                        labels = c("type-A", "type-B", "type-C", "type-D")) + 
      opts(strip.text.y = theme_text(size=14, face="bold")) + 
      opts(legend.position="top", legend.text=theme_text(size=15), 
           legend.title=theme_text(size=0,colour="white"), 
           legend.key = theme_rect(colour = NA))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to assign values to properties through reflection, and it has been OK
I need to assign an active class to my main-level navigation rendered by wordpress.
I need to assign a variable which will be used to create the id
I am exporting datatable in MSEXCEL.I need to Assign MultipleLines in a cell in
A TreeNode class has Text Name Tag I need to assign more values to
What I want to do is following. Inside a function, I need to assign
I need to get UserCarId(int) from this query and assign to int type variable.
I need to find the text 'ifeq ($(Param1)' using grep. I try to assign
How can I assign processes to specific cores ?. I have a 16 core
From an application I need to develop, I've received a specific font that has

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.