library(macleish)
library(ggplot2)
library(tidyverse)
library(dygraphs)
library(plotly)
library(mdsr)
library(ggExtra)Assignment 2
Problem 3:
The macleish package contains weather data collected every 10 minutes in 2015 from two weather stations in Whately, Massachusetts.
Using the ggplot2 package, create a data graphic that displays the average temperature over each 10-minute interval (temperature) as a function of time (when) from the whately_2015 dataframe. Create annotations to include context about the four seasons: the date of the vernal and autumnal equinoxes, and the summer and winter solstices.
set.seed(123)
whately_2015 <- whately_2015
equinoxes <- data.frame(
event = c("Vernal Equinox", "Summer Solstice", "Autumnal Equinox", "Winter Solstice"),
date = as.POSIXct(c("2015-03-20 00:00:00", "2015-06-21 00:00:00", "2015-09-23 00:00:00", "2015-12-22 00:00:00"))
)
# Create the plot
ggplotly_1 <- whately_2015 |>
ggplot(aes(x = when, y = temperature)) +
geom_line(color = "lightblue", size = 0.3) +
geom_smooth(color = "black") +
labs(title = "Average Temperature Over Time",
x = "Time",
y = "Average Temperature") +
theme_minimal() +
geom_vline(data = equinoxes, aes(xintercept = date), linetype = "dashed", color = "darkred") +
annotate("Text", x = equinoxes$date, y = 55, label = equinoxes$event, color = "darkred", size = 3, vjust = -0.5) +
theme(legend.position = "bottom", panel.background = element_rect(fill ="#ECE7E6")) # Remove legend if not neededWarning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
ggplotly_1`geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
whately_2015 |> select(when,temperature)# A tibble: 52,560 × 2
when temperature
<dttm> <dbl>
1 2015-01-01 00:00:00 -9.32
2 2015-01-01 00:10:00 -9.46
3 2015-01-01 00:20:00 -9.44
4 2015-01-01 00:30:00 -9.3
5 2015-01-01 00:40:00 -9.32
6 2015-01-01 00:50:00 -9.34
7 2015-01-01 01:00:00 -9.3
8 2015-01-01 01:10:00 -9.1
9 2015-01-01 01:20:00 -9.07
10 2015-01-01 01:30:00 -8.99
# ℹ 52,550 more rows
season_2015 <- tibble(when = (ymd(c("2015 March 20", "2015 June 21",
"2015 September 23", "2015 December 21"))), season = c("Spring","Summer","Fall", "Winter"))
whately_plt <- whately_2015 |> ggplot(aes(as.Date(when), temperature))+
geom_vline(data = season_2015, color = "darkgrey", aes(xintercept= as.Date(when)))+
geom_text(data = season_2015, aes(y = 33, label = season, hjust = "left"))+
geom_line(size = 0.3)+
geom_smooth( )+
scale_x_date()
plotly_obj <- ggplotly(whately_plt)`geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
# Display the plotly object
plotly_objProblem 4: Modify the restaurant violations Shiny app so that it displays a table of the number of restaurants within a given type of cuisine along with a count of restaurants (as specified by the dba variable. (Hint: Be sure not to double count. The dataset should include 842 unique pizza restaurants in all boroughs and 281 Caribbean restaurants in Brooklyn.)
head(Violations)# A tibble: 6 × 16
camis dba boro building street zipcode phone inspection_date action
<int> <chr> <chr> <int> <chr> <int> <dbl> <dttm> <chr>
1 30075445 MORR… BRONX 1007 MORRI… 10462 7.19e9 2015-02-09 00:00:00 Viola…
2 30075445 MORR… BRONX 1007 MORRI… 10462 7.19e9 2014-03-03 00:00:00 Viola…
3 30075445 MORR… BRONX 1007 MORRI… 10462 7.19e9 2013-10-10 00:00:00 No vi…
4 30075445 MORR… BRONX 1007 MORRI… 10462 7.19e9 2013-09-11 00:00:00 Viola…
5 30075445 MORR… BRONX 1007 MORRI… 10462 7.19e9 2013-09-11 00:00:00 Viola…
6 30075445 MORR… BRONX 1007 MORRI… 10462 7.19e9 2013-08-14 00:00:00 Viola…
# ℹ 7 more variables: violation_code <chr>, score <int>, grade <chr>,
# grade_date <dttm>, record_date <dttm>, inspection_type <chr>,
# cuisine_code <dbl>
head(ViolationCodes)# A tibble: 6 × 3
violation_code critical_flag violation_description
<chr> <chr> <chr>
1 02A Critical Food not cooked to required minimum temperature.
2 02B Critical Hot food item not held at or above 140º F.
3 02C Critical Hot food item that has been cooked and refrigera…
4 02D Critical Precooked potentially hazardous food from commer…
5 02E Critical Whole frozen poultry or poultry breasts, other t…
6 02F Critical Meat, fish or molluscan shellfish served raw or …
head(Cuisines)# A tibble: 6 × 2
cuisine_code cuisine_description
<int> <fct>
1 1 Afghan
2 2 African
3 3 American
4 4 Armenian
5 5 Asian
6 6 Australian
mergedViolations <- Violations |>
right_join(Cuisines)Joining with `by = join_by(cuisine_code)`
head(mergedViolations)# A tibble: 6 × 17
camis dba boro building street zipcode phone inspection_date action
<int> <chr> <chr> <int> <chr> <int> <dbl> <dttm> <chr>
1 30075445 MORR… BRONX 1007 MORRI… 10462 7.19e9 2015-02-09 00:00:00 Viola…
2 30075445 MORR… BRONX 1007 MORRI… 10462 7.19e9 2014-03-03 00:00:00 Viola…
3 30075445 MORR… BRONX 1007 MORRI… 10462 7.19e9 2013-10-10 00:00:00 No vi…
4 30075445 MORR… BRONX 1007 MORRI… 10462 7.19e9 2013-09-11 00:00:00 Viola…
5 30075445 MORR… BRONX 1007 MORRI… 10462 7.19e9 2013-09-11 00:00:00 Viola…
6 30075445 MORR… BRONX 1007 MORRI… 10462 7.19e9 2013-08-14 00:00:00 Viola…
# ℹ 8 more variables: violation_code <chr>, score <int>, grade <chr>,
# grade_date <dttm>, record_date <dttm>, inspection_type <chr>,
# cuisine_code <dbl>, cuisine_description <fct>
mergedViolations %>% select(dba, boro, cuisine_description) %>%
group_by(cuisine_description) %>%
summarize(n = n_distinct(dba)) %>%
filter(cuisine_description == "Pizza")# A tibble: 1 × 2
cuisine_description n
<fct> <int>
1 Pizza 842
mergedViolations %>% select(dba, boro, cuisine_description) %>%
filter(boro == "BROOKLYN") %>%
group_by(cuisine_description) %>%
summarize(n = n_distinct(dba)) %>%
filter(cuisine_description == "Caribbean")# A tibble: 1 × 2
cuisine_description n
<fct> <int>
1 Caribbean 281
Shiny App Url[ https://7gtfwc-vikas0reddy-bodireddy.shinyapps.io/Homework2/]
Problem 6:
data_frame_1 <- palmerpenguins::penguins
p <- ggplot(data_frame_1, aes(x = bill_length_mm, y = body_mass_g)) +
geom_point() +
theme_classic() +
stat_smooth(method = "loess", formula = y ~ x, size = 2)
ggExtra::ggMarginal(p, type = "histogram", binwidth = 3)Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
Removed 2 rows containing non-finite values (`stat_smooth()`).
Warning: Removed 2 rows containing missing values (`geom_point()`).
We can see from the graph that there are few outliers in the data which pull down the graph at the end also from the histogram for body_mass also that there are few points that are extreme and pulling the grapgh towards them and also Grapgh is not purely normal.
Problem 7:
Shiny App Link[https://7gtfwc-vikas0reddy-bodireddy.shinyapps.io/penguins/]
Problem 8:
Shiny App Link[ https://7gtfwc-vikas0reddy-bodireddy.shinyapps.io/Whatley/]