install.packages(‘rsconnect’)
rsconnect::setAccountInfo(name=‘paychern1004’,
token=‘B1CE1C1B514D8371E1419AA4CA5F8D65’, secret=‘
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
line_plot <- gapminder %>%
filter(country %in% c("United States", "China", "India", "Germany", "Brazil")) %>%
ggplot(aes(x = year, y = gdpPercap, color = country)) +
geom_line(size = 1.2) +
labs(title = "GDP per Capita Over Time",
x = "Year",
y = "GDP per Capita",
color = "Country") +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
print(line_plot)
bar_chart <- gapminder %>%
filter(year == 2007) %>%
arrange(desc(pop)) %>%
top_n(10, pop) %>%
ggplot(aes(x = reorder(country, pop), y = pop)) +
geom_bar(stat = "identity", fill = "steelblue") +
coord_flip() +
labs(title = "Population of Top 10 Countries in 2007",
x = "Country",
y = "Population") +
theme_minimal()
print(bar_chart)
box_plot <- gapminder %>%
ggplot(aes(x = continent, y = lifeExp, fill = continent)) +
geom_boxplot() +
labs(title = "Life Expectancy by Continent",
x = "Continent",
y = "Life Expectancy") +
theme_minimal()
print(box_plot)
scatter_plot <- gapminder %>%
filter(year == 2007) %>%
ggplot(aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) +
geom_point(alpha = 0.7) +
scale_x_log10() +
labs(title = "GDP vs Life Expectancy (2007)",
x = "GDP per Capita (log scale)",
y = "Life Expectancy",
color = "Continent",
size = "Population") +
theme_minimal()
print(scatter_plot)
# Create a correlation matrix
cor_data <- gapminder %>%
select(lifeExp, gdpPercap, pop) %>%
cor()
# Melt the correlation matrix
melted_cor_data <- reshape2::melt(cor_data)
heatmap_plot <- ggplot(data = melted_cor_data, aes(x=Var1, y=Var2, fill=value)) +
geom_tile() +
scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Correlation") +
theme_minimal() +
labs(title = "Correlation Matrix Heatmap",
x = "",
y = "") +
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1))
print(heatmap_plot)
library(tidyr)
##
## Attaching package: 'tidyr'
## The following object is masked from 'package:reshape2':
##
## smiths
dumbbell_data <- gapminder %>%
filter(country %in% c("United States", "China", "India", "Germany", "Brazil") &
year %in% c(1952, 2007)) %>%
select(country, year, lifeExp) %>%
spread(year, lifeExp) %>%
rename(LifeExp_1952 = `1952`, LifeExp_2007 = `2007`)
dumbbell_chart <- ggplot(dumbbell_data, aes(x = LifeExp_1952, xend = LifeExp_2007, y = reorder(country, LifeExp_2007))) +
geom_dumbbell(color = "#a3c4dc", size = 2, point.colour.l = "#0e668b") +
labs(title = "Change in Life Expectancy from 1952 to 2007",
x = "Life Expectancy",
y = "Country") +
theme_minimal()
## Warning in geom_dumbbell(color = "#a3c4dc", size = 2, point.colour.l =
## "#0e668b"): Ignoring unknown parameters: `point.colour.l`
print(dumbbell_chart)
## Warning: Using the `size` aesthetic with geom_segment was deprecated in ggplot2 3.4.0.
## ℹ Please use the `linewidth` aesthetic instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
interactive_plot <- gapminder %>%
filter(year == 2007) %>%
plot_ly(x = ~gdpPercap, y = ~lifeExp, text = ~country, size = ~pop, color = ~continent,
type = 'scatter', mode = 'markers') %>%
layout(title = 'GDP vs Life Expectancy (2007)',
xaxis = list(title = 'GDP per Capita'),
yaxis = list(title = 'Life Expectancy'))
interactive_plot
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
# Load required libraries
library(ggplot2)
library(gganimate)
# Create ggplot object with transition specification
animated_plot <- gapminder %>%
ggplot(aes(x = gdpPercap, y = lifeExp, size = pop, color = continent, frame = year)) +
geom_point(alpha = 0.7) +
scale_x_log10() +
labs(title = "Life Expectancy and GDP per Capita Over Time",
x = "GDP per Capita (log scale)",
y = "Life Expectancy") +
theme_minimal() +
transition_time(year) +
labs(title = 'Year: {frame_time}')
# Animate the plot
# Install the animation package
# Set the CRAN mirror
options(repos = "https://cloud.r-project.org")
# Install the animation package
install.packages("animation")
##
## The downloaded binary packages are in
## /var/folders/gv/s06qh3zx5l529mnjryv81zgc0000gn/T//Rtmpxy5ark/downloaded_packages
# Animate the plot with a different fps value
animated_gif <- animate(animated_plot, nframes = 200, fps = 10)
# Save the animation as a GIF
anim_save("animated_life_expectancy.gif", animation = animated_gif)
# Display the animation
animated_gif