Skip to content

Commit e465714

Browse files
author
Annie Yang
committed
App combine version 1.0
1 parent 82f27b6 commit e465714

25 files changed

+1455388
-0
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#' @title Compare AQI category percentage by CBSA
2+
#'
3+
#' @description There are six categories of AQI. Each category corresponds to a different level of health concern. This function
4+
#' plots percentage of each category by CBSA. It can be used to compare AQI among different CBSAs.
5+
#' @param data A data.frame. The default dataset is annual_aqi.
6+
#' @param cbsa A vector of CBSA. You can input the CBSAs you want to compare.
7+
#' @param category A character vector of health concern category by AQI. There are six categories.
8+
#' Input category from "Good", "Moderate", "UnhealthyForSensitiveGroup", "Unhealthy", "VeryUnhealthy" and "Hazardous".
9+
#' Default value includes all six categories.
10+
#' @param year A numeric vector.
11+
#' @param plot A character. The value is either "line" or "bar".
12+
#' Default value is "line". You can use it to decide which kind of plot you want to get.
13+
#' @examples
14+
#' aqi_healthconcern(annual_aqi,c("Aberdeen, SD","Adrian, MI"),
15+
#' category =c("Good","Moderate","Unhealthy"),year=c(2000,2008,2015),plot="bar")
16+
#'
17+
#' @export
18+
19+
aqi_healthconcern<-function(data=annual_aqi,cbsa,
20+
category=c("Good", "Moderate", "UnhealthyForSensitiveGroup", "Unhealthy", "VeryUnhealthy","Hazardous"),
21+
year=c(2000:2015),plot="line"){
22+
23+
24+
healthconcern <- data%>%
25+
mutate(
26+
Good = as.numeric(data$'Good Days')/as.numeric(data$'Days with AQI'),
27+
Moderate = as.numeric(data$'Moderate Days')/as.numeric(data$'Days with AQI'),
28+
UnhealthyForSensitiveGroup = as.numeric(data$'Unhealthy for Sensitive Groups Days')/as.numeric(data$'Days with AQI'),
29+
Unhealthy = as.numeric(data$'Unhealthy Days')/as.numeric(data$'Days with AQI'),
30+
VeryUnhealthy = as.numeric(data$'Very Unhealthy Days')/as.numeric(data$'Days with AQI'),
31+
Hazardous = as.numeric(data$'Hazardous Days')/as.numeric(data$'Days with AQI')
32+
)%>%
33+
select(CBSA,Year,category)%>%
34+
filter(CBSA %in% cbsa & Year %in% year) # Calculate the percentage of each AQI category.
35+
36+
if(plot=="line"){
37+
healthconcern %>%
38+
tidyr::gather("id", "Percentage", (ncol(healthconcern)-length(category)+1):ncol(healthconcern)) %>%
39+
ggplot(., aes(Year, Percentage, color=CBSA))+
40+
geom_smooth(method = "loess", se=FALSE)+
41+
facet_wrap(~id,scales="free_y")+
42+
theme_bw()
43+
} else{
44+
healthconcern %>%
45+
tidyr::gather("id", "Percentage", (ncol(healthconcern)-length(category)+1):ncol(healthconcern)) %>%
46+
ggplot(., aes(Year, Percentage))+
47+
geom_bar(stat = "identity",aes(fill = CBSA), position = "dodge")+
48+
facet_wrap(~id,scales="free_y")+
49+
theme_bw()
50+
}
51+
52+
}

App_combine_version/R/avg_temp.R

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#' @title Average Temperatures Trend
2+
#'
3+
#' @description This function helps you analyse global as well as countries average temperatures trend during a specified time period.
4+
#' You can use it to do comparison between countries.
5+
#' @param data A data.frame. Use GlobalTemperatures or GlobalLandTemperaturesByCountry dataset to do analysis.
6+
#' @param type A vector giving which type of trend you want to plot. 1 indicates yearly trend. 2 indicates monthly trend. c(1,2) indicates both yearly and monthly average temperature trend.
7+
#' @param year A vector. Time period during which you want to analyse how the temperature changed.
8+
#' @param month A vector. Time period during which you want to analyse how the temperature changed.
9+
#' @param country A vector of countries' name. Compare how the temperature changed in these countries during a specified time period.
10+
#' @param con Display confidence interval around smooth? (TRUE for confidence interval. FALSE by default)
11+
#' @examples
12+
#' avg_temp(data=GlobalLandTemperaturesByCountry, type=c(1,2),
13+
#' year=c(2000:2015), month=c(1:12),
14+
#' country = c("Afghanistan","Andorra"),con="F")
15+
#'
16+
#' @export
17+
18+
# Load GlobalTemperatures or GlobalLandTemperaturesByCountry dataset before using this function
19+
20+
avg_temp <- function(data, type, year, month, country, con = "FALSE"){
21+
22+
if(missing(country)){ # when data=GlobalTemperatures, the country argument is missing
23+
glb_avg_temp <- data %>%
24+
mutate(Month=as.numeric(format(data$dt,"%m")), # Create new column month (decimal number)
25+
Month.String=format(data$dt,"%B"), # Create string month (full name)
26+
Year=as.numeric(format(data$dt,"%Y")))%>% # Create new column year (4 digit)
27+
select(dt, Month, Month.String, Year, LandAverageTemperature, LandAverageTemperatureUncertainty)%>%
28+
na.omit() # Remove missing values
29+
30+
31+
avg_temp_year<-glb_avg_temp %>%
32+
filter(Year %in% year)%>%
33+
group_by(Year) %>%
34+
summarise(AverageTemp=mean(LandAverageTemperature)) %>% # Calculate yearly average temperature
35+
ungroup()%>%
36+
ggplot(aes(x = Year, y = AverageTemp)) +
37+
geom_smooth(method="loess", se=con)+
38+
labs(title = "Global Yearly Average Temperatures",
39+
x="Year",
40+
y="Average Temperature")
41+
42+
avg_temp_month<-glb_avg_temp %>%
43+
filter(Year %in% year & Month %in% month)%>%
44+
ggplot(aes(x=dt, y=LandAverageTemperature,colour=reorder(Month.String,-LandAverageTemperature,mean)))+
45+
# Sort month from highest temperature to lowest temperature
46+
geom_smooth(method="loess",se=con)+
47+
labs(title="Global Average Temperatures By Month",
48+
x="Year",
49+
y="Average Temperature",
50+
colour="Month")
51+
52+
if(identical(type,c(1,2))){
53+
ggpubr::ggarrange(avg_temp_year,avg_temp_month,ncol=2,nrow=1)
54+
} else if(type==1) {
55+
avg_temp_year
56+
} else {
57+
avg_temp_month
58+
}
59+
} else {
60+
glb_avg_temp <- data %>%
61+
mutate(Month=as.numeric(format(data$dt,"%m")),
62+
Month.String=format(data$dt,"%B"),
63+
Year=as.numeric(format(data$dt,"%Y")))%>% # Create new columns month and year
64+
select(dt, Month, Month.String, Year, Country, AverageTemperature, AverageTemperatureUncertainty)%>%
65+
na.omit() %>% # Remove missing values
66+
filter(Country %in% country)
67+
68+
avg_temp_year <- glb_avg_temp %>%
69+
filter(Year %in% year)%>%
70+
group_by(Year, Country) %>%
71+
summarise(AverageTemp=mean(AverageTemperature)) %>% # Calculate yearly average temperature by country
72+
ungroup()%>%
73+
ggplot(aes(x = Year, y = AverageTemp,colour=Country)) +
74+
geom_smooth(method="loess", se=con)+
75+
labs(title = "Yearly Average Temperatures By Country",
76+
x="Year",
77+
y="Average Temperature")
78+
79+
avg_temp_month <- glb_avg_temp %>%
80+
filter(Year %in% year & Month %in% month)%>%
81+
ggplot(aes(x=dt, y=AverageTemperature,colour=reorder(Month.String,-AverageTemperature,mean)))+
82+
geom_smooth(method="loess",se=con)+
83+
facet_wrap(~Country,scales="free")+
84+
labs(title="Monthly Average Temperatures By Country",
85+
x="Year",
86+
y="Average Temperature",
87+
colour="Month")
88+
89+
if(identical(type,c(1,2))){
90+
ggpubr::ggarrange(avg_temp_year,avg_temp_month)
91+
} else if(type==1) {
92+
avg_temp_year
93+
} else {
94+
avg_temp_month
95+
}
96+
}
97+
}

App_combine_version/R/boxplot_aqi.R

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#' @title Make the Boxplot of AQI
2+
#'
3+
#' @description This function helps you make boxplots of AQI in the year you are interested. And also mark the point of the CBSA you are interested.
4+
#' So that you can know the AQI of the CBSA is relatively high or low compared to other geographic areas. You can make comparison of AQI in
5+
#' different years by the notch. if two boxes' notches do not overlap there is ‘strong evidence’ (95% confidence) their medians differ.
6+
#' @param data A data.frame. The default dataset is annual_aqi.
7+
#' @param year A vector, years in which you want to display the distribution of AQI.
8+
#' @param cbsa A CBSA in US.
9+
#' @examples
10+
#' boxplot_aqi(annual_aqi,c("2000","2005"),"Albany, GA")
11+
#'
12+
#' @export
13+
14+
15+
16+
boxplot_aqi<-function(data=annual_aqi,year, cbsa){
17+
18+
names(data)[13] <- "MedianAQI"
19+
20+
interested_cbsa<-data%>%filter(CBSA==cbsa & Year %in% year)
21+
22+
box_data<-data%>%filter(Year %in% year)
23+
24+
ggplot(box_data,aes(x=as.factor(Year), y=MedianAQI, fill=as.factor(Year)))+
25+
geom_boxplot(alpha=0.2,
26+
# Does the midian of AQI differ?
27+
# if two boxes' notches do not overlap there is ‘strong evidence’ (95% confidence) their medians differ.
28+
notch=TRUE,
29+
notchwidth = 0.5,
30+
31+
outlier.colour="blue",
32+
outlier.fill="blue",
33+
outlier.size=2
34+
)+
35+
geom_point(data=interested_cbsa,
36+
aes(x=as.factor(Year), y=MedianAQI),
37+
color="red", size=2)+
38+
labs(x= "Year")+
39+
theme(legend.position="none")
40+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
# Install packages that the program need
3+
4+
install_packages = function(names)
5+
{
6+
for(name in names)
7+
{
8+
if (!(name %in% installed.packages()))
9+
install.packages(name, repos="http://cran.us.r-project.org")
10+
11+
library(name, character.only=TRUE)
12+
}
13+
}
14+
15+
install_packages(c("dplyr", "ggplot2",
16+
"choroplethr","choroplethrMaps","plotly","countrycode"))
17+

App_combine_version/R/map_temp.R

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#' @title Plot Temperature Geographic Maps by States in USA
2+
#'
3+
#' @description This function plots temperature geographic maps for States in USA in specific year
4+
#' @param data A data.frame. The defalt dataset is GlobalLandTemperaturesByState.
5+
#' @param year numeric
6+
#' @examples
7+
#' temp_state(year=2012)
8+
#'
9+
#' @export
10+
11+
# Loading Packages
12+
# library(choroplethr)
13+
# library(choroplethrMaps)
14+
# library(plotly)
15+
# library(countrycode)
16+
17+
temp_state<-function(data=GlobalLandTemperaturesByState,year){
18+
19+
map <- data %>%
20+
mutate(Month=as.numeric(format(data$dt,"%m")), # Create new column month (decimal number)
21+
Month.String=format(data$dt,"%B"), # Create string month (full name)
22+
Year=as.numeric(format(data$dt,"%Y"))) %>% # Create new column year (4 digit)
23+
na.omit() %>% filter(Country=="United States")
24+
25+
map$State <- as.character(map$State)
26+
map$State[map$State=="Georgia (State)"] <- "Georgia" # Changing Georgia (State)
27+
map$State<- as.factor(map$State)
28+
29+
#' select columns of interest
30+
map_select <- map %>%
31+
select(Year,AverageTemperature,State) %>%
32+
group_by(Year, State) %>%
33+
summarise(value=mean(AverageTemperature))
34+
35+
#Data frame must have a column named region (all lower case) and another one value.
36+
colnames(map_select)[2]<- "region"
37+
map_select$region<-tolower(map_select$region)
38+
39+
map_state<-map_select %>%
40+
filter(Year==year)
41+
42+
map_state<-map_state[,2:3]
43+
44+
print(state_choropleth(map_state,
45+
title = paste("Land Temperature",year," "),
46+
num_colors = 8,
47+
legend = "Degrees"),reference_map=TRUE)
48+
}
49+
50+
#' @title Plot Temperature Geographic Maps by Country
51+
#'
52+
#' @description This function plots temperature geographic maps for countries in specific year. You can use this
53+
#' function to get a temperature geographic map showing the temperature change from the start year to end year.
54+
#' @param data A data.frame. The defalt dataset is GlobalLandTemperaturesByCountry.
55+
#' @param year A numeric. You can get temperature geographic maps for countries in this year.
56+
#' @param start A numeric. The start year you want to do temperature comparison.
57+
#' @param end A numeric. Then end year you want to do temperature comparison.
58+
#' @param diff A character. If diff=="TRUE", you will get a temperature geographic map showing the temperature
59+
#' change from the start year to end year. (Default value is "FALSE")
60+
#' @examples
61+
#' temp_country(year=2012)
62+
#' temp_country(start=1990,end=2000,diff="TRUE")
63+
64+
temp_country<-function(data=GlobalLandTemperaturesByCountry, year, start, end, diff="FALSE"){
65+
# light grey boundaries
66+
l <- list(color = toRGB("grey"), width = 0.5)
67+
68+
# specify map projection/options
69+
g <- list(
70+
showframe = FALSE,
71+
showcoastlines = FALSE,
72+
projection = list(type = 'Mercator')
73+
)
74+
75+
map_country <- data %>%
76+
mutate(Month=as.numeric(format(data$dt,"%m")), # Create new column month (decimal number)
77+
Month.String=format(data$dt,"%B"), # Create string month (full name)
78+
Year=as.numeric(format(data$dt,"%Y"))) %>% # Create new column year (4 digit)
79+
na.omit()%>%
80+
select(Year,AverageTemperature,Country) %>%
81+
group_by(Year, Country) %>%
82+
summarise(AvgTemp=mean(AverageTemperature))
83+
84+
code<-countrycode(map_country$Country,'country.name', 'iso3c') # Converts long country name into country codes
85+
86+
map_country$CODE<-code # Create new column in map_country named "CODE"
87+
88+
if(diff=="FALSE"){
89+
temp<-map_country%>%filter(Year==year)
90+
91+
map_temp <- plot_geo(temp) %>%
92+
add_trace(
93+
z = ~AvgTemp, color = ~AvgTemp, colors = 'Reds',
94+
text = ~Country, locations = ~CODE, marker = list(line = l)
95+
) %>%
96+
colorbar(title = 'Temperature') %>%
97+
layout(
98+
title = paste(year,"Temperature Map",sep=" "),
99+
geo = g
100+
)
101+
102+
map_temp
103+
} else if (diff=="TRUE") {
104+
105+
temp_diff<-map_country %>%
106+
filter(Year==start | Year==end) %>%
107+
tidyr::spread(Year, AvgTemp)
108+
109+
temp_diff$Difference<-unlist(temp_diff[,4]-temp_diff[,3]) # Calculate temperature variation from start year to end year
110+
111+
map_temp <- plot_geo(temp_diff) %>%
112+
add_trace(
113+
z = ~Difference, color = ~Difference, colors = 'Reds',
114+
text = ~Country, locations = ~CODE, marker = list(line = l)
115+
) %>%
116+
colorbar(title = 'Temperature Variation') %>%
117+
layout(
118+
title = paste(start,"-",end,"Temperature Variation Map", sep=" "),
119+
geo = g
120+
)
121+
print(map_temp)
122+
}
123+
124+
}

App_combine_version/R/stat.R

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#' @title Calculate summary statistics for AQI
2+
#'
3+
#' @description This function helps you calculate summary statistics for AQI by CBSA.
4+
#' @param data A data.frame
5+
#' @param cbsa CBSA in US
6+
#' @seealso
7+
#' @ruturn
8+
#' @examples stat_func(cbsa="Ames, IA")
9+
#'
10+
#' @export
11+
#' Loading packages
12+
#' library(dplyr)
13+
#' library(Rmisc)
14+
15+
16+
stat_func<-function(data=aqi,cbsa){
17+
data_stat<-data%>%
18+
filter(CBSA==cbsa)
19+
stat_table <- as.array(summary(data_stat$`Median AQI`))
20+
stat_df <- as.data.frame(stat_table)
21+
colnames(stat_df) <- c("Stat", "Value")
22+
Confi_Interval <- CI(data_stat$`Median AQI`, ci = 0.95) #Caculate the confidence interval of Median AQI
23+
lower <- data.frame('CI lower',Confi_Interval[['lower']])
24+
upper <- data.frame('CI upper',Confi_Interval[['upper']])
25+
names(lower) <- c("Stat", "Value")
26+
names(upper) <- c("Stat", "Value")
27+
stat_df <- rbind(stat_df,lower, upper)
28+
return(stat_df)
29+
}
30+

0 commit comments

Comments
 (0)