0

There are similar question answered here, but not for my particular issue.

Let's have a data frame and ggplot bar:

d <- data.frame(
  letters = LETTERS[1:10],
  numbers = 11:20
)

ggplot(d, aes(x = letters, y = numbers, fill = letters)) +
  geom_bar(stat = "identity")

I need to show only A and J items in legend. I can use this code, but the gradient palette is broken completely and I don't know how to put it back.

ggplot(d, aes(x = letters, y = numbers, fill = letters)) +
  geom_bar(stat = "identity") +
  scale_fill_manual(breaks = c("A", "J"), values = d$letters)

Do you know it please?

Similiar questions:

Community
  • 1
  • 1
Marek L.
  • 323
  • 3
  • 11
  • Here is a question which answers your issue: http://stackoverflow.com/questions/33697211/select-ggplot-legend-items-and-preserve-palette-colors – Heroka Nov 13 '15 at 16:25

1 Answers1

4

You have to specify scale_fill_discrete your code should therefore look like this:

ggplot(d, aes(x = letters, y = numbers, fill = letters)) +
  geom_bar(stat = "identity") +
  scale_fill_discrete(breaks = c("A", "J"))

Plot

David
  • 9,216
  • 4
  • 45
  • 78