Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 scale_fill_gradient() function not changing point colors R

I'm using ggplot2 in R to create maps. In the past, I have been able to successfully use the scale_fill_gradient() function to control geom_point fills. However, when I run the code below (with example table provided), I simply get a map of all black points. The legend appears correct, but the points never change color. I think my desired variable is not mapping to the fill aesthetic, but I cannot figure out why. Thank you in advance!

(if it matters, I am using tibble package to define tables)

table = tibble(long = c(15.28, 15.29, 15.3, 15.31, 15.32), lat = c(-4.4, -4.39, -4.38, -4.37, -4.36), consumption = c(NA, 3, 54, 6, 8))

mapping = aes_string(x = 'long', y = 'lat', fill = 'consumption')

# define breaks, limits, colors

low = 'seashell'
high = 'tan3'
breaks = c(0, max(na.omit(table)[['consumption']]))
limits = breaks

# plot 

p <- ggplot() +
  # points 
  geom_point(mapping = mapping, data = table, alpha = 0.7, size = 4) +
  # point colors
  scale_fill_gradient(low = low, high = high, na.value = 'darkgrey', guide = 'colorbar', aesthetics = 'fill'
                      , breaks = breaks, limits = limits) +
  # title
  ggtitle('consumption') +
  # title formatting
  theme(plot.title = element_text(color = "red", size = 10, face = "bold", hjust=0),
        legend.position="bottom",
        legend.text=element_text(size=9),
        legend.title=element_text(size=9)) +
  # legend
  guides(fill=guide_colorbar(title='consumption')) +
  # get rid of axes, etc.
  theme(axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()) +
  xlab('') +
  ylab('') +
  # make legend correct
  theme(legend.box = 'vertical') +
  # add max/min corresponding to map
  xlim(c(15.28, 15.38)) +
  ylim(c(-4.41, -4.30))
like image 409
Dan C Avatar asked Oct 21 '25 15:10

Dan C


1 Answers

As mentioned in the comments you have to change the fill to color. Here is how I achieved it:

library(tidyverse)
table = tibble(long = c(15.28, 15.29, 15.3, 15.31, 15.32), lat = c(-4.4, -4.39, -4.38, -4.37, -4.36), consumption = c(NA, 3, 54, 6, 8))

##Changed here to color
mapping = aes_string(x = 'long', y = 'lat', color = 'consumption') 

# define breaks, limits, colors

low = 'seashell'
high = 'tan3'
breaks = c(0, max(na.omit(table)[['consumption']]))
limits = breaks

# plot 

ggplot() +
    # points 
    geom_point(mapping = mapping,
               data = table, alpha = 0.7, size = 4) +
    # point colors
    #Change here to aesthetics = color
    scale_color_gradient(low = low, high = high, na.value = 'darkgrey', guide = 'colorbar', aesthetics = 'color'
                        , breaks = breaks, limits = limits) +
    # title
    ggtitle('consumption') +
    # title formatting
    theme(plot.title = element_text(color = "red", size = 10, face = "bold", hjust=0),
          legend.position="bottom",
          legend.text=element_text(size=9),
          legend.title=element_text(size=9)) +
    # legend
    guides(fill=guide_colorbar(title='consumption')) +
    # get rid of axes, etc.
    theme(axis.line = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank()) +
    xlab('') +
    ylab('') +
    # make legend correct
    theme(legend.box = 'vertical') +
    # add max/min corresponding to map
    xlim(c(15.28, 15.38)) +
    ylim(c(-4.41, -4.30))

enter image description here

like image 51
Henry Cyranka Avatar answered Oct 23 '25 05:10

Henry Cyranka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!