Question

I need help working with maps in R. This is the my first foray with R's mapping tools, and need help. I am trying to plot Yelp's star ratings for each restaurant in a city. The code I started only gives the location of the restaurant, and not a the star rating. My hope is to have a gradient for the star rating with 5 being red, and 1 being yellow, or there abouts. Any help is appreciated!

library(ggmap)
Phoenix <- get_map(location = 'Phoenix', zoom = 11)
ggmap(Phoenix)

mapPoints <- ggmap(map) + geom_point(aes(x=longitude, y=latitude, size=sqrt(stars)), data=cleanDataFrame, alpha=.1)

the data frame I am using is structured as so:

        business_id longitude stars latitude duration
1 --5jkZ3-nUPZxUvtcbr8Uw -111.9269   4.5 33.46337      381
2 --BlvDO_RG2yElKu9XA1_g -111.8983   4.0 33.62146      690
3 -_Ke8q969OAwEE_-U0qUjw -112.1863   4.5 33.65387      604
4 -_npP9XdyzILAjtFfX8UAQ -112.0739   3.0 33.44990     1916
5 -2xCV0XGD9NxfWaVwA1-DQ -112.2766   4.0 33.56626      226
6 -3WVw1TNQbPBzaKCaQQ1AQ -112.0692   3.5 33.48585     2190
Était-ce utile?

La solution

Are you trying to color the points based on the number of stars?

Load the data frame:


df <- read.table(text='        business_id longitude stars latitude duration
1 --5jkZ3-nUPZxUvtcbr8Uw -111.9269   4.5 33.46337      381
2 --BlvDO_RG2yElKu9XA1_g -111.8983   4.0 33.62146      690
3 -Ke8q969OAwEE-U0qUjw -112.1863   4.5 33.65387      604
4 -_npP9XdyzILAjtFfX8UAQ -112.0739   3.0 33.44990     1916
5 -2xCV0XGD9NxfWaVwA1-DQ -112.2766   4.0 33.56626      226
6 -3WVw1TNQbPBzaKCaQQ1AQ -112.0692   3.5 33.48585     2190')

And plot your image while defining a color gradient for the points (yellow to red as you stated).

ggmap(Phoenix) + geom_point(aes(x=longitude, y=latitude, color=stars), size=5, data=df, alpha=.3) + scale_color_gradient(low="yellow",high="red")

which will produce the following (I've increased the alpha and size to make it easier to see):

plot of restaurants with rankings

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top