library(tidyverse) library(ggplot2) help.search("geom_", package = "ggplot2") # geom TAB - review menu ?diamonds ggplot(data=diamonds) + geom_point(mapping = aes(x=carat, y=price)) # how else can I express this ??geom 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/ ggplot(data=diamonds) + geom_point(mapping = aes(x=carat, y=price), color = "lightskyblue") # built-in help in R colors() # exmample of setting the colot aesthetic outside of the aes function ggplot(data=diamonds) + geom_point(mapping = aes(x=carat, y=price), color = "lightskyblue") # transform the data in the first level of the graphic ggplot(data=diamonds, aes(x=log10(carat), y=log10(price))) + geom_point(color = "lightskyblue") # transform the scale of the data to log 10 ggplot(diamonds, aes(x=carat, y=price)) + geom_point() + scale_x_log10() + scale_y_log10() # specifying aes arguments by position ggplot(diamonds, aes(carat, price)) + geom_point() + scale_x_log10() + scale_y_log10() # transforming the coordinate system: 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 ggplot(diamonds, aes(carat, price)) + geom_point() + geom_smooth(method = "lm") + scale_x_log10() + scale_y_log10() # look at the relationship between city and highway miles per gallon and the class of car ggplot(mpg, aes(x=cty, y=hwy, color=class))+ geom_point() # 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() # labels for points ggplot(filter(mpg, model== "a4"), aes(x=cty, y=hwy))+ geom_text(aes(label=cyl), size = 5) #ggplot(data = ) + # ( # mapping = aes(), # stat = , # position = # ) + # + # p <- ggplot(mpg, aes(displ, cty)) + geom_point() p + facet_grid(. ~ cyl) p + facet_grid(drv ~ .) p + facet_grid(drv ~ cyl) ggplot(data=df, aes(x=fan))+stat_count() # the histogram geometry that sets the binwidth ggplot(mpg, aes(x = cyl)) + geom_histogram(stat = "bin", binwidth=1) # geom_bar does a statistica; transformation counts records ggplot(mpg, aes(x = cyl)) + geom_bar() ggplot(diamonds, aes(x = cut, y = carat)) + geom_bar(stat="identity") statsum <- ggplot(data = diamonds) + stat_summary(mapping = aes(x = cut, y = depth), fun.ymin = min, fun.ymax = max, fun.y = median) ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1)) ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = color, y = ..prop.., group = color)) ggplot(data = diamonds) + stat_summary( mapping = aes(x = cut, y = depth), fun.ymin = min, fun.ymax = max, fun.y = median ) # no points basic color ggplot(diamonds) + geom_pointrange( aes(x = cut, y = depth), stat = "summary", fun.ymin = min, fun.ymax = max, fun.y = median ) #with detailed points ggplot(mpg, aes(trans, cty)) + geom_point() + stat_summary(geom = 'pointrange', fun.ymin = min, fun.ymax = max, fun.y = mean, colour = "red", size = 1) # without points ggplot(mpg, aes(trans, cty)) + stat_summary(geom = 'pointrange', fun.ymin = min, fun.ymax = max, fun.y = mean, colour = "red", size = 1) #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.