positioning_L07.Rmd - title\"Positioning Lab(L07 author\"Siyu Zhao date output html_document Overview We used commands like `facet`s `position` and

positioning_L07.Rmd - title"Positioning Lab(L07...

This preview shows page 1 - 3 out of 7 pages.

--- title: "Positioning Lab (L07)" author: "Siyu Zhao" date: "January 28, 2016" output: html_document --- # Overview We used commands like `facet`s, `position` and `coord`s and to visualize different data such as cdc, economics_long and CA_census. # Datasets We will be utilizing the familiar `cdc.txt` dataset, the `CA_census.txt` dataset, and the built in `economics_long` dataset (or `economics` dataset). For explanation of variables for `cdc.text` see earlier labs labs, for `economics_long` simply use `?economics_long`, and for `CA_census.txt` see the bottom of the `census_data_cleaner.R` script from lab L05. # Task Run the provided R code, just below, prior to completing the 5 tasks. ```{r, eval = FALSE} library(ggplot2) library(dplyr) library(reshape2) # Read in the cdc dataset cdc <- read.table(file = "cdc.txt",sep="|",header = TRUE) # Selecting a random subset of size 100; set the seed for reproducibility set.seed(8221984) cdc_small <- cdc[sample(x = nrow(cdc),size = 1000,replace = FALSE),] ``` <br><br> **It is expected that all plots will be properly labeled (this now includes legends).** <br><br> 1. Using the `cdc_small` dataset construct graphic that displays a scatter plot of `wtdesire-weight` by `weight` for each `genhlth` by `gender` group (i.e. excellent and male group, excellent and female group, ...) with the following requirements (*Hint: You should be using faceting.*): * `genhlth should be in the columns position. * `gender` should be in the rows position. * The faceting headers should have appropriate and clear labels. * `genhlth` should be appropriately ordered. * The plots for each category of `genhlth` should have all points for men and women colored grey80. Then have the points for the plot's gender colored (your choice of colors to identify `gender`). The effect is to have the opposite gender's points in the background of each scatter plot giving a better idea of relative position within each conditional scatter plot. * Do not include the color legend for `gender` since faceting clearly identifies gender. * Include appropriate titles for axes and the graphic. ```{r} # Construct a new variable - wtdifference wtdifference <- cdc_small$wtdesire - cdc_small$weight # Rename the weight and gender variables in cdc_small weight_sample <- cdc_small$weight gender_sample <- cdc_small$gender
# Relabel the gender levels gender_sample <- factor(gender_sample, labels = c("Female", "Male")) # Reorder and relabel genhlth levels cdc_small$genhlth <- factor(cdc_small$genhlth,levels = c("excellent","very good","good","fair", "poor"), labels = c("Excellent", "Very Good", "Good", "Fair", "Poor")) # Rename genhlth in cdc_small genhlth_sample <- cdc_small$genhlth # Combine the variables into one dataset dat1 <- cbind.data.frame(weight_sample, wtdifference, gender_sample, genhlth_sample) # Create another dataset without gender_sample hh <- select(.data = dat1, -gender_sample) # Make the plot. Make the female's red and male's blue ggplot(data = dat1, aes(x = weight_sample, y = wtdifference)) + ggtitle("Amount of Desired Weight Loss/Gain by Weight for Specified Groups") + scale_x_continuous(name = "Weight in Pounds") + scale_y_continuous(name = "Weight Loss/Gain in Pounds") + geom_point(data = hh, color = "grey80") + geom_point(aes(color = gender_sample), show.legend = FALSE) +

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture