ggplot2v3.4.3 快速入门library(tidyverse)
mpg # print(mpg)
#> # A tibble: 234 × 11#> manufacturer model displ year cyl trans drv cty hwy fl class #> <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr> #> 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact#> 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact#> 3 audi a4 2 2008 4 manual(m6) f 20 31 p compact#> # ℹ 231 more rowslibrary(tidyverse)
mpg # print(mpg)
#> # A tibble: 234 × 11#> manufacturer model displ year cyl trans drv cty hwy fl class #> <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr> #> 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact#> 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact#> 3 audi a4 2 2008 4 manual(m6) f 20 31 p compact#> # ℹ 231 more rows# 查看ggplot2包内置数据集mpg的帮助文档?mpg # help(mpg)library(tidyverse)
mpg # print(mpg)
#> # A tibble: 234 × 11#> manufacturer model displ year cyl trans drv cty hwy fl class #> <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr> #> 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact#> 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact#> 3 audi a4 2 2008 4 manual(m6) f 20 31 p compact#> # ℹ 231 more rows# 查看ggplot2包内置数据集mpg的帮助文档?mpg # help(mpg)
displ: a car's engine size, in litres.hwy: a car's fuel efficiency on the highway, in miles per gallon (mpg). class: "type" of carggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, shape = class))
#> Warning: The shape palette can deal with a maximum of 6 discrete values because more than#> 6 becomes difficult to discriminate; you have 7. Consider specifying shapes#> manually if you must have them.#> Warning: Removed 62 rows containing missing values (`geom_point()`).
# 生成辅助数据集best <- mpg %>% group_by(class) %>% filter(row_number(desc(hwy)) == 1)
ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = class)) + geom_text( aes(label = model), data = best )

ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = class)) + geom_label( aes(label = model), data = best, nudge_y = 2, alpha = 0.5 )

theme(line, rect, text, title, aspect.ratio, axis.title, axis.title.x, axis.title.x.top, axis.title.x.bottom, axis.title.y, axis.title.y.left, axis.title.y.right, axis.text, axis.text.x, axis.text.x.top, axis.text.x.bottom, axis.text.y, axis.text.y.left, axis.text.y.right, axis.ticks, axis.ticks.x, axis.ticks.x.top, axis.ticks.x.bottom, axis.ticks.y, axis.ticks.y.left, axis.ticks.y.right, axis.ticks.length, axis.ticks.length.x, axis.ticks.length.x.top, axis.ticks.length.x.bottom, axis.ticks.length.y, axis.ticks.length.y.left, axis.ticks.length.y.right, axis.line, axis.line.x, axis.line.x.top, axis.line.x.bottom, axis.line.y, axis.line.y.left, axis.line.y.right, legend.background, legend.margin, legend.spacing, legend.spacing.x, legend.spacing.y, legend.key, legend.key.size, legend.key.height, legend.key.width, legend.text, legend.text.align, legend.title, legend.title.align, legend.position, legend.direction, legend.justification, legend.box, legend.box.just, legend.box.margin, legend.box.background, legend.box.spacing, panel.background, panel.border, panel.spacing, panel.spacing.x, panel.spacing.y, panel.grid, panel.grid.major, panel.grid.minor, panel.grid.major.x, panel.grid.major.y, panel.grid.minor.x, panel.grid.minor.y, panel.ontop, plot.background, plot.title, plot.title.position, plot.subtitle, plot.caption, plot.caption.position, plot.tag, plot.tag.position, plot.margin, strip.background, strip.background.x, strip.background.y, strip.clip, strip.placement, strip.text, strip.text.x, strip.text.x.bottom, strip.text.x.top, strip.text.y, strip.text.y.left, strip.text.y.right, strip.switch.pad.grid, strip.switch.pad.wrap, ..., complete = FALSE, validate = TRUE)
Keyboard shortcuts
| ↑, ←, Pg Up, k | Go to previous slide |
| ↓, →, Pg Dn, Space, j | Go to next slide |
| Home | Go to first slide |
| End | Go to last slide |
| Number + Return | Go to specific slide |
| b / m / f | Toggle blackout / mirrored / fullscreen mode |
| c | Clone slideshow |
| p | Toggle presenter mode |
| t | Restart the presentation timer |
| ?, h | Toggle this help |
| o | Tile View: Overview of Slides |
| Esc | Back to slideshow |
ggplot2v3.4.3 快速入门library(tidyverse)
mpg # print(mpg)
#> # A tibble: 234 × 11#> manufacturer model displ year cyl trans drv cty hwy fl class #> <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr> #> 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact#> 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact#> 3 audi a4 2 2008 4 manual(m6) f 20 31 p compact#> # ℹ 231 more rowslibrary(tidyverse)
mpg # print(mpg)
#> # A tibble: 234 × 11#> manufacturer model displ year cyl trans drv cty hwy fl class #> <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr> #> 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact#> 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact#> 3 audi a4 2 2008 4 manual(m6) f 20 31 p compact#> # ℹ 231 more rows# 查看ggplot2包内置数据集mpg的帮助文档?mpg # help(mpg)library(tidyverse)
mpg # print(mpg)
#> # A tibble: 234 × 11#> manufacturer model displ year cyl trans drv cty hwy fl class #> <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr> #> 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact#> 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact#> 3 audi a4 2 2008 4 manual(m6) f 20 31 p compact#> # ℹ 231 more rows# 查看ggplot2包内置数据集mpg的帮助文档?mpg # help(mpg)
displ: a car's engine size, in litres.hwy: a car's fuel efficiency on the highway, in miles per gallon (mpg). class: "type" of carggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, shape = class))
#> Warning: The shape palette can deal with a maximum of 6 discrete values because more than#> 6 becomes difficult to discriminate; you have 7. Consider specifying shapes#> manually if you must have them.#> Warning: Removed 62 rows containing missing values (`geom_point()`).
# 生成辅助数据集best <- mpg %>% group_by(class) %>% filter(row_number(desc(hwy)) == 1)
ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = class)) + geom_text( aes(label = model), data = best )

ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = class)) + geom_label( aes(label = model), data = best, nudge_y = 2, alpha = 0.5 )

theme(line, rect, text, title, aspect.ratio, axis.title, axis.title.x, axis.title.x.top, axis.title.x.bottom, axis.title.y, axis.title.y.left, axis.title.y.right, axis.text, axis.text.x, axis.text.x.top, axis.text.x.bottom, axis.text.y, axis.text.y.left, axis.text.y.right, axis.ticks, axis.ticks.x, axis.ticks.x.top, axis.ticks.x.bottom, axis.ticks.y, axis.ticks.y.left, axis.ticks.y.right, axis.ticks.length, axis.ticks.length.x, axis.ticks.length.x.top, axis.ticks.length.x.bottom, axis.ticks.length.y, axis.ticks.length.y.left, axis.ticks.length.y.right, axis.line, axis.line.x, axis.line.x.top, axis.line.x.bottom, axis.line.y, axis.line.y.left, axis.line.y.right, legend.background, legend.margin, legend.spacing, legend.spacing.x, legend.spacing.y, legend.key, legend.key.size, legend.key.height, legend.key.width, legend.text, legend.text.align, legend.title, legend.title.align, legend.position, legend.direction, legend.justification, legend.box, legend.box.just, legend.box.margin, legend.box.background, legend.box.spacing, panel.background, panel.border, panel.spacing, panel.spacing.x, panel.spacing.y, panel.grid, panel.grid.major, panel.grid.minor, panel.grid.major.x, panel.grid.major.y, panel.grid.minor.x, panel.grid.minor.y, panel.ontop, plot.background, plot.title, plot.title.position, plot.subtitle, plot.caption, plot.caption.position, plot.tag, plot.tag.position, plot.margin, strip.background, strip.background.x, strip.background.y, strip.clip, strip.placement, strip.text, strip.text.x, strip.text.x.bottom, strip.text.x.top, strip.text.y, strip.text.y.left, strip.text.y.right, strip.switch.pad.grid, strip.switch.pad.wrap, ..., complete = FALSE, validate = TRUE)