Bar Charts with Diamonds Dataset

Author

IL

Access Library package - Tidyverse

# Load the libraries
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
#"tidyverse package will automatically load ggplot2"

Load the pre-built dataset, Diamonds, and view it in the global environment

view (diamonds)
head(diamonds) #displays the first few lines of the data set
# 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
data(diamonds) # places the dataset in the global environment

Create bar chart with geom_bar()

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

Create bar chart with fill=cut

ggplot(data=diamonds, aes(x=cut, fill=cut)) +
  geom_bar (alpha=0.4)  #change transparency with alpha

Bar chart with 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")

##Bar chart with position = “fill” to stretch out bars and fill the vertical space proportionally

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

##Bar chart with position = “dodge” to get side by side bars

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

Bar chart with the angle of the x-axis labels changed

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

Bar chart with the x-axis labels fitting in a narrow width

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