0

I have a bar graph that I made using ggplot2 that is colored both by color= and by fill=, and both of those aesthetics appear in my legend (see picture below). I am happy with how the color part of the legend is appearing, but for the fill part, instead of the boxes being white, I would like there to be a combination of both the red and the yellow, to indicate that the outline can be found on bars of either color. I was hoping that the boxes could be split down the middle diagonally, or something similar, with half of the "legend box" red and half of the "legend box" white. Right now, this is the code that I have for the coloring of my graph:

guides(color = guide_legend(override.aes = list(fill = "white")))

My current plot can be found at the following link:

enter image description here

Again, ideally, those white boxes would each be filed with the two colors, red and yellow, half of each box for each color. I have tried to make a vector of the two colors and assign that to the fill, but that just colors one box red and one box yellow.

A reproducible example, with an example dataset that mimics my own, is included below:

rpe <- data.frame(unit = rep(c(1, 2, 3, 4), times = 2),
                  value = c(8.33, 43.55, 15.63, 87.59, 91.67, 56.45, 84.37, 12.41),
                  category1 = (c(rep("Expected", times = 4), rep("Unexpected", times = 4))),
                  tag = c("AMPH", "APO", "APO", "AMPH", "AMPH", "APO", "APO", "AMPH")
)

ggplot(rpe, aes(unit, value, fill = category1, color = tag)) +
  geom_bar(position = "fill", stat = "identity") +
  scale_y_continuous(labels = percent) +
  scale_fill_manual(values=c("tomato", "darkgoldenrod1")) +
  scale_color_manual(values = c("black", NA)) +
  guides(color = guide_legend(override.aes = list(fill = "white")))

Any help would be very much appreciated! Thank you!

Joe North
  • 73
  • 5
  • 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 that can be used to test and verify possible solutions. – MrFlick May 07 '21 at 19:53
  • @MrFlick Suggestion much appreciated! Thank you! – Joe North May 07 '21 at 20:57

0 Answers0