library(tidyverse)
library(here)
#数据
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
mtcars %>%
ggplot(aes(x= mpg, y = wt,colour = cyl)) +
geom_point() +
geom_line() +
geom_line(aes(x= mpg, y = drat),color = "red")#color写在aes里面的话则应该是某种变量,写在外面用引号,某值
#分组
mtcars %>%
ggplot(aes (x = wt,y = hp, group = factor (am), color = gear)) + #所对应的观测点将am因子化后的值进行分组
geom_line() +
geom_point(aes(shape = factor(am)))
mtcars %>%
ggplot(aes (x = wt,y = hp, color = gear)) +
geom_point(aes(color = factor(am),alpha = qsec,size = qsec)) +
geom_point(color = "blue",size = 5,alpha = 0.1)
head(diamonds)
## # A tibble: 6 x 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.290 Premium I VS2 62.4 58 334 4.2 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
diamonds %>%
ggplot(aes(carat)) +
geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#图像调整
diamonds %>% count(cut)
## # A tibble: 5 x 2
## cut n
## <ord> <int>
## 1 Fair 1610
## 2 Good 4906
## 3 Very Good 12082
## 4 Premium 13791
## 5 Ideal 21551
d <- ggplot(diamonds, aes(x = clarity, fill = cut))
d + geom_histogram(aes(x = clarity, y = cut), stat = "identity")#固定的x轴和y轴值来进行作图, 此时需要用stat = “identity” 来申明, 即表示不对数据进行统计变换
## Warning: Ignoring unknown parameters: binwidth, bins, pad
d + geom_bar(position = "dodge")#并排方式,y有几种,每个x下就几条
d + geom_bar(position = "fill")#堆叠图像,并将高度标准化为1
d + geom_bar(position = "stack")#堆叠图像元素
d + geom_point(aes(color, price/carat), color = "blue")
d + geom_point(aes(color, price/carat), color = "blue",position = "jitter")#给点增加扰动避免重合,另一种方式是改变点的透明度,
d + geom_point(aes(color, price/carat,alpha = table), color = "blue")#改变点的透明度,
#给lm, glm, gam, loess, rlm等添加平滑线
m <- ggplot(mtcars,aes (x = wt,y = hp))
m + stat_smooth() + geom_point()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
m + stat_smooth(se = FALSE) + geom_point()#取消默认的置信区间
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
m + stat_smooth(fill = "red", size = 2, alpha = 0.5, color = "green") + geom_point()#更改置信区间和线条颜色
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
m + stat_smooth(method = "lm") + geom_point()#用一元一次线性方程拟合
m + stat_smooth(method = "lm", formula = y ~ poly(x, 3)) + geom_point()#使用一元二次方程拟合
library(splines)
library(MASS)
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
m + stat_smooth(method = "lm", formula = y ~ ns(x, 3)) + geom_point()# 加载splines和MASS包, 使用自由度为3的自然样条来进行拟合
x <- ggplot(mtcars, aes(y = wt, x = mpg, group = factor(cyl)))
x + stat_smooth(method = lm, aes(color = factor(cyl), fill = factor(cyl))) + geom_point( aes(color = factor(cyl)))#按cyl这个离散变量进行分组, 分别拟合数据
#分面
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + facet_grid(. ~ cyl) #以cyl为分类变量
p + facet_wrap( ~ cyl, nrow = 3) #wrap与grid的区别
p + facet_grid(cyl ~ .) #以cyl为分类变量
p + facet_wrap( ~ cyl, ncol = 3) #wrap与grid的区别
p + facet_grid(vs ~ am) #以vs和am为分类变量
p + facet_wrap(vs ~ am, ncol = 2) #wrap与grid 的区别
w <- ggplot(aes(cty, hwy), data = mpg) + geom_point()
w + facet_wrap( ~ cyl)
#调整scales的标度, 共有fixed, free, free_x和free_y四种变换
w + facet_wrap( ~ cyl, scales = "free") # 这里标度更改为free
w + facet_grid(. ~ cyl, scales = "free", space = "free")
#space设置为free时, 每列的宽度与该列的标度范围成比例
##使用自由标度来替代<strong>双坐标轴</strong>的实战的一个例子中
#主题
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
p <- ggplot(mtcars, aes(x = mpg)) + geom_histogram()
p + theme_bw() #白色背景
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
p + theme_grey() #默认浅灰色背景
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
p <- p + labs(title = "histogram")
p + theme(plot.title = element_text(size = 20, color = "red",
hjust = 1, face = "bold",
angle = 45))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
p + theme(panel.background = element_blank()) #blank是去掉某种绘图元素
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#画地图
library(maps)
## Warning: package 'maps' was built under R version 3.6.1
##
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
##
## map
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
crimesm <- reshape2::melt(crimes, id = 1)
if (require(maps)) {
states_map <- map_data("state")
ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder), map = states_map) +
expand_limits(x = states_map$long, y = states_map$lat)
last_plot() + coord_map()
ggplot(crimesm, aes(map_id = state)) +
geom_map(aes(fill = value), map = states_map) +
expand_limits(x = states_map$long, y = states_map$lat) +
facet_wrap( ~ variable)
}
#输出
ggsave( file = "mtcars_plot.png", width = 5, height = 6, type = "cairo", dpi = 600)#输出的是紧挨着上面的那个图