Skip to content

Commit

Permalink
Case study 01 Archive
Browse files Browse the repository at this point in the history
  • Loading branch information
jingmiao7 committed Sep 16, 2024
1 parent cbb7098 commit e8a08c8
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions week_01/case_study_01.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,45 @@ ggplot(data = iris, aes(x = Petal.Length, y = after_stat(density))) +
geom_vline(aes(xintercept = mean_length), length_stats, color = "red", linewidth = 1) +
geom_density(color = "green", linewidth = 1)

# change number of bins by changing the width of it
ggplot(data = iris, aes(x = Petal.Length, y = after_stat(density))) +
geom_histogram(boundary = 0.5) +
geom_vline(aes(xintercept = mean_length), length_stats, color = "red", linewidth = 1) +
geom_density(color = "green", linewidth = 1)

#change the colors of the background and the bins
ggplot(data = iris, aes(x = Petal.Length, y = after_stat(density))) +
geom_histogram(bins = 50, color = "white", fill = "blue") +
geom_vline(aes(xintercept = mean_length), length_stats, color = "red", linewidth = 1) +
geom_density(color = "green", linewidth = 1)

# import the factor "Species" from data_iris
data_iris <- iris |>
mutate(Species = factor(Species))

# group it by factor "species"
ggplot(data = data_iris, aes(x = Petal.Length, y = after_stat(density), fill = Species)) +
geom_histogram(bins = 50, color = "white", fill = "blue") +
geom_vline(aes(xintercept = mean_length), length_stats, color = "red", linewidth = 1) +
geom_density(color = "gray", linewidth = 1)+
labs(x ='Length of Petal (cm)', y='Count', title = 'Petal Length Distributions')

# Add the range of x using xlin()
ggplot(data = data_iris, aes(x = Petal.Length, y = after_stat(density), fill = Species)) +
geom_histogram(bins = 50, color = "white", fill = "blue") +
geom_vline(aes(xintercept = mean_length), length_stats, color = "red", linewidth = 1) +
geom_density(color = "gray", linewidth = 1)+
labs(x ='Length of Petal (cm)', y='Count', title = 'Petal Length Distributions') +
xlim(0, 8)

ggplot(data = data_iris, aes(x = Petal.Length, y = after_stat(density), fill = Species)) +
# adjust the position of the legend and separate the three species into three graphs

PetalPlot <- ggplot(data = data_iris, aes(x = Petal.Length, y = after_stat(density), fill = Species)) +
geom_histogram(bins = 50, color = "white", fill = "blue") + facet_grid(vars(Species)) +
geom_vline(aes(xintercept = mean_length), length_stats, color = "red", linewidth = 1) +
geom_density(color = "gray", linewidth = 1)+
labs(x ='Length of Petal (cm)', y='Count', title = 'Petal Length Distributions') +
geom_density(color = "gray", linewidth = 1, alpha = 0.5)+
labs(x ='Length of Petal (cm)', y='Count', title = 'Petal length distributions of three species', caption = "Data Resource: iris (https://rpubs.com/moeransm/intro-iris)") +
xlim(0, 8) +
theme(legend.position = "bottom")
theme(legend.position = "bottom", plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5))

ggsave("Petal_Length_Distributions.png", plot = PetalPlot, width = 8, height = 6, dpi = 300)

0 comments on commit e8a08c8

Please sign in to comment.