-2

I am trying to graph a grouped barplot, where the x-axis contains different states, and within each state, I have values for winter, spring, summer, fall. Basically, on the x-axis, I have 5 states, and each stage is a cluster of bars, and within each cluster I have 4 bars, one for each of winter, spring, summer, fall.

I want to vary the color of the 4 vars in each cluster so that they are different shades of the same color. Say I pick the color blue, I want winter to be the darkest shade of blue and fall to the be lightest shade of blue. I also, want the code to be flexible so it can accomodate more than 4 values within each cluster.

Here is the code I'm working with

# fill = different categories within a group
# x = grouping vars
# y = value (height of bar)
xvar = rlang::sym('states') #grouping var
yvar = rlang::sym('pct_aware')
xlabel = ('state')
ylabel = ('% of respondents')
graphtitle = 'awareness by segmentation'

ylim_max = 100

withingroupvar = rlang::sym('seasons')
legendtitle = "seasons"

p <- ggplot(data = graphinput, aes(x = factor(!!xvar, levels = jobboards), y = !!yvar,
                                   fill = factor(!!withingroupvar, levels =segorder_text))) 
p + geom_bar(stat = "identity",
             position = position_dodge(0.9)) +
  labs(x = xlabel, y = ylabel) +
  ggtitle(graphtitle) +
  geom_text(aes(label=round(!!yvar,0)), vjust=-0.6, color="black",
            position = position_dodge(0.9), size=4) + 
  scale_fill_manual(name = legendtitle, 
                    values = c('winter'="pink", 'spring' = "aquamarine", 'summer' = "lightblue", 'fall' = "salmon1"),
                    breaks=segorder_text) +
  theme(text = element_text(size = 15))  + ylim(0,ylim_max)

Right now I'm manually assigning color values to each of the 4 seasons. I would like this to be automatic--I only have to pick the base color and R would assign different shades of the same color to each of the seasons.

How should I modify my code to achieve this?

Amazonian
  • 391
  • 2
  • 8
  • 22
  • 3
    We can't do much with your code if you don't [provide any data](https://stackoverflow.com/a/5963610/6574038). – jay.sf Nov 25 '18 at 09:14

1 Answers1

2

I would define a color palette

#define color palette
color_palette <- colorRampPalette(colors = c("steelblue", "darkblue"))(length(segorder_text));

and I would refer to this color palette in scale_fill_manual.

... scale_fill_manual(name = legendtitle, values = color_palette) + ...

Then your code would look like this:

# fill = different categories within a group
# x = grouping vars
# y = value (height of bar)
xvar = rlang::sym('states') #grouping var
yvar = rlang::sym('pct_aware')
xlabel = ('state')
ylabel = ('% of respondents')
graphtitle = 'awareness by segmentation'

ylim_max = 100

withingroupvar = rlang::sym('seasons')
legendtitle = "seasons"

#define color palette
color_palette <- colorRampPalette(colors = c("steelblue", "darkblue"))(length(segorder_text));

p <- ggplot(data = graphinput, aes(x = factor(!!xvar, levels = jobboards), y = !!yvar,
                                   fill = factor(!!withingroupvar, levels =segorder_text))) 
p + geom_bar(stat = "identity",
             position = position_dodge(0.9)) +
  labs(x = xlabel, y = ylabel) +
  ggtitle(graphtitle) +
  geom_text(aes(label=round(!!yvar,0)), vjust=-0.6, color="black",
            position = position_dodge(0.9), size=4) + 
  scale_fill_manual(name = legendtitle, values = color_palette) +
  theme(text = element_text(size = 15))  + ylim(0,ylim_max)
Franziska W.
  • 346
  • 2
  • 4