Open swkilar opened 6 years ago
I just tested this code, and it ran alright.
library( dplyr )
library( pander )
library( ggmap )
library( leaflet )
library( viridis )
URL <- "https://github.com/DS4PS/Data-Science-Class/blob/master/DATA/TempeTrafficAccidents.rds?raw=true"
dat <- readRDS(gzcon(url( URL )))
head( dat )
d2 <-
dat %>%
filter( Totalinjuries > 0 | Totalfatalities > 0 ) %>%
mutate( tot.injuries.fatalities = Totalinjuries + Totalfatalities )
point.size <- d2$tot.injuries.fatalities
par( mar=c(0,0,4,0) )
plot( d2$Longitude, d2$Latitude, pch=19,
cex=0.25*d2$tot.injuries.fatalities,
col=alpha( "firebrick", alpha=0.5),
main="Traffic Accidents in Tempe, AZ",
xlab="", ylab="",
bty="n", axes=F )
title( main="(plot size relative to injuries and fatalities)",
line=0.5, cex.main=0.7 )
qmplot( Longitude, Latitude, data=d2, maptype="toner-lite", zoom=14,
size=I(point.size), color=I("firebrick"), alpha = I(0.1) )
You might try reinstalling ggmap
from GitHub directly:
if(!requireNamespace("devtools")) install.packages("devtools")
devtools::install_github("dkahle/ggmap", ref = "tidyup")
Or just check your data steps to make sure they all worked before you try plotting?
The only other thing I can think of is if you are getting an error because qmplot() can't download map tiles. Sometimes you hit a daily limit from the API service, and it can be funny on university networks sometimes.
Thank you. I don't know what was wrong. I closed RStudio and then reopened my lab file and then it ran without an error.
But now I'm stuck on Part 2 of the lab, leaflet. When I run the example code under the Lab 2 header, I don't get the orange and black circles. Only the map shows up but no data is mapped. What code is supposed to precede this:
# scale points by severity of code violations
d2$fatal <- d2$Totalfatalities > 0
col.vec <- ifelse( d2$fatal, "orange", "black" )
opac <- ifelse( d2$fatal, 1, 0.2 )
# add details of crash as a popup:
crash.details <- paste0( "<b>Time: </b>", d2$DateTime, "<br>",
"<b>Collision Manner: </b>", d2$Collisionmanner, "<br>",
"<b>Violation Issued: </b>", d2$Violation1_Drv1 )
tempe <- leaflet( ) %>%
addProviderTiles( "CartoDB.Positron" ) %>%
setView( lng=-111.9278, lat=33.39951, zoom=14 )
addCircles( tempe, lng=d2$Longitude, lat=d2$Latitude,
fillColor=col.vec, fillOpacity=opac,
stroke=F, radius=20*point.size,
popup=crash.details )
I must be missing something in the code before this portion.
This runs for me in a new R environment on a Windows machine:
library( leaflet )
library( dplyr )
URL <- "https://github.com/DS4PS/Data-Science-Class/blob/master/DATA/TempeTrafficAccidents.rds?raw=true"
dat <- readRDS(gzcon(url( URL )))
head( dat )
d2 <-
dat %>%
filter( Totalinjuries > 0 | Totalfatalities > 0 ) %>%
mutate( tot.injuries.fatalities = Totalinjuries + Totalfatalities )
point.size <- d2$tot.injuries.fatalities
# scale points by severity of code violations
d2$fatal <- d2$Totalfatalities > 0
col.vec <- ifelse( d2$fatal, "orange", "black" )
opac <- ifelse( d2$fatal, 1, 0.2 )
# add details of crash as a popup:
crash.details <- paste0( "<b>Time: </b>", d2$DateTime, "<br>",
"<b>Collision Manner: </b>", d2$Collisionmanner, "<br>",
"<b>Violation Issued: </b>", d2$Violation1_Drv1 )
tempe <- leaflet( ) %>%
addProviderTiles( "CartoDB.Positron" ) %>%
setView( lng=-111.9278, lat=33.39951, zoom=14 )
addCircles( tempe, lng=d2$Longitude, lat=d2$Latitude,
fillColor=col.vec, fillOpacity=opac,
stroke=F, radius=20*point.size,
popup=crash.details )
Do you get any error messages?
Are you on a Mac by any chance? Maybe try dropping opacity:
addCircles( tempe, lng=d2$Longitude, lat=d2$Latitude,
fillColor=col.vec, # fillOpacity=opac,
stroke=F, radius=20*point.size,
popup=crash.details )
I started my code chunk with this line:
point.size <- d2$tot.injuries.fatalities
and then followed it with this:
# scale points by severity of code violations
d2$fatal <- d2$Totalfatalities > 0
col.vec <- ifelse( d2$fatal, "orange", "black" )
opac <- ifelse( d2$fatal, 1, 0.2 )
# add details of crash as a popup:
crash.details <- paste0( "<b>Time: </b>", d2$DateTime, "<br>",
"<b>Collision Manner: </b>", d2$Collisionmanner, "<br>",
"<b>Violation Issued: </b>", d2$Violation1_Drv1 )
tempe <- leaflet( ) %>%
addProviderTiles( "CartoDB.Positron" ) %>%
setView( lng=-111.9278, lat=33.39951, zoom=14 )
addCircles( tempe, lng=d2$Longitude, lat=d2$Latitude,
fillColor=col.vec, fillOpacity=opac,
stroke=F, radius=20*point.size,
popup=crash.details )
I left these chunks out because I had already coded all of it previously for other parts of the lab, in other chunks (i.e., I had already added the libraries, the data call, and created d2 in my setup for the facet wrap portion):
library( leaflet )
library( dplyr )
URL <- "https://github.com/DS4PS/Data-Science-Class/blob/master/DATA/TempeTrafficAccidents.rds?raw=true"
dat <- readRDS(gzcon(url( URL )))
head( dat )
d2 <-
dat %>%
filter( Totalinjuries > 0 | Totalfatalities > 0 ) %>%
mutate( tot.injuries.fatalities = Totalinjuries + Totalfatalities )
That didn't work. But as soon as I added the d2 creation portion into my leaflet chunk, it started working properly (and, no, I was not getting an error message before):
d2 <-
dat %>%
filter( Totalinjuries > 0 | Totalfatalities > 0 ) %>%
mutate( tot.injuries.fatalities = Totalinjuries + Totalfatalities )
Shouldn't I be able to leave this out if I've already created d2, as I did before the facet wrap portion of the lab?
Thank you for your help!
It is strange. Some things are localized within a chunk, for example changing a working directory or a plot() function and a point() function need to be within the same chunk (point adds data to the plot window). But after d2 is created, it should be available in subsequent steps. So unless you were altering d2 or point.size in some way they should be available in future chunks.
If you can send the file where you had problems I can diagnose, but otherwise I'm not sure what the issue may have been without seeing more what you were doing?
I'll send it over. If you happen to see what's causing the problem, please let me know. If not, that's OK. This problem didn't impact my ability to finish the lab.
Oh, I was altering d2 in some way -- by adding a mutation. But I thought that adding a new column to the data frame wouldn't impact it. It could be that. Thanks!
It shouldn't, but unclear to me what would cause the glitch. Glad it's working.
I'm running into an error when I run the example in Lab 11. When I run the code below I get the following error: "Error: Aesthetics must be either length 1 or the same as the data (28470): colour, size, alpha"
Can anyone explain why this would be? I'm I layering these functions improperly?