Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change scientific notation on legend labels in ggplot2

Tags:

r

ggplot2

I wrote this code to create a map.

ggplot(data = Canada2015_Import_3) +
  borders(database = "world", 
          colour = "grey60",
          fill="grey90") + 
  geom_polygon(aes(x=long, y=lat, group = group, fill = Trade_Value_mean),
               color = "grey60") +
  scale_fill_gradient(low = "blue", high = "red", name = "Trade Value") + 
  ggtitle("Canadien Imports in 2015") + 
  xlab("") + ylab("") + 
  theme(panel.background = element_blank(),
        plot.title = element_text(face = "bold"),
        axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())

This map gives me a legend with scientific notation, and I would like to change it to normal or with commas.

enter image description here

Does anybody know how to do that?

Here is the basic structure of my data frame.

Country   Trade_Value_mean  long      lat     group order subregion
Afghanistan    2359461     74.89131 37.23164     2    12      <NA>

All help is appreciated.

like image 610
Julien Avatar asked Oct 16 '16 17:10

Julien


People also ask

How do I get rid of scientific notation in ggplot2?

The scale_x_continuous() and scale_y_continuous() methods can be used to disable scientific notation and convert scientific labels to discrete form.

How do I change the legend text in ggplot2?

You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))

How do I change the title of a legend in ggplot2?

Method 1: Change Legend Title using guides() Function. Now if we want to change Legend Title then we have to add guides and guide_legend functions to the geom_point function. Inside guides() function, we take parameter named 'color' because we use color parameter for legend in ggplot() function.

How do I add a legend in ggplot2?

You can place the legend literally anywhere. To put it around the chart, use the legend. position option and specify top , right , bottom , or left . To put it inside the plot area, specify a vector of length 2, both values going between 0 and 1 and giving the x and y coordinates.


2 Answers

I figured it out. Basically all you have to do is insert the scales library and add labels = comma. Here's the modified code :

library(scales) 

ggplot(data = Canada2015_Import_3) +
  borders(database = "world", 
          colour = "grey60",
          fill="grey90") + 
  geom_polygon(aes(x=long, y=lat, group = group, fill = Trade_Value_mean),
               color = "grey60") +
  scale_fill_gradient(low = "blue", high = "red", name = "Trade Value", labels = comma) + 
  ggtitle("Canadien Imports in 2015") + 
  xlab("") + ylab("") + 
  theme(panel.background = element_blank(),
        plot.title = element_text(face = "bold"),
        axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())
like image 174
Julien Avatar answered Sep 29 '22 11:09

Julien


you can also use at the beginning of your code:

options(scipen=10000)
like image 43
Andy White Avatar answered Sep 29 '22 11:09

Andy White