0
stat_density2d(aes(fill = ..level..), alpha = .5, h = .02, n = 300, 
   geom = "polygon", data = accident_before) + 
scale_fill_gradient(low = "green", high = "red", guide = "colorbar", 
  name = "Level", breaks=seq(-40,80,30), limits=c(-40,80)) 

This is the code I am using but the output of this is a map that plots the city in grey color. What do low and high specify here? How should I understand which dataset is considered to output the map in what color.

tjebo
  • 21,977
  • 7
  • 58
  • 94

1 Answers1

0

you are setting your scale limits to be -40 to 80, thus those will be the high and low colors. If you have grey values, this is either because you have NA values, or you have values out of this range.

Check ?ggplot2::scale_fill_gradient for a bit more information.

The underlying gradient is calculated with scales::seq_gradient_pal()

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Oh thank you so much! What would be the best way for me to get rid of the NA values? – Alicewonderland Jan 22 '21 at 17:02
  • @Alicewonderland google helps... https://stackoverflow.com/questions/4862178/remove-rows-with-all-or-some-nas-missing-values-in-data-frame. Note that NAs are not per se *bad*, as a matter of fact, they are *good*. You need to very carefully consider if you really want to remove NA before doing so – tjebo Jan 22 '21 at 17:16
  • I have tried to remove the NA's and it does not show me any NA values when I use any(is.na(boston2[])) it gives me FALSE. But I still have NA's on my map. Is there anything else I can do? – Alicewonderland Jan 24 '21 at 17:17
  • @Alicewonderland really difficult to help without data in this case..... have you checked the range of your values? – tjebo Jan 24 '21 at 18:40