5

I am using following data set:

head(trip_3)
  TIP     LON    LAT
1   0    -73.866 40.741
2   10.0 -73.906 40.707
3   1.2  -73.706 40.738
4   2.0  -73.946 40.640
5   0    -73.946 40.625
6   1.5  -73.986 40.602

I was able to generate following map to present points with high and low average tip values: enter image description here

I have achieved it with following code:

nyc_map + geom_point(data=as.data.frame(trip_3), aes(x=LON, y=LAT, fill=TIP), size=3, 
shape=21, alpha=0.6, position="jitter") + scale_fill_continuous(low="white", high="#FF0033")

Now I want to get a map presenting areas (density) with high and low tips but not using points - I want to get something like this: enter image description here

But it counts amount of points, not bases on TIP value. It would be great to achieve what I have described earlier. It is the code I used:

nyc_map + stat_density2d(aes(x=LON, y=LAT, fill = ..level..), size=3,
bins=10, data=as.data.frame(trip_3), geom="polygon")

How to make stat_density2d rely on TIP, not amount of points?

  • 1
    [Check out this answer](http://stackoverflow.com/questions/24198514/ggplot2-modify-geom-density2d-to-accept-weights-as-a-parameter) as I think the weighted `kde2d` version is what you're really looking to use under the hood. As an aside, you'd likely get better/faster answers if you supply actual reproducible data/code for people to work with. – Forrest R. Stevens Jul 08 '15 at 17:18
  • Thanks it is working – Mateusz Cebula Jul 10 '15 at 07:32
  • 2
    So, if it is working, perhaps you could post the solution? – jflournoy Oct 20 '15 at 22:50
  • Possible duplicate of [Generating spatial heat map via ggmap in R based on a value](https://stackoverflow.com/questions/45319970/generating-spatial-heat-map-via-ggmap-in-r-based-on-a-value) – rafa.pereira Jul 28 '17 at 13:23

1 Answers1

2

Try geom_contour. It allows you to specify a third variable as z. geom_contour will draw lines at equal TIP areas like a topographic map.

nyc_map + geom_contour(aes(x=LON, y=LAT, z=TIP, fill=TIP), color="white")

You can also use stat_contour if you want it to shade between the lines.

nyc_map + stat_contour(aes(fill=..level..), geom="polygon", binwidth=250, alpha=.5)

Here's an example. The white lines are drawn by geom_contour. The shading is done by stat_contour.

Contour Example Image

Dylan Cross
  • 544
  • 2
  • 6
  • 14