HS/HSL R Workshop Live Code


Visualizing a built-in data set

  1. get working directory for current project

    getwd()
  2. set working directory for current project
    (put your cursor between the two quotes below and press the "tab" key)

    setwd(dir = "")
  3. list the files in your working directory

    list.files()
  4. install ggplot2

    install.packages("ggplot2")
  5. load ggplot2 into the current workspace

    library(ggplot2)
  6. load the diamonds dataset into the current workspace

    data(diamonds)
  7. show the first few lines of diamonds data

    head(diamonds)
  8. view the diamonds data

    View(diamonds)
  9. show the documentation for the diamonds data

    help(diamonds)
  10. summarize the diamonds data

    summary(diamonds)
  11. use ggplot2 to show the distribution of diamond clarity types

    bar1 <- ggplot(diamonds, aes(x=clarity)) + geom_bar() + labs(title="Counts of diamond clarities")
  12. show plot

    bar1
  13. plot the amount of each type of diamond cut within each type of diamond clarity

    bar2 <- ggplot(diamonds, aes(x=clarity, fill=cut)) + geom_bar() + labs(title="Counts of diamond clarities filled by cut count")
  14. show plot

    bar2
  15. create a scatterplot showing the relationship between a diamonds weight and price

    plot1 <- ggplot(diamonds, aes(x=carat, y=price)) + geom_point()
  16. show plot

    plot1
  17. use the color of each dot to show a third variable, diamond clarity

    plot2 <- ggplot(diamonds, aes(x=carat, y=price, color=clarity)) + geom_point()
  18. show plot

    plot2
  19. replace the third variable with color

    plot3 <- ggplot(diamonds, aes(x=carat, y=price, color=color)) + geom_point()
  20. show plot

    plot3
  21. use the size of each dot to plot a fourth variable, diamond cut

    plot4 <- ggplot(diamonds, aes(x=carat, y=price, color=clarity, size=cut)) + geom_point()
  22. show plot

    plot4
  23. or use the shape of each dot to plot a fourth variable

    plot5 <- ggplot(diamonds, aes(x=carat, y=price, color=clarity, shape=cut)) + geom_point()
  24. show plot

    plot5
  25. show a smoothing trend of the relationship between two variables

    plot6 <- ggplot(diamonds, aes(x=carat, y=price)) + geom_point() + geom_smooth()
  26. show plot

    plot6
  27. remove the confidence interval

    plot7 <- ggplot(diamonds, aes(x=carat, y=price)) + geom_point() + geom_smooth(se=FALSE)
  28. show plot

    plot7
  29. show the linear model instead of the curve

    plot8 <- ggplot(diamonds, aes(x=carat, y=price)) + geom_point() + geom_smooth(se=FALSE, method="lm")
  30. show plot

    plot8
  31. add a third variable to create smoothing curves for each category of the new variable

    plot9 <- ggplot(diamonds, aes(x=carat, y=price, color=clarity)) + geom_point() + geom_smooth(se=FALSE)
  32. show plot

    plot9
  33. show only the layer of the different smoothing curves

    plot10 <- ggplot(diamonds, aes(x=carat, y=price, color=clarity)) + geom_smooth(se=FALSE)
  34. show plot

    plot10
  35. facet three variables over the different categories of a fourth variable, start with three variables

    plot11 <- ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point()
  36. show plot

    plot11
  37. add the facet function for the fourth variable, clarity

    plot11 <- ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point() + facet_wrap(~ clarity)
  38. show plot

    plot11
  39. give the plot a title

    plot11 <- ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point() + facet_wrap(~ clarity) + ggtitle("Diamond carat, price, and cut by level of diamond clarity")
  40. show plot

    plot11
  41. save the plot as a .png in your current working directory

    ggsave(filename="diamonds.png", plot11)
  42. save the plot as a .jpg in your current working directory

    ggsave(filename="diamonds.jpg", plot11)
  43. save the plot as a .pdf in your current working directory

    ggsave(filename="diamonds.pdf", plot11)
  44. save the plot as a .png in your current working directory

    ggsave(filename="diamonds.png", plot11)
  45. save the plot as a .jpg in your current working directory

    ggsave(filename="diamonds.jpg", plot11)
  46. save the plot as a .pdf in your current working directory

    ggsave(filename="diamonds.pdf", plot11)