Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R ggplot remove certain items from legend [duplicate]

Tags:

r

ggplot2

Is it possible to remove certain items from a legend created with ggplot? I have a plot that is faceted, and point sizes provide another dimension to the plot. Since the plot is faceted I do not need to have certain legend items since it is explained by the facet titles, but the legend is still relevant for the point size.

In the plot below I would like to remove the "AREA" legend items since it is already explained by the faceting, but keep the "TOTAL_VOLUME" legend items that explain the point sizes.

a

Here is the code used to generate the plot:

library(data.table) # Import libraries
library(ggplot2)
library(scales)

set.seed(1234) # Set Seed

area.list <- LETTERS[seq(1:7)] # 7 Possible areas
date.list <- seq(as.Date("2014/03/01"), by="month", length=13)

# Build a random data set
data <- data.table(AREA = sample(area.list, 80, replace=TRUE),
               DATE = sample(date.list, 80, replace=TRUE),
               VOLUME = rnorm(n=80, mean=100000,sd=40000),
               NON_CONFORMING_VOLUME = rnorm(n=80, mean=30000,sd=5000))
# Summarise data by area and date
data <- data[, list(TOTAL_VOLUME=sum(VOLUME),
                TOTAL_NC_VOLUME=sum(NON_CONFORMING_VOLUME)),
         by=list(AREA, DATE)]
data$PERCENT_NC <- data$TOTAL_NC_VOLUME / data$TOTAL_VOLUME * 100

p <- ggplot(data = data, aes(x = DATE, 
                          y = PERCENT_NC,
                          colour = AREA)) +
  geom_point(aes(size = TOTAL_VOLUME)) +
  geom_line() +
  facet_grid(. ~ AREA) +
  theme(legend.position="bottom", axis.text.x=element_text(angle=90,hjust=1)) +
  ggtitle("Percent Non-Conforming by Area by Month") +
  labs(x = "Month", y = "% Non-Conforming") +
  scale_size_continuous(labels = comma)

plot(p)

I tried adding show_guide=FALSE to geom_point() but that removes both TOTAL_VOLUME and AREA.

Thank you

like image 867
David Dickson Avatar asked Apr 03 '15 20:04

David Dickson


1 Answers

You can set the guide for each scale in the following way:

p + guides(size = "legend", colour = "none")

plot

like image 130
rcs Avatar answered Oct 17 '22 14:10

rcs