3

I have a dataframe:

df1 <- data.frame(place = c("a", "a", "b", "b", "c", "c", "d", "d", "e", "e", "f", "f", "g", "g", 
                         "h", "h", "i", "i", "j", "j", "k", "k", "l", "l", "m", "m", "n", 
                         "n", "o", "o", "p", "p", "q", "q"), 
               cost_other = c("cost_savings", "other_savings",
                            "cost_savings", "other_savings",
                            "cost_savings", "other_savings",
                            "cost_savings", "other_savings",
                            "cost_savings", "other_savings",
                            "cost_savings", "other_savings",
                            "cost_savings", "other_savings",
                            "cost_savings", "other_savings",
                            "cost_savings", "other_savings",
                            "cost_savings", "other_savings",
                            "cost_savings", "other_savings",
                            "cost_savings", "other_savings",
                            "cost_savings", "other_savings",
                            "cost_savings", "other_savings",
                            "cost_savings", "other_savings",
                            "cost_savings", "other_savings",
                            "cost_savings", "other_savings"), 
               values = c(1.8,0.55, 0.836, 1.06, 1.096, 1.83875908682016, 
                          0.966, 1.34497094648805, 1.62275, 0.600277163210231, 0.873875, 
                          0.875130680022367, 1.487, 0.283241350805961, 1.0776, 0.849116238360361, 
                          1.221, 1.45510685371131, 0.961882352941176, 1.30607084680655, 
                          1.027, 1.52026452067783, 0.807666666666667, 1.22377282341786, 
                          0.788384615384615, 0.984172521942087, 0.975, 1.94002090880358, 
                          1.00333333333333, 1.18402178405582, 0.8956, 1.16165422673896, 
                          0.95975, 0.825533052928448))

I want to create a facet-wrap-based geom_bar chart using these data, such that every faceted space shows all the bar charts in grey color. Then, I need the specific bars for specific grid to be in any color.

I tried doing it, but got something like this:

I used this to get my result:

ggplot(data=df,aes(x=place,
                 y=values))+
  geom_bar(aes(fill=cost_other),
       stat = "identity",
       position = "dodge")  +
 facet_wrap(~place) +
 theme_calc()+
 theme(legend.position = "none",
    axis.text.x=element_blank())

I am getting the specific bars in color, but then all the other spaces are empty. I want other bars for all variables as well, specifically in grey color.

neilfws
  • 32,751
  • 5
  • 50
  • 63
knick
  • 37
  • 4

1 Answers1

5

Using tidyr::expand_grid(), create a place_facet column with unique values of place and cross it with the initial data. This will give you an expanded dataframe that reproduces your original data once for each facet. Then plot two layers of bars: one in gray using all values, and another in color for just the values where place and place_facet match.

library(tidyr)
library(dplyr)
library(ggplot2)
library(ggthemes)

df1 %>%
  tidyr::expand_grid(place_facet = unique(.$place)) %>%
  mutate(values_facet = ifelse(place == place_facet, values, NA)) %>%
  ggplot(aes(x = place)) +
  geom_col(
    aes(y = values, group = cost_other), 
    position = "dodge",
    fill = "gray"
  ) +
  geom_col(
    aes(y = values_facet, fill = cost_other),
    position = "dodge",
    show.legend = FALSE
  ) +
  facet_wrap(~place_facet) +
  theme_calc() +
  theme(axis.text.x = element_blank())

zephryl
  • 14,633
  • 3
  • 11
  • 30
  • 1
    It worked like a charm. Thanks so much for such a quick response with an elegant solution. – knick Feb 21 '23 at 23:23