Data exploration (1): Data Visualization with ggplot2
程序员文章站
2022-07-14 21:43:12
...
Goal
A Graphing Template
ggplot(data=<DATA>)+
<GEOM_FUNCTION>(mapping=aes(<MAPPINGGS>))
Aesthetic Mappings
An aesthetic is a visual property of the objects in your plot,
including the size, the shape, or the color of points.
For example, map the colors of points to the class variable
library(tidyverse)
ggplot(data=mpg)+
geom_point(mapping=aes(x=displ,y=hwy,color=class)) # alpha=class 透明度, shape=class 形状, size=class 大小
Facets
> Two ways to add additional variables:
1. with aesthetics
2. split plot into facets (particularly for categorical variables)
For example,
ggplot(data=mpg)+
geom_point(mapping=aes(x=displ,y=hwy)) +
facet_wrap(~ class, nrow=2)
For the combination of two variables, using facet_grid()
ggplot(data=mpg)+
geom_point(mapping=aes(x=displ,y=hwy)) +
facet_grid(drv ~ cyl)
Geometric Objects
A geom is the geometrical object that a plot use to represent data. Such as bar geoms, point geoms, box geoms
For example
ggplot(data=mpg,mapping=aes(x=displ,y=hwy,color=drv))+
geom_point()+
geom_smooth()
Smooth all the class points
ggplot(data=mpg,mapping=aes(x=displ,y=hwy))+
geom_point(mapping=aes(color=class))+
geom_smooth()
Smooth a subst of the class points
ggplot(data=mpg,mapping=aes(x=displ,y=hwy))+
geom_point(mapping=aes(color=class))+
geom_smooth(data=filter(mpg,class=="subcompact"), se=FALSE)
# 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))+
geom_point()+
geom_smooth(mapping=aes(group=drv),se=FALSE)
# 3图
ggplot(data=mpg,mapping=aes(x=displ,y=hwy,color=drv))+
geom_point()+
geom_smooth(mapping=aes(group=drv),se=FALSE)
# 4图
ggplot(data=mpg,mapping=aes(x=displ,y=hwy))+
geom_point(mapping=aes(color=drv))+
geom_smooth(se=FALSE)
# 5图
ggplot(data=mpg,mapping=aes(x=displ,y=hwy,color=drv))+
geom_point()+
geom_smooth(mapping=aes(linetype=drv),se=FALSE)
# 6图 做反了????
ggplot(data=mpg,mapping=aes(x=displ,y=hwy))+
geom_point(aes(color=drv),size=4)+
geom_point(color="white",size=0.6)
Statistical Transformations
Statistical transformation is also called stat: the algorithm used to calculate new values for a graph
For example, geom_bar and stat_count are interchangeable.
ggplot(data=diamonds,aes(x=cut))+
stat_count()
#指定统计变换
demo <- tribble(~a,~b,"bar_1",20,"bar_2",30,"bar_3",40)
ggplot(data=demo,aes(a,b))+
geom_bar(sta="identity")
Position Adjustments
# 面积颜色相同,边框颜色表示不同分类
ggplot(diamonds,aes(x=cut,color=clarity))+
geom_bar(alpha=1/5,position="identity)
# 边框颜色固定,面积颜色表示不同分类
ggplot(diamonds,aes(x=cut,fill=clarity))+
geom_bar(alpha=1/5,position="identity",color="black")
# fill
ggplot(diamonds,aes(x=cut,fill=clarity))+
geom_bar(alpha=0.5,,position="fill")
#identity
ggplot(diamonds,aes(x=cut,fill=clarity))+
geom_bar(alpha=0.5,,position="identity")
#dodge
ggplot(diamonds,aes(x=cut,fill=clarity))+
geom_bar(alpha=0.5,,position="dodge")
Coordinate Systems
coord_flip坐标翻转,coord_quickmap等比例放缩,coord_polar极坐标转换
The Layered Grammar of Graphics
Code template:
ggplot(data=<DATA>)+
<GEOM_FUNCTION>(mapping=aes(<MAPPINGGS>),stat=<STAT>,position=<POSITION>) +
<COORDINATE FUNCTION> +
<FACET_FUNCTION>
推荐阅读
-
load data infile将excel文件中的数百万条数据在1分钟内导入数据_MySQL
-
Oracle X$ tables – Part 1 – Where do they get their data f
-
Data Exploration using Pandas pandas库 数据读取&清洗
-
Data exploration (1): Data Visualization with ggplot2
-
Python for Data Analysis v2 | Notes_ Chapter 1-2
-
软件构造 3-1 Data Type and Type Checking
-
100 Days Of ML Code:Day1-Data Preprocessing(数据预处理)
-
Kevin Xu-TensorFlow Tutorials-cifar10 input_data(1)
-
对itemrenderer的一些认知(1)-理解data driven
-
vue 后台status返回200但data中返回-1在then中判断是否为错误返回