Devon O'Rourke
Devon O'Rourke

Reputation: 267

changing ggplot legend unit scale

This question is motivated by a previous post illustrating various ways to change how axes scales are plotted in a ggplot figure, from the default exponential notation to the full integer value (when ones axes values are very large). While I am able to convert the axes scales from exponential notation to full values, I am unclear how one would achieve the same goal for the values appearing in the legend.

While I understand that one can manually change the length of the legend scale with "scale_color..." or "scale_fill..." followed by the "limits" argument, this does not appear to be a solution to getting my legend values to show "6000000000" rather than "6e+09" (or "0" rather than "0e+00" for that matter).

The following example should suffice. My hope is someone can point out how to implement the 'scales' package to apply for legend scales rather than axes scales.

Thanks very much.

library(ggplot2)
library(scales)
Data <- data.frame(
  pi = c(2,71,828,1828,45904,523536,2874713,52662497,757247093,6999595749),
  e = c(3,14,159,2653,58979,311599,7963468,54418516,1590576171, 99),
  face = 1:10)
p <- ggplot(data = Data, aes(x=face, y=e, colour = pi))
myplot <- p + geom_point() +
  scale_y_continuous(labels = comma) +
  scale_color_gradientn(colours = rainbow(2), limits=c(0,7000000000))
myplot

Upvotes: 1

Views: 11341

Answers (1)

Edgar Santos
Edgar Santos

Reputation: 3504

Use the Comma formatter in scale_color_gradientn by setting labels = comma e.g.:

p <- ggplot(data = Data, aes(x=face, y=e, colour = pi))
myplot <- p + geom_point() +
  scale_y_continuous(labels = comma) +
  scale_color_gradientn(colours = rainbow(2), limits=c(0,7000000000), labels = comma)
myplot

enter image description here

Upvotes: 6

Related Questions