week 5

Author

Qiuyang Zhang

Published

October 24, 2024

library(ggplot2)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
ggplot(iris, aes(x = Species, y = Sepal.Length, color = Species)) +
  geom_boxplot() +
  labs(x = "Species", 
       y = "Sepal Length", 
       color = "Species", 
       title = "Sepal Length by 3 Different Species") +
  scale_color_brewer(palette = "Dark2") +
  theme_minimal()

ggplot(iris, aes(x = Petal.Length, fill = Species)) +
  geom_density(alpha = 0.6) +
  labs(x = "Petal Length", 
       y = "Density", 
       fill = "Species", 
       title = "Density of Petal Length by by 3 Different Species") +
   scale_fill_manual(values = c("setosa" = "red", "versicolor" = "green", "virginica" = "blue")) +
  scale_fill_brewer(palette = "Dark2") +
  theme_minimal()
Scale for fill is already present.
Adding another scale for fill, which will replace the existing scale.

ggplot(iris, aes(x = Petal.Length, y = Petal.Width, color = Species, shape = Species)) +
  geom_point(size = 2) +
  geom_smooth(method = "lm", se = FALSE) +  
  labs(x = "Petal Length", 
       y = "Petal Width", 
       color = "Species", 
       shape = "Species",
       title = "Petal Length vs Petal Width with Trend Lines by 3 Different Species") +
  scale_color_brewer(palette = "Dark2") +
  theme_minimal()
`geom_smooth()` using formula = 'y ~ x'

iris <- iris %>%
  mutate(size = ifelse(Sepal.Length < median(Sepal.Length), "small", "big"))
ggplot(iris, aes(x = Species, fill = size)) +
  geom_bar(position = "dodge") +
  labs(x = "Species", 
       y = "Count", 
       fill = "Size", 
       title = "Distribution of Big and Small Size by 3 Different Species") +
  scale_fill_brewer(palette = "Dark2") +
  theme_minimal()