data(mpg)
## Warning in data(mpg): data set 'mpg' not found
library(ggplot2)
Obviously, there’s a DATA FRAME which contains the data you’re trying | to plot. Then the AESTHETIC MAPPINGS determine how data are mapped to | color, size, etc. The GEOMS (geometric objects) are what you see in | the plot (points, lines, shapes) and FACETS are the panels used in | conditional plots. You’ve used these or seen them used in the first | ggplot2 (qplot) lesson. STATS are statistical transformations such as | binning, quantiles, and smoothing which ggplot2 applies to the data. | SCALES show what coding an aesthetic map uses (for example, male = | red, female = blue). Finally, the plots are depicted on a COORDINATE | SYSTEM. When you use qplot these were taken care of for you.
qplot(displ, hwy, data=mpg, geom = c("point","smooth"), facets = .~drv)
## `geom_smooth()` using method = 'loess'
g<-ggplot(mpg, aes(displ, hwy))
summary(g)
## data: manufacturer, model, displ, year, cyl, trans, drv, cty, hwy,
## fl, class [234x11]
## mapping: x = displ, y = hwy
## faceting: <ggproto object: Class FacetNull, Facet>
## compute_layout: function
## draw_back: function
## draw_front: function
## draw_labels: function
## draw_panels: function
## finish_data: function
## init_scales: function
## map: function
## map_data: function
## params: list
## render_back: function
## render_front: function
## render_panels: function
## setup_data: function
## setup_params: function
## shrink: TRUE
## train: function
## train_positions: function
## train_scales: function
## vars: function
## super: <ggproto object: Class FacetNull, Facet>
g + geom_point()+geom_smooth()
## `geom_smooth()` using method = 'loess'
the blue line is the confidence band.
By changing the smoothing function to “lm” (linear model) ggplot2 | generated a regression line through the data.
g + geom_point()+geom_smooth(method = "lm")
Using facets
g + geom_point()+geom_smooth(method = "lm")+facet_grid(. ~ drv)
Adding titile
g + geom_point()+geom_smooth(method = "lm")+facet_grid(. ~ drv)+ggtitle("Swirl Rules!")
Modifying geom_point
g + geom_point(color="pink", size=4, alpha = 1/2)+geom_smooth(method = "lm")+facet_grid(. ~ drv)+ggtitle("Swirl Rules!")
another beautiful aestetic
g+geom_point(size=4, alpha = 1/2, aes(color = drv))
Addint titiles and labs
g + geom_point(aes(color = drv)) + labs(title="Swirl Rules!") + labs(x="Displacement", y="Hwy Mileage")
custmize smooth line
g+geom_point(aes(color=drv), size = 2, alpha = 1/2)+geom_smooth(size=4, linetype = 3, method = "lm", se = FALSE)
What did these arguments do? The method specified a linear regression | (note the negative slope indicating that the bigger the displacement | the lower the gas mileage), the linetype specified that it should be | dashed (not continuous), the size made the dashes big, and the se | flag told ggplot to turn off the gray shadows indicating standard | errors (confidence intervals)
Playing with themes
g+geom_point(aes(color=drv))+theme_bw(base_family = "Times")
Simple geom line
g+geom_line()
To ignore the outliers, set limits. To show there is outlier - cer cartesian
g+geom_line()+ylim(-3,3)
g + geom_line() + coord_cartesian(ylim=c(-3,3))
g<-ggplot(data=mpg, aes(x=displ, y=hwy, color=factor(year)))
Adding margins. The margins argument tells ggplot to display the | marginal totals over each row and column, so instead of seeing 3 rows | (the number of drv factors) and 4 columns (the number of cyl factors) | we see a 4 by 5 display
g+geom_point()+ facet_grid(drv~cyl, margins = TRUE)
Adding smoothers
g+geom_point()+ facet_grid(drv~cyl, margins = TRUE)+geom_smooth(method="lm", se = FALSE, size = 2, color = "black")
Adding labs
g + geom_point() + facet_grid(drv~cyl,margins=TRUE)+geom_smooth(method="lm",size=2,se=FALSE,color="black")+labs(x="Displacement",y="Highway Mileage",title="Swirl Rules!")