paste0(letters,1:5,collapse=":")paste()
function.Name includes the word “coffee” (in any case!)stringr function!Inspection_Score by
business (Name) and Year.City variable so that it is in
title case. Then, modify your plot from 2.1 such that each city has a
different color.qmplot function in the gmap packagegeom_label_repelload(url("https://pearce790.github.io/CSSS508/Lectures/Lecture8/restaurants.Rdata"))
paste0(letters,1:5,collapse=":")nchar(paste0(letters,1:5,collapse=":"))
## [1] 77
paste()
function.ANSWER: “sep” is what separates strings provided by multiple arguments to the
pastefunction. “collapse” is what separates the already-pasted strings after additionally collapsing them into a single string.
Name includes the word “coffee” (in any case!)coffee <- restaurants
coffee$Name <- str_to_lower(coffee$Name)
coffee <- coffee %>% filter(str_detect(Name,"coffee"))
coffee$NameLength <- str_length(str_trim(coffee$Name))
stringr function!coffee$Year <- str_sub(coffee$Inspection_Date,-4,-1)
ggplot(coffee,aes(Year,NameLength))+geom_boxplot()
Inspection_Score by
business (Name) and Year.coffee_summary <- coffee %>% group_by(Name,Year) %>%
summarize(MaxScore=max(Inspection_Score))
## `summarise()` has grouped output by 'Name'. You can override using the
## `.groups` argument.
coffee_summary %>% head(6)
## # A tibble: 6 × 3
## # Groups: Name [2]
## Name Year MaxScore
## <chr> <chr> <int>
## 1 701 coffee 2015 10
## 2 701 coffee 2016 48
## 3 701 coffee 2017 0
## 4 909 coffee and wine 2007 2
## 5 909 coffee and wine 2008 5
## 6 909 coffee and wine 2009 2
ggplot(coffee_summary,aes(Year,MaxScore,group=Name))+
geom_line(alpha=.2)+theme_bw()
ggplot(coffee,aes(x=Longitude,y=Latitude))+
geom_point()+
theme_bw()
City variable so that it is in
title case. Then, modify your plot from 2.1 such that each city has a
different color.coffee$City <- str_to_title(coffee$City)
ggplot(coffee,aes(x=Longitude,y=Latitude,color=City))+
geom_point()+
theme_bw()
qmplot function in the gmap packageqmplot(data=coffee,x=Longitude,y=Latitude,
color=City)
Filter to coffee shops in Bellevue first!!
bellevue_coffee <- coffee %>% filter(City == "Bellevue")
qmplot(data = bellevue_coffee,
geom = "blank",
x = Longitude,
y = Latitude,
darken = 0.5)+
stat_density_2d(
aes(fill = stat(level)), #<<
geom = "polygon",
alpha = .2, color = NA
)+
scale_fill_gradient2(
"Coffee Shops",
low = "white",
mid = "yellow",
high = "red")
Hint: Use the select, filter, and
distinct functions (in that order). Within
filter, you’ll use str_detect.
unique_bellevue_coffee <- bellevue_coffee %>%
select(Name,Latitude,Longitude) %>%
filter(str_detect(string = Name,pattern = "starbucks")) %>%
distinct()
geom_label_repelqmplot(data=bellevue_coffee,
x=Longitude,y=Latitude,
alpha=I(.1))+
geom_label_repel(
data = unique_bellevue_coffee,
aes(label = Name),
size=2)