library(tidyverse) library(ggplot2) # 1. Find help on the ggplot2 package help.search("geom_", package = "ggplot2") # geom TAB - review menu # 2. Investigate the diamonds dataset and create a scatter plot of carat versus price ?diamonds # 3. how else can I express this chart ?? ??geom # 4. Change the color of the points to a different color ggplot(data=diamonds) + geom_point(mapping = aes(x=carat, y=price), color = "blue") # why is the color outside of aes? # want a different shade consult # http://research.stowers.org/mcm/efg/R/Color/Chart/ # 5. Change to one from the colors listed in the chart above ggplot(data=diamonds) + geom_point(mapping = aes(x=carat, y=price), color = "lightskyblue") # 6. Investigate the colors() function using the built-in help in R colors() # 7. create another scatter plot where carat and price are transformed with the log10 function in the first level of the graphic # 8. transform the scale of the data to log 10 using one of the scale_* functions # 9. specifying aes arguments by position as opposed to x=, y= # 10. transforming the coordinate system for the plot from #7 ggplot(diamonds, aes(carat, price)) + geom_point() + coord_trans(x = "log10", y = "log10") # coordinate transformation are done after any statistical transformations # scale transforations are done BEFORE statistical functions # 11. add a smoothing line using geom_smooth to plot 10 and plot 11 # 12. look at the relationship between city and highway miles per gallon and the # class of car so 3 variables represented in your graph # 13. now lets add another geometry that smooths the data # it adds a line and a ribbon that should represent spread ggplot(mpg, aes(x=cty, y=hwy))+ geom_point() + geom_smooth() # 14. plot the data for car model "a4" and add labels for points #ggplot(data = ) + # ( # mapping = aes(), # stat = , # position = # ) + # + # # using p as define below p <- ggplot(mpg, aes(displ, cty)) + geom_point() # 15 add a facet grid for cyl variable p + facet_grid(. ~ cyl) # 16. now add a facet grid for drv. p + facet_grid(drv ~ .) # 16. now add a facet grid for cyl and drv. p + facet_grid(drv ~ cyl) ggplot(data=df, aes(x=fan))+stat_count() # 17 create for the cyl variavle - using the histogram geometry that sets the binwidth # 18 now use geom_bar to generate the data/statistics; transformation counts records # 19 using the diamonds dataset and geom_bar plot cut versus carat ggplot(diamonds, aes(x = cut, y = carat)) + geom_bar(stat="identity") # 20 use stat_summary to sisplay the min, max, median for depth for each diamond cut # 21 use geom_bar to create proportion plot #22 use geom_bar to create a stacked bar #23 # no points basic color ggplot(diamonds) + geom_pointrange( aes(x = cut, y = depth), stat = "summary", fun.ymin = min, fun.ymax = max, fun.y = median ) # 24 use stat_summary and the geom = 'pointrange' to create a summary of trans to cty for mpg # where you set the color of the summary points # 25 remove the detailed points from the chart above #Scales #Aesthetic mapping (i.e., with aes()) only says that a variable should be mapped #to an aesthetic. It doesn't say how that should happen. For example, when #mapping a variable to shape with aes(shape = x) you don't say what shapes #should be used. Similarly, aes(color = z) doesn't say what colors should be # used. Describing what colors/shapes/sizes etc. to use is done by modifying the # corresponding scale. In ggplot2 scales include #position #color and fill #size #shape #line type #Scales are modified with a series of functions using a scale__ #naming scheme.