David
David

Reputation: 779

r ggplot legend exponent control

The legend in my plot occasionally shows values for the size variable as 1e+05. My audience is unlikely to know right away that this is the same as 100000, so I would like the legend to say 100000, 80000, 60000, 40000, 20000. Is there anyway to control this exponent functionality in ggplot?

I have included the sample code with some fake data and an example image. You would have to re-run the code multiple times to get it to reproduce the problem shown.

enter image description here

library(data.table)
dt <- data.table(DATE = c("2014-03-01", "2014-04-01", "2014-05-01", 
                      "2014-06-01", "2014-07-01", "2014-08-01", 
                      "2014-09-01", "2014-10-01", "2014-11-01", 
                      "2014-12-01", "2015-01-01", "2015-02-01"
                      , "2015-03-01"),
             TOTAL_VOLUME = rnorm(n = 13, mean = 53000, sd = 25000),
             NON_CONFORMING_VOLUME = rnorm(n = 13, mean = 8400, sd = 6000))

# Switch any negatives to positive
dt$NON_CONFORMING_VOLUME <- abs(dt$NON_CONFORMING_VOLUME)
# Could have NON_CONFORMING > TOTAL, set arbitrarily to 30% of total
dt$NON_CONFORMING_VOLUME <- ifelse(dt$NON_CONFORMING_VOLUME > dt$TOTAL_VOLUME, 
                               dt$TOTAL_VOLUME * .3, dt$NON_CONFORMING_VOLUME)
dt$PERCENT_NON_CONFORMING <- dt$NON_CONFORMING_VOLUME / dt$TOTAL_VOLUME * 100

    p1 <- ggplot(data = dt, aes(x = DATE,
                        y = PERCENT_NON_CONFORMING, 
                        size = TOTAL_VOLUME)) +
  geom_point() +
  theme(legend.position="bottom") +
  ggtitle("Percent Non-Conforming by Month") +
  labs(x = "Month", y = "% Non-Conforming") +
  geom_hline(yintercept = mean(dt$PERCENT_NON_CONFORMING),
         colour = "darkorange")
plot(p1)

Upvotes: 0

Views: 1058

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 146040

I'm surprised, when running your code I don't get scientific notation, I get 20000, 40000 etc. as you ask for. That said, the fix is to specify the labels function within the size scale, and the scales package has nice options. Probably the comma format will be best with this data, then you'll get 20,000, 40,000...

library(scales)
plot(p1 + scale_size_continuous(labels = comma))

Upvotes: 1

Related Questions