3

Is there a way to display both size and color in the legend when using geom_point?

library(ggplot2)
cons2 <- data.frame(
  value_date  = as.Date(c('2013-04-30', '2013-04-30', '2013-06-13', '2013-06-13')),
  ticker = c('AAPL','FTW','AAPL','FTW'),
  discount = c(0.34,0.10,0.25,0.20),
  b = c(0.40,0.55,.60,0.90),
  yield = c(0.08,0.04, 0.06,0.03)
)

p <- ggplot(cons2)
p <- p + geom_point(aes(yield,b, size = discount, color=value_date))
p

This plot will only show size(discount) in the legend, but I would like to display both size(discount) and color(value_date).

hadley
  • 102,019
  • 32
  • 183
  • 245
Børge Klungerbo
  • 177
  • 3
  • 11

2 Answers2

3

ggplot2 doesn't know quite what to do with the Date class. Try:

color=factor(value_date)

instead.

joran
  • 169,992
  • 32
  • 429
  • 468
1

For some reason ggplot2 isn't automatically figuring out that it needs the date frame. Try telling it explicitly:

ggplot(cons2) +
  geom_point(aes(yield, b, size = discount, color = value_date)) +
  scale_colour_gradient(trans = "date")
hadley
  • 102,019
  • 32
  • 183
  • 245