R Markdown

This is an R Markdown document.

library(ggplot2)

avoid warning msg in library

suppressWarnings(suppressMessages(library(ggplot2)))
suppressWarnings(suppressMessages(library(gridExtra)))
data(package="ggplot2")
str(economics)
## spc_tbl_ [574 × 6] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ date    : Date[1:574], format: "1967-07-01" "1967-08-01" ...
##  $ pce     : num [1:574] 507 510 516 512 517 ...
##  $ pop     : num [1:574] 198712 198911 199113 199311 199498 ...
##  $ psavert : num [1:574] 12.6 12.6 11.9 12.9 12.8 11.8 11.7 12.3 11.7 12.3 ...
##  $ uempmed : num [1:574] 4.5 4.7 4.6 4.9 4.7 4.8 5.1 4.5 4.1 4.6 ...
##  $ unemploy: num [1:574] 2944 2945 2958 3143 3066 ...

Unique values of the economics

#change ALL character variables in to factor(categorical) #variable

economics=as.data.frame(unclass(economics),
                       stringsAsFactors = TRUE)
str(economics)
## 'data.frame':    574 obs. of  6 variables:
##  $ date    : Date, format: "1967-07-01" "1967-08-01" ...
##  $ pce     : num  507 510 516 512 517 ...
##  $ pop     : num  198712 198911 199113 199311 199498 ...
##  $ psavert : num  12.6 12.6 11.9 12.9 12.8 11.8 11.7 12.3 11.7 12.3 ...
##  $ uempmed : num  4.5 4.7 4.6 4.9 4.7 4.8 5.1 4.5 4.1 4.6 ...
##  $ unemploy: num  2944 2945 2958 3143 3066 ...

#density plot (smoothed histogram)

ggplot(economics, aes(x=date)) + 
  geom_density(color="violet",size=2)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

#histogram with density plot

ggplot(economics, aes(x=date))+
  geom_histogram(aes(y = ..density..),
                 col="darkred",fill="yellow",
                 position = "identity")+
  geom_density(col="tan3",size = 2)
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

dotplot in ggplot

ggplot(economics, aes(x=date)) +
  geom_dotplot(dotsize=0.5,color="red",fill="violet")
## Bin width defaults to 1/30 of the range of the data. Pick better value with
## `binwidth`.

#Area Plot

ggplot(economics, aes(unemploy))+
  geom_area(stat = "bin",color="red",fill='blue')
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#Pie Chart - Univariate # Create a new categorical variable by binning ‘pce’ for pie chart

economics$grouped_pce <- cut(economics$pce, breaks=4, labels=c("Low", "Medium", "High", "Very High"))
ggplot(economics, aes(x = factor(""), fill = grouped_pce)) +
  geom_bar(width=1) +
  coord_polar(theta = "y") +
  theme(axis.ticks=element_blank(),  
        axis.title=element_blank(),  
        axis.text.y=element_blank(),
        axis.text.x=element_blank(),
        panel.grid  = element_blank(),
        legend.position = "bottom") +
  labs(title = "Economics Data - PCE Categories (Pie Chart)",
       caption="Grouped PCE from the economics dataset")

Donut Chart

# Create a new categorical variable by binning 'unemploy' for the donut chart

economics$grouped_unemploy <- cut(economics$unemploy, breaks=4, labels=c("Low", "Medium", "High", "Very High"))

ggplot(economics, aes(x = 2, fill = grouped_unemploy)) +
  geom_bar(width=1) +
  coord_polar(theta = "y") +
  theme(axis.ticks=element_blank(),  
        axis.title=element_blank(),  
        axis.text.y=element_blank(),
        axis.text.x=element_blank(),
        panel.grid  = element_blank(),
        legend.position = "bottom") +
  labs(title = "Economics Data - Unemployment Categories (Donut Chart)",
       caption="Grouped Unemployment from the economics dataset") +
  xlim(0, 2.5)