Ghose Bishwajit
Ghose Bishwajit

Reputation: 342

Using a different color for only the selected bar in geom_bar

I want to highlight the bar in the middle (named SSA) using a different color. I have tried the available examples, but not getting the desired result.

It seems I'm not being able to use the ifelse element correctly. Thanks for helping to detect the error.

"fill" is not a factor in my data, unlike in the solution provided here: change color of only one bar in ggplot

Data:

structure(list(yield = c(48, 33, 46, 44, 79, 20, 21, 8, 40, 72, 
12, 31, 65, 10, 71, 36, 20, 60, 69, 59, 58, 49, 75, 28, 71, 61, 
34, 39, 42, 64, 47, 36, 78, 73, 51, 46, 3, 55, 70, 80, 29, 45, 
70, 72, 32, 42, 48), df = c(2, 13, 0, 9, -3, 3, 2, 0, 2, 11, 
0, 0, 5, -2, -1, -15, 0, 2, 14, 1, 6, 2, -1, 2, 8, 16, 8, 0, 
-13, 3, 0, 10, 10, -3, 7, 0, -6, 16, 0, 1, -23, 9, 11, 12, 4, 
8, 28), country = c("Angola", "Benin", "Botswana", "Burkina Faso", 
"Burundi", "Cabo Verde", "Cameroon", "Central African Republic", 
"Chad", "Comoros", "Congo, Dem. Rep.", "Congo, Rep.", "Cote d'Ivoire", 
"Equatorial Guinea", "Eswatini", "Ethiopia", "Gabon", "Gambia, The", 
"Ghana", "Guinea", "Guinea-Bissau", "Kenya", "Lesotho", "Liberia", 
"Madagascar", "Malawi", "Mali", "Mauritania", "Mauritius", "Mozambique", 
"Namibia", "Niger", "Nigeria", "Rwanda", "Sao Tome and Principe", 
"Senegal", "Seychelles", "Sierra Leone", "Somalia", "South Africa", 
"Sudan", "Tanzania", "Togo", "Uganda", "Zambia", "Zimbabwe", 
"SSA")), row.names = c(NA, -47L), class = c("tbl_df", "tbl", 
"data.frame"))

My code:

ggplot(arble.land, 
       aes(x = reorder(country, yield), y = yield), 
       col = ifelse(country = "SSA", "Highlighted", "Normal")) + 
  geom_bar(stat = "identity") + 
  coord_flip()  

enter image description here

Upvotes: 4

Views: 6245

Answers (1)

KoenV
KoenV

Reputation: 4283

There are some issues with your code:

  • as @Z_Lin mentions, the color part should be in the call to aes.
  • in the if_else statement you should use a logical operator like ==
  • the col argument affects the line color of the bar, you might have preferred the fill argument

The following code:

ggplot(arble.land, 
       aes(x = reorder(country, yield), 
           y = yield, 
           fill = ifelse(country == "SSA", "Highlighted", "Normal") )) + 
  geom_bar(stat = "identity") + 
  ## drop legend and Y-axis title
  theme(legend.position = "none", axis.title.y = element_blank()) +
  coord_flip()  

yields this graph:

enter image description here

please let me know whether this is what you want.

Update

if you want to remove "extra space" on the left and right, you may use the expand argument in coord_flip like this

ggplot(arble.land, 
       aes(x = reorder(country, yield), 
           y = yield, 
           fill = ifelse(country == "SSA", "Highlighted", "Normal") )) + 
  geom_bar(stat = "identity") + 
  ## drop legend and Y-axis title
  theme(legend.position = "none", axis.title.y = element_blank()) +
  coord_flip(expand = FALSE) ########## SMALL UPDATE

yielding the following plot:

enter image description here

Update 2: Fill colors

You can set the colors manually with scale_fill_manual()

ggplot(arble.land, 
       aes(x = reorder(country, yield), 
           y = yield, 
           fill = ifelse(country == "SSA", "Highlighted", "Normal") )) + 
  geom_bar(stat = "identity") + 
  ## add manual color scale
  scale_fill_manual("legend",         ###### in this call: colour change
                    values = c("Highlighted" = "black", "Normal" = "orange")) +
  ## drop legend and Y-axis title
  theme(legend.position = "none", axis.title.y = element_blank()) +
  coord_flip(expand = FALSE) 

producing this plot:

enter image description here

Upvotes: 4

Related Questions