Kreitz Gigs
Kreitz Gigs

Reputation: 379

Hide a value from the legend in R package ggplot2

I am trying to "hide" a value from the legend of a ggplot2 object. For example in the following example, I would try to hide the third value in the legend while keeping the actual data within the plot. Desired output would be the same plot but without the 8 cycle listed in the legend but with the data still within the plot.

ggplot(mtcars, aes(x=wt, y=mpg, group=as.factor(cyl))) +
  geom_point(aes(shape=as.factor(cyl), color=as.factor(cyl)))

I'm also trying to change the colors and shapes so that every other shape repeats and the colors are only black and white as follows

plot <- ggplot(mtcars, aes(x=wt, y=mpg, group=as.factor(cyl))) +
        geom_point(aes(shape=as.factor(cyl), color=as.factor(cyl)))

plot2 <- plot +
         scale_colour_manual(values = c('#999999', '#999999','#999999')) +   
         scale_shape_manual(values = c(0, 1, 0))

plot2

However when I then try to only keep the first two values within the legend it seems to overwrite the previous changes. For example:

plot2 +
scale_color_discrete(breaks=c(4, 6)) +
scale_shape_discrete(breaks=c(4, 6))    

Upvotes: 0

Views: 207

Answers (1)

Jon Spring
Jon Spring

Reputation: 66415

ggplot(mtcars, aes(x=wt, y=mpg, color=as.factor(cyl))) +
  geom_point(aes(shape=as.factor(cyl))) +
  scale_color_discrete(breaks=c(4, 6)) +
  scale_shape_discrete(breaks=c(4, 6))

Upvotes: 2

Related Questions