This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.

# Stacked Bar Chart

# Load necessary libraries
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
# Create or modify 'cyl4' column
mtcars$cyl4 <- ifelse(mtcars$cyl == 4, "4 cylinder", "Not 4 cylinder")


# Modify am variable to have descriptive labels
mtcars$am <- factor(mtcars$am, levels = c(0, 1), labels = c("Automatic", "Manual"))

# Create a table
mtcars_table <- with(mtcars, table(am, cyl4))


# Create a data frame from the table
df <- as.data.frame(mtcars_table)
names(df) <- c("Transmission", "FourCyl", "Count")

# Create interactive plot
fig <- df %>%
  plot_ly(x = ~Transmission, y = ~Count, color = ~FourCyl,
          type = "bar", split = ~FourCyl) %>%
  layout(barmode = 'stack', title="Car Specs",
         xaxis=list(title="Transmission"),
         yaxis=list(title="Number of Cars"))

fig
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
# Histogram with Skewed Data
# Load necessary libraries
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
# Load diamonds dataset
data(diamonds)

# Calculate median and IQR
median_price <- median(diamonds$price)
IQR_price <- IQR(diamonds$price)
Q1_price <- quantile(diamonds$price, 0.25) # First Quartile (Q1)
Q3_price <- quantile(diamonds$price, 0.75) # Third Quartile (Q3)

# Create histogram
ggplot(diamonds, aes(x = price)) +
  geom_histogram(aes(y = ..density..), binwidth = 500, fill = "steelblue", alpha = 0.7) +
  
  # Add vertical lines for Median and IQR
  geom_vline(aes(xintercept = median_price), color="blue", linetype="dashed", size=1) +
  geom_vline(aes(xintercept = Q1_price), color="green", linetype="dashed", size=1) +
  geom_vline(aes(xintercept = Q3_price), color="green", linetype="dashed", size=1) +
  
  labs(
    title ="Histogram with Median and Interquartile Range",
    subtitle = "Price data from 'diamonds'",
    x = "Price ($)",
    y = "Density"
  ) +
  
  # Adding text to denote Median and IQR 
  annotate("text", x = c(Q1_price -1000 , Q3_price +2000 ,median_price),
           y=c(.00001,.00001,.00002),
           label=c(paste("Q1 =", round(Q1_price)),
                   paste("Q3 =", round(Q3_price)),
                   paste("Median =", round(median_price))),
           colour=c("green","green","blue")) +
  
  theme_minimal() # For prettier theme
## 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.
## 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.

# Histogram with Normal Data
# Load necessary libraries
library(ggplot2)
library(dplyr)

# Calculate mean and standard deviation of mpg
mean_mpg <- mean(mtcars$mpg)
sd_mpg <- sd(mtcars$mpg)

# Create histogram
ggplot(mtcars, aes(x = mpg)) +
  geom_histogram(aes(y = ..density..), binwidth = 1, fill = "steelblue", alpha = 0.7) +
  
  # Add lines for Mean and Mean ± SD
  geom_vline(aes(xintercept = mean_mpg), color="blue", linetype="dashed", size=1) +
  geom_vline(aes(xintercept = mean_mpg - sd_mpg), color="red", linetype="dashed", size=1) +
  geom_vline(aes(xintercept = mean_mpg + sd_mpg), color="red", linetype="dashed", size=1) +
  
  labs(
    title ="Histogram with Mean and Standard Deviation",
    subtitle = "Miles Per Gallon ('mpg') from 'mtcars'",
    x = "Miles Per Gallon",
    y = "Density"
  ) +
  
  # Adding text to denote Mean and SD 
  annotate("text", x = c(mean_mpg - sd_mpg , mean_mpg + sd_mpg ,mean_mpg),
           y=c(.02,.02,.03),
           label=c(paste("Mean - SD =", round(mean_mpg - sd_mpg, digits=2)),
                   paste("Mean + SD =", round(mean_mpg + sd_mpg, digits=2)),
                   paste("Mean =", round(mean_mpg, digits=2))),
           colour=c("red","red","blue")) +
  
  theme_minimal() # For prettier theme

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Cmd+Option+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Cmd+Shift+K to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.