ggplot2 —— Facets & Geometric ob

2020-04-25  本文已影响0人  超懒的懒懒

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)
> 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
facet_grid(drv ~ .)
facet_grid(. ~ cyl)

Geometric objects

ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy))

ggplot(data = mpg) + 
  geom_smooth(mapping = aes(x = displ, y = hwy))
point geom
smooth geom

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()

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()

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)
This command selects only the subcompact cars.

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 line chart

A boxplot

> ggplot(data = mpg)+
+     geom_boxplot(mapping = aes(x = displ, y = hwy, group = class))
A boxplot

A histogram

> ggplot(data = mpg)+
+         geom_histogram(mapping = aes(x = hwy), bins = 30)  #条数是30
A histogram

An area chart

> ggplot(data = mpg)+
+    geom_area(mapping = aes(x = displ, y = hwy))
An area chart
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)
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)
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.
> ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
+        geom_point() + 
+        geom_smooth(se = FALSE)
> ggplot(data = mpg, mapping = aes(x = displ, y = hwy, group = drv))+
+      geom_point()+
+      geom_smooth(se = FALSE)
> ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv))+
+           geom_point()+
+           geom_smooth(se = FALSE)
#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)
> 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)
> ggplot(data = mpg)+
+ geom_point(mapping = aes(x = displ, y = hwy, color = drv))
上一篇 下一篇

猜你喜欢

热点阅读