library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
data(diamonds)
head(diamonds)
## # A tibble: 6 × 10
##   carat cut       color clarity depth table price     x     y     z
##   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.23 Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
## 2  0.21 Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
## 3  0.23 Good      E     VS1      56.9    65   327  4.05  4.07  2.31
## 4  0.29 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
## 5  0.31 Good      J     SI2      63.3    58   335  4.34  4.35  2.75
## 6  0.24 Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48

First Bar PLot:

ggplot -> “gg” stands for grammar of graphics. “data = diamonds” is the data set we want. “aes” stands for asthetics so there is a cut or white line inbetween each Cut variable in the X axis Adding the plus sign (Must go on first line if you use two lines) puts in the data set (dark grey bars) can also code it like this: ggplot(data = diamonds, aes(x = cut)) + geom_bar()

ggplot(data = diamonds) +geom_bar(aes(x = cut))

Unique function:

Shows the Unique categories as shown below with the unique levels of diamond cuts and their levels or tiers

unique(diamonds$cut)
## [1] Ideal     Premium   Good      Very Good Fair     
## Levels: Fair < Good < Very Good < Premium < Ideal

Tibble

Making a bar graph with x and y variables

demo <- tribble(
      ~cut,         ~freq,
      "Fair",       1610,
      "Good",       4906,
      "Very Good",  12082,
      "Premium",    13791,
      "Ideal",      21551
    )
ggplot(data = demo) +
  geom_bar(mapping = aes(x = cut, y = freq), stat = "identity")

Proportional Bars

“labs” = Lables x axis is not labled “Diamond Cuts” y axis is labled Graph is titled “Proportional Bar Graph of Diamond Cuts”

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, y = stat(prop), group = 1)) +
  labs(x = "Diamond Cut", y = "Proportion", 
       title = "Proportional Bar Graph of Diamond Cuts")
## Warning: `stat(prop)` was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(prop)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Bar Plot with Alpha Transparencey

Alpha is the transparency level “fill” fills the bar

ggplot(data = diamonds, aes(x=cut, fill = cut)) + 
  geom_bar(alpha = 0.5)+  # try replacing alpha = 0.5 with 0.8 to see how it changes
  labs(x = "Diamond Cut", y = "Frequency", 
       title = "Frequency Bar Graph of Diamond Cuts")

## Color

ggplot(data = diamonds, aes(x=cut, fill = cut)) + 
  geom_bar(Color = "blue")+ 
  labs(x = "Diamond Cut", y = "Frequency", 
       title = "Frequency Bar Graph of Diamond Cuts")
## Warning in geom_bar(Color = "blue"): Ignoring unknown parameters: `Color`

Stacking Bar graphs (position = stack)

ggplot(data = diamonds) + 
  geom_bar(aes(x = cut, fill = clarity), position = "stack") +
    labs(x = "Diamond Cut", y = "Frequency", 
       title = "Stacked Bar Graph of Diamond Cuts by Clarity")

## Position equals fill:

ggplot(data = diamonds) + 
  geom_bar(aes(x = cut, fill = clarity), position = "fill") +
    labs(x = "Diamond Cut", y = "Frequency", 
       title = "Stacked Bar Graph of Diamond Cuts by Clarity")

Position equals “dodge)

ggplot(data = diamonds) + 
  geom_bar(aes(x = cut, fill = clarity), position = "dodge") +
    labs(x = "Diamond Cut", y = "Frequency", 
       title = "Stacked Bar Graph of Diamond Cuts by Clarity")

Change x axis of text

theme is the function call

ggplot(data = diamonds) + 
  geom_bar(aes(x = cut, fill = clarity), position = "dodge") +
    labs(x = "Diamond Cut", y = "Frequency", 
       title = "Stacked Bar Graph of Diamond Cuts by Clarity") + 
  theme(axis.text.x = element_text(angle = 45))

Make x axis lables fit in narrow width

ggplot(data = diamonds) + 
  geom_bar(aes(x = cut, fill = clarity), position = "dodge") +
    labs(x = "Diamond Cut", y = "Frequency", 
       title = "Stacked Bar Graph of Diamond Cuts by Clarity") + 
  scale_x_discrete(labels = function(x) str_wrap(x, width = 5))