Bar Chart

Import the Package

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

Get data insight

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

Set the data globally

data(diamonds)

Draw the first bar chat

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

demo <- tribble(
~cut, ~freq,
"Fair", 1610,
"Good", 4906,
"Very Good", 12082,
"Premium", 13791,
"Ideal", 21551
)

Draw the first bar chat

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.

Draw Statitics

ggplot(data = diamonds) +
stat_summary(
mapping = aes(x = cut, y = depth),
fun.min = min,
fun.max = max,
fun = median
)

Draw Graph with colors

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

Draw Graph with colors

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")

Draw Graph with colors

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")

Draw Graph with colors

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

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