0

I am creating multiple series of plots for different vegetation types and using filter to subset the individual veg type being plotted. These are bar graphs and I'd like to use the same palette of colors to indicate successional class, but use more descriptive labels in the legend. I have 1 column for the x variable/legend labels and another for the symbol colors.

I used the symbol column in aes for fill, but this is causing the legend labels to default to the symbol values rather than the x variable values. When I use scale_fill_manual() to assign the label column (df$symbol) it pulls in values from outside of the subset range. Is there a way to use the x variable column for my legend labels without manually entering them?

Thank you!

Example
make <- c('volvo','volvo','volvo','volvo',
'mazda','mazda','mazda','mazda',
'toyota','toyota','toyota','toyota')

label <- c('alpha','beta','charlie','delta',
'echo','foxtrot','golf','hotel',
'juliett','kilo','lima','mike')

symbol <- rep(c('a','b','c','d'), 3)
length <- sample(40, size = 12, replace = TRUE)

df <- data.frame("make" = make, "length" = length, "label" = label, "symbol" = symbol)

Symbol_col = c(a ='grey', b ='red', c = 'green', d = 'gold')

ggplot(filter(df, make == 'volvo'), aes(x = label, y = length, fill= symbol)) +
  geom_col() +
  scale_fill_manual(values = Symbol_col)
  • 2
    Can yo u show a small reproducible example with dput – akrun May 20 '21 at 18:59
  • *"When I use scale_fill_manual() to assign the label column (df$symbol) it pulls in values from outside of the subset range."* You might just need the `drop = TRUE` argument. You could also potentially pass a function to `labels` that looks up the right label based on the `symbol` column. As akrun says, a reproducible example would help a lot. – Gregor Thomas May 20 '21 at 19:09
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 20 '21 at 19:12
  • I tried drop = TRUE but no luck. Your function approach sounds like it could work well, similar to using vlookup in excel. Do you recommend a function that might work? – emmaflowers Jun 08 '21 at 07:20

0 Answers0