knitr::opts_chunk$set(echo = TRUE)

counts <- c(8, 15, 11, 16)
barplot(height = counts, 
        names.arg = c("I", "II", "III", "IV"), 
        col = "blue", 
        main = "Distribution of Cancer Stage", 
        xlab = "Stage",
        ylab = "Frequency")

## ?barplot

## # a: intercept
## # b: slope
## # h: the y-value(s) for horizontal line(s)
## # v: the x-value(s) for vertical line(s)
## abline(a, b, h, v)
## 
## # x: x coordinate for the text
## # y: y coordinate for the text
## # "Text": The text to be written
## text(x, y, "Text")

counts <- c(8, 15, 11, 16)
barplot(height = counts, 
        names.arg = c("I", "II", "III", "IV"), 
        col = "pink", 
        main = "Distribution of Cancer Stage", 
        xlab = "Stage",
        ylab = "Frequency")
abline(h = c(8, 15), lty = 2, lwd = 4, col = c("blue", "red"))

stages <- c("Stage 1 Freq", "Stage 2 Freq")
legend(x = "topleft", 
       legend = stages,
       lty = 2,
       lwd = 4,
       col = c("blue", "red"))

## # setwd("H:/HawkID/BIOS4120Labs") # set working directory to the folder where you want the image saved
## 
## pdf("Name_of_Plot.pdf", height = 3.5, width = 5.25)
## 
## ## INSERT PLOT CODE HERE
## 
## dev.off()

titanic <- read.delim('https://raw.githubusercontent.com/IowaBiostat/data-sets/main/titanic/titanic.txt')

barplot(height = table(titanic$Survived, titanic$Class),
        col = c("blue", "red"),
        main = "Survival Between Classes")
legend(x = "topleft",
       legend = c("Died", "Survived"),
       title = "Survival Status",
       fill = c("blue", "red"))

par(mfrow=c(1,2)) # To turn it back to normal, run par(mfrow = c(1,1))

par(mfrow=c(1,2))

# Bar plot for the females
barplot(height = table(titanic$Survived, titanic$Class, titanic$Sex)[,,1],
        col = c("blue", "red"), 
        main = "Female Survival vs Classes")
legend(x = "topleft", 
       legend = c("Died", "Survived"), 
       title = "Survival Status", 
       fill = c("blue", "red"),
       cex = 0.8) # shrinks size of legend
# Bar plot for the males
barplot(height = table(titanic$Survived, titanic$Class, titanic$Sex)[,,2],
        col = c("blue", "red"), 
        main = "Male Survival vs Classes")
legend(x = "topleft", 
       legend = c("Died", "Survived"), 
       title = "Survival Status", 
       fill = c("blue", "red"),
       cex = 0.8)

people <- c(16, 21, 10, 13)

par(mfrow = c(1,1)) # turns plot region back to "normal"
barplot(people)

barplot(people, 
        main = "Iowa City Night Life")

barplot(people, 
        main = "Iowa City Night Life",
        ylim = c(0,25))

## Create a vector named "bars" that contains the names of the bars.

restaurants <- c("Shorts", "Donnelly's Pub", "Blue Moose", "Joe's Place")

## Add the bar categories to the bar plot

barplot(people, 
        main = "Iowa City Night Life",
        ylim = c(0,25),
        names.arg = restaurants)

## Label the x and y-axis

barplot(people, 
        main = "Iowa City Night Life",
        ylim = c(0,25),
        names.arg = restaurants,
        ylab = "Number of People",
        xlab = "Restaurants in Iowa City")

# Make the line green, dotted line
barplot(people, 
        main = "Iowa City Night Life",
        ylim = c(0,25),
        names.arg = restaurants,
        ylab = "Number of People",
        col = "orange")

abline(h = 10, col = "green", lty = 2, lwd = 2)

## Label the line

text(x = 3, y = 11, labels = "Fun Threshold")

barplot(people, 
        main = "Iowa City Night Life",
        ylim = c(0,25),
        names.arg = restaurants,
        ylab = "Number of People",
        col = "orange")

abline(h = 10, col = "green", lty = 2, lwd = 2)

legend(x = "topright",
       legend = "Fun Threshold",
       lty = 2,
       lwd = 2,
       col = "green")

pdf("Bar-Plot-01.pdf", height = 3.5, width = 7.25)

barplot(people, 
        main = "Iowa City Night Life",
        ylim = c(0,25),
        names.arg = restaurants,
        ylab = "Number of People",
        col = "orange")

abline(h = 10, col = "green", lty = 2)

legend(x = "topright",
       legend = "Fun Threshold",
       lty = 2,
       col = "green")

dev.off()
