From e8a08c89120317aa83bf2d7b30afc9664a6e28e6 Mon Sep 17 00:00:00 2001 From: Jing Miao Date: Mon, 16 Sep 2024 17:03:33 -0400 Subject: [PATCH] Case study 01 Archive --- week_01/case_study_01.R | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/week_01/case_study_01.R b/week_01/case_study_01.R index a8986c8..e010aef 100644 --- a/week_01/case_study_01.R +++ b/week_01/case_study_01.R @@ -48,25 +48,30 @@ 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) + @@ -74,11 +79,14 @@ ggplot(data = data_iris, aes(x = Petal.Length, y = after_stat(density), fill = S 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)