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


# eval = FALSE
## ?barplot


# eval = FALSE
## # 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"))


# eval = FALSE
## 
## pdf("Name_of_Plot.pdf", height = 3.5, width = 5.25)
## 
## ## PLOT CODE HERE
## 
## dev.off()


# 
#install.packages(ggplot2)
library(ggplot2)



# 
titanic <- read.delim("http://myweb.uiowa.edu/pbreheny/data/titanic.txt")

ggplot(titanic, aes(x = Class)) +
  geom_bar(position = "stack", fill="forest green") +
  ylab("Number of Individuals in each Class") +
  xlab("Class")


# 
ggplot(titanic, aes(x = Survived)) +
  geom_bar(position = "stack", fill="forest green") +
  ggtitle("Death and Survival Counts for Individuals on the Titanic") +
  theme(plot.title = element_text(hjust = 0.5))


# 
ggplot(titanic, aes(x = Class)) +
  geom_bar(position = "stack", aes(fill = Survived))+
  xlab("Class")


# 
ggplot(titanic, aes(x = Class)) +
  geom_bar(position = "stack", aes(fill = Survived)) +
  facet_wrap(c("Age", "Sex")) +
  xlab("Class") 

ggplot(titanic, aes(x = Class)) +
  geom_bar(position = "stack", aes(fill = Survived)) +
  facet_wrap(c("Age", "Sex"), scales = "free") +
  xlab("Class")


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


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


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

abline(a = 10, b = 0)

# Or 

abline(h = 10)


# 

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


# message = FALSE
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()

