R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

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
#############################################################
#Ex1
df <- ggplot2::mpg

fig <- ggplot(data = df, mapping = aes(x = displ, y = hwy)) +
  geom_point(size = 7, alpha = 1/3, position = "jitter", color = "red") +
 # facet_grid(transmission ~ type_of_drive, scales = "free") +
 # geom_smooth(method = "lm") +
 # coord_cartesian() +
 # scale_y_continuous(breaks = seq(0,50,5)) +
 # scale_x_continuous(breaks = seq(0,10,0.5)) +
  xlab("Engine displacement (volume in litres)") +
  ylab("Highway miles per gallon (MPG)") +
  ggtitle("Car fuel consumption") +
  theme(axis.text = element_text(size = 16),
        axis.title = element_text(size = 20),
        axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 0)),
        axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)),
        plot.title = element_text(size = 30, face = "bold", hjust = 0.5,
                                  margin = margin(t = 0, r = 0, b = 20, l = 0)),
        #strip.background = element_rect(colour = "black", fill = "white"),
        strip.text = element_text(face = "bold", size = 16))


print(fig) # print plot

ggsave(filename = "C:/Users/y_du10/OneDrive - Fort Hays State University/Desktop/KU Data Science Program/Courses/Data 824/Week 1/Part I  Introduction to ggplot2/01 assignment fig1.png", units = "cm", 
       width = 29.7, height = 21, dpi = 600)
###########################################################
#Ex2
df <- ggplot2::mpg

# create new variable transmission from variable trans
df <- df %>% mutate(transmission = substr(trans, 1, 1)) %>% 
  mutate(transmission = case_when(transmission == "a" ~ "automatic trans.",
                                  transmission == "m" ~ "manual trans.")) 


fig <- ggplot(data = df, mapping = aes(x = displ, y = hwy, color = transmission)) +
  geom_point(size = 8, alpha = 1/5, position = "jitter") +
  # facet_grid(transmission ~ type_of_drive, scales = "free") +
  # geom_smooth(method = "lm") +
  # coord_cartesian() +
  # scale_y_continuous(breaks = seq(0,50,5)) +
  # scale_x_continuous(breaks = seq(0,10,0.5)) +
  xlab("Engine displacement (volume in litres)") +
  ylab("Highway miles per gallon (MPG)") +
  ggtitle("Car fuel consumption") +
  theme(axis.text = element_text(size = 16),
        axis.title = element_text(size = 20),
        axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 0)),
        axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)),
        plot.title = element_text(size = 30, face = "bold", hjust = 0.5,
                                  margin = margin(t = 0, r = 0, b = 20, l = 0)),
        #strip.background = element_rect(colour = "black", fill = "white"),
        strip.text = element_text(face = "bold", size = 16))


print(fig) # print plot

ggsave(filename = "C:/Users/y_du10/OneDrive - Fort Hays State University/Desktop/KU Data Science Program/Courses/Data 824/Week 1/Part I  Introduction to ggplot2/01 assignment fig2.png", units = "cm", 
       width = 29.7, height = 21, dpi = 600)
###########################################################
#Ex3
df <- ggplot2::mpg

fig <- ggplot(data = df, mapping = aes(x = cyl, y = hwy)) +
  geom_point(size = 7, alpha = 1/3, position = "jitter", color = "red") +
  # facet_grid(transmission ~ type_of_drive, scales = "free") +
  # geom_smooth(method = "lm") +
  # coord_cartesian() +
  # scale_y_continuous(breaks = seq(0,50,5)) +
  # scale_x_continuous(breaks = seq(0,10,0.5)) +
  xlab("Number of Cylinders") +
  ylab("Highway miles per gallon (MPG)") +
  ggtitle("Car fuel consumption") +
  theme_bw() +
  theme(axis.text = element_text(size = 16),
        axis.title = element_text(size = 20),
        axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 0)),
        axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)),
        plot.title = element_text(size = 30, face = "bold", hjust = 0.5,
                                  margin = margin(t = 0, r = 0, b = 20, l = 0)),
        #strip.background = element_rect(colour = "black", fill = "white"),
        strip.text = element_text(face = "bold", size = 16))


print(fig) # print plot

ggsave(filename = "C:/Users/y_du10/OneDrive - Fort Hays State University/Desktop/KU Data Science Program/Courses/Data 824/Week 1/Part I  Introduction to ggplot2/01 assignment fig3.png", units = "cm", 
       width = 29.7, height = 21, dpi = 600)
###########################################################
#Ex4
df <- ggplot2::mpg

fig <- ggplot(data = df, mapping = aes(x = class, y = hwy)) +
  geom_point(size = 7, alpha = 1/3, position = "nudge", color = "red") +
  # facet_grid(transmission ~ type_of_drive, scales = "free") +
  # geom_smooth(method = "lm") +
  # coord_cartesian() +
  # scale_y_continuous(breaks = seq(0,50,5)) +
  # scale_x_continuous(breaks = seq(0,10,0.5)) +
  xlab("Car Type") +
  ylab("Highway miles per gallon (MPG)") +
  ggtitle("Car fuel consumption") +
  theme_minimal() +
  theme(axis.text = element_text(size = 16),
        axis.title = element_text(size = 20),
        axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 0)),
        axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)),
        plot.title = element_text(size = 30, face = "bold", hjust = 0.5,
                                  margin = margin(t = 0, r = 0, b = 20, l = 0)),
        #strip.background = element_rect(colour = "black", fill = "white"),
        strip.text = element_text(face = "bold", size = 16))


print(fig) # print plot

ggsave(filename = "C:/Users/y_du10/OneDrive - Fort Hays State University/Desktop/KU Data Science Program/Courses/Data 824/Week 1/Part I  Introduction to ggplot2/01 assignment fig4.png", units = "cm", 
       width = 29.7, height = 21, dpi = 600)

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.