pacman::p_load(dplyr,ggplot2,datasauRus,tidyverse,gghalves,patchwork)
01 散点图
#查看datasauRus数据
datasaurus_dozen %>%
View()
#显示对象的结构
str(datasaurus_dozen)
#删除重复的元素/行
unique(datasaurus_dozen$dataset)
#筛选dataset每个数据xy的平均值、方差等数据
datasaurus_dozen %>%
group_by(dataset) %>%
summarize(
mean_x= mean(x),
mean_y = mean(y),
std_dev_x = sd(x),
std_dev_y = sd(y),
corr_x_y = cor(x,y)
)
#按照类别画散点图,使用facet_wrap()函数通过“dataset列”进行分面展示。
ggplot(datasaurus_dozen, aes(x=x, y=y,colour=dataset))+
geom_point()+
theme_void()+
theme(legend.position = "none")+
facet_wrap(~dataset, ncol=3)
用法:
ggplot(data=数据源) + <GEOM_FUNCTION> (
mapping=aes(x=,y=),
stat=<STAT> ,
position=<POSSITION>)+
<COORDINATE_FUNCION> +
<FACET_FUNCTION>
1.在aes内添加,给第三个变量增加样式
alpha=变量
alpha=factor(变量)
如果在aes外增加属性则适用于所有特点:color 边框颜色,fill填充颜色
2.分割成多个图facet_wrap(~变量)
其中,两个变量对图形进行分面 facet_grid(变量1~变量2)
3.stat statistical transformation
stat_count()
stat_summary()
4.Positon5种方式
point---① jitter (抖动->分散,原理point并不是一个精确点) 快速实现 geom_jitter()
bar---② indentity / ③fill / ④dodge / ⑤stack
添加主题和背景
ggplot2有多个内置主题,其中:
默认主题是theme_grey(),淡灰色背景和白色网格
theme_bw(): 是theme_grey()的变体,白色背景和灰色网格线theme_linedraw(): 白色背景黑色线条
theme_light(): 和theme_linedraw()很像,区别是线条为灰色theme_dark():黑色背景的theme_light(),可以用来画薄彩色线条
theme_minimal():简约主题
theme_classic(): 只有x、y轴没有背景和网格线
theme_void(): 完全空白的主题
theme(legend.position = "none") 图例的位置,“ none”、“ left”、“ right”、“ bottom”、“ top”或双元素数值向量
> unique(datasaurus_dozen$dataset)
[1] "dino" "away" "h_lines" "v_lines" "x_shape"
[6] "star" "high_lines" "dots" "circle" "bullseye"
[11] "slant_up" "slant_down" "wide_lines"
>datasaurus_dozen %>% group_by(dataset) %>% summarize()
# A tibble: 13 x 6
dataset mean_x mean_y std_dev_x std_dev_y corr_x_y
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 away 54.3 47.8 16.8 26.9 -0.0641
2 bullseye 54.3 47.8 16.8 26.9 -0.0686
3 circle 54.3 47.8 16.8 26.9 -0.0683
4 dino 54.3 47.8 16.8 26.9 -0.0645
5 dots 54.3 47.8 16.8 26.9 -0.0603
6 h_lines 54.3 47.8 16.8 26.9 -0.0617
7 high_lines 54.3 47.8 16.8 26.9 -0.0685
8 slant_down 54.3 47.8 16.8 26.9 -0.0690
9 slant_up 54.3 47.8 16.8 26.9 -0.0686
10 star 54.3 47.8 16.8 26.9 -0.0630
11 v_lines 54.3 47.8 16.8 26.9 -0.0694
12 wide_lines 54.3 47.8 16.8 26.9 -0.0666
13 x_shape 54.3 47.8 16.8 26.9 -0.0656
02 条形图极坐标化geom_bar()+coord_polar()
用法:coord_polar()、学习ggplot2 饼图
coord_polar(theta="x",start=0,direction=1,clip="on")
theta:x 轴极化,x 轴刻度值对应扇形弧度,y 轴刻度值对应圆环半径;y 轴极化,y 轴刻度值对应扇形弧度,x 轴长度对应扇形半径。
start:起始位置为距离 12 点针方向的度数。
direction:1, 顺时针; -1, 逆时针。
clip:Should drawing be clipped to the extent of the plot panel?
#雷达图,theta默认是x
diamonds %>%
ggplot(aes(cut,fill=cut))+
geom_bar()+
coord_polar()
#甜甜圈图,theta="y"
diamonds %>%
ggplot(aes(cut,fill=cut))+
geom_bar()+
coord_polar(theta="y")
#把cyl(值有4、6和8)列各个值的数量做成柱状图
#柱状图改饼图,长度变成弧度
pie <- ggplot(mtcars,aes(x=factor(1),fill=factor(cyl)))+
geom_bar(width=1) + coord_polar(theta="y")
#柱状图改成环状图
ring <- ggplot(mtcars,aes(x=factor(1),fill=factor(cyl)))+
geom_bar(width=1) + coord_polar(theta="x")
#R可视化扩展-图像合并
p1 <- ggplot(data = datasaurus_dozen, mapping = aes(x =x, y=y, color = dataset)) +
geom_point() +
facet_wrap(~dataset) +
coord_polar() +
theme_bw()
#图像左右拼接
pie+p1
#图像上下拼接
pie/p1
#展示合适的数据量
#增加透明度 alpha
d5k <- sample_n(diamonds,5000)
#随机抽取 sample
d5k %>%
ggplot(aes(carat,price))+
geom_point(alpha=0.1)
03 动态图bubble
install_github("dgrtwo/gganimate",force = TRUE) #安装gganimate包
pacman::p_load(dplyr,ggplot2,dslabs,gganimate,gifski,av,png,tidyverse,devtools)
#对数据进行分组
data(gapminder)
west <- c("Western Europe","Northern Europe","Southern Europe",
"Northern America","Australia and New Zealand")
gapminder1 <- gapminder %>%
mutate(group = case_when(
.$region %in% west ~ "老达",
.$region %in% c("Eastern Asia","South-Eastern Asia") ~ "远东",
.$region %in% c("Caribbean","Central America","South America") ~ "拉美",
.$continent=="Africa" & .$region!="Northern Africa" ~ "非非",
TRUE ~ "其他"))
gapminder1 <- gapminder1 %>%
mutate(group = factor(group,levels = rev(c("其他","拉美","远东","非非","老达"))))
years <- seq(1962, 2013)
# 去掉空值,建立新的列,数值变小
gapminder2 <- filter(gapminder1,year %in% years & !is.na(group) &
!is.na(fertility) & !is.na(life_expectancy)) %>%
mutate(population_in_millions = population / 10^6)
# a %in% b ,判断b的值是否在a中; filter筛选;mutate 增加新的列
gapminder2 %>% view()
gapminder2$year
str(gapminder2)
p <- gapminder2 %>% ggplot(aes(fertility,y=life_expectancy,color=group,
size=population_in_millions) )+
geom_point(shape=16)+ #shape = 16 为实心圆点
guides(size=FALSE)+ #移除由ase中匹配的size生成的图例
theme(legend.title=element_blank() )+
coord_cartesian(ylim=c(30,85))+ #纵轴截距
#下面最后三行用到动态图
labs(title='{frame_time}年', x='生育率', y='人均寿命预期')+ #labs 修改轴、图例等标签, frame_time给出了当前帧对应的时间
transition_time(year)+ #根据年变化
ease_aes('linear') #ease_aes()定义了一个值在调整期间如何变化为另一个值
p
anim_save("C:/Users/apple/Desktop/ferta.gif")