Facets
1. facet by one variable
The variable that you pass to facet_wrap()
should be discrete.
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ, y = hwy)) +
+ facet_wrap(~ class, nrow = 4) # row: n=4
nrow
: facet in n rows
ncol
: facet in n columns
2. facet by two variable
> ggplot(data = mpg)+
+ geom_point(mapping = aes(x = displ, y = hwy))+
+ facet_grid(drv ~ cyl)
3. not facet in the rows or columns dimension
use a .
instead of a variable name
> ggplot(data = mpg)+
+ geom_point(mapping = aes(x = displ, y = hwy))+
+ facet_grid(. ~ cyl)
- difference
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ, y = hwy)) +
+ facet_grid(drv ~ .) # row
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ, y = hwy)) +
+ facet_grid(. ~ cyl) # column
Geometric objects
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy))
1. linetype
geom_smooth()
will draw a different line, with a different linetype, for each unique value of the variable that you map to linetype.
> ggplot(data = mpg) +
+ geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv))
2. group
> ggplot(data = mpg) +
+ geom_smooth(mapping = aes(x = displ, y = hwy))
> ggplot(data = mpg) +
+ geom_smooth(mapping = aes(x = displ, y = hwy, group = drv))
> ggplot(data = mpg) +
+ geom_smooth(
+ mapping = aes(x = displ, y = hwy, color = drv),
+ show.legend = FALSE #不显示图例
+ )
3. display multiple geoms in one plot
add multiple geom functions to ggplot()
:
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ, y = hwy)) +
+ geom_smooth(mapping = aes(x = displ, y = hwy))
A better way !
ggplot2 will treat these mappings as global mappings that apply to each geom in the graph.
> ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
+ geom_point() +
+ geom_smooth()
- If you place mappings in a geom function, ggplot2 will treat them as local mappings for the layer. It will use these mappings to extend or overwrite the global mappings for that layer only.
This makes it possible to display different aesthetics in different layers.
> ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
+ geom_point(mapping = aes(color = class)) +
+ geom_smooth()
- You can use the same idea to specify different
data
for each layer. Our smooth line displays just a subset of thempg
dataset.
The subcompact cars. The local data argument in geom_smooth()
overrides the global data argument in ggplot()
for that layer only.
> library(dplyr) #load dplyr !!! filter() is from dplyr
> ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
+ geom_point(mapping = aes(color = class)) +
+ geom_smooth(data = filter(mpg, class == "subcompact"), se = FALSE)
Exercises
1. What geom would you use to draw a line chart? A boxplot? A histogram? An area chart?
A line chart
> ggplot(data = mpg)+
+ geom_line(mapping = aes(x = hwy, y = cty))
A boxplot
> ggplot(data = mpg)+
+ geom_boxplot(mapping = aes(x = displ, y = hwy, group = class))
A histogram
> ggplot(data = mpg)+
+ geom_histogram(mapping = aes(x = hwy), bins = 30) #条数是30
An area chart
> ggplot(data = mpg)+
+ geom_area(mapping = aes(x = displ, y = hwy))
2. Run this code in your head and predict what the output will look like. Then, run the code in R and check your predictions.
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) +
geom_point() +
geom_smooth(se = FALSE)
3. What does the se
argument to geom_smooth()
do?
> ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) +
+ geom_point() +
+ geom_smooth(se = TRUE)
standard error(se):
The
se
parameter adds a standard error band to the fitted curve(添加的标准误差,默认TRUE)
4. show.legend = FALSE
: 不显示图例
5. Recreate the R code necessary to generate the following graphs.
- (1)
> ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
+ geom_point() +
+ geom_smooth(se = FALSE)
- (2)
> ggplot(data = mpg, mapping = aes(x = displ, y = hwy, group = drv))+
+ geom_point()+
+ geom_smooth(se = FALSE)
- (3)
> ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv))+
+ geom_point()+
+ geom_smooth(se = FALSE)
- (4)
#1
> ggplot(data = mpg)+
+ geom_point(mapping = aes(x = displ, y = hwy, color = drv))+
+ geom_smooth(mapping = aes(x = displ, y = hwy), se = FALSE)
#2
> ggplot(data = mpg, mapping = aes(x = displ, y = hwy))+
+ geom_point(mapping = aes(x = displ, y = hwy, color = drv))+
+ geom_smooth(se = FALSE)
- (5)
> ggplot(data = mpg)+
+ geom_point(mapping = aes(x = displ, y = hwy, color = drv))+
+ geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv), se = FALSE)
- (6)
> ggplot(data = mpg)+
+ geom_point(mapping = aes(x = displ, y = hwy, color = drv))