欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Data exploration (1): Data Visualization with ggplot2

程序员文章站 2022-07-14 21:43:12
...

Goal

Data exploration (1): Data Visualization with ggplot2

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 大小
Data exploration (1): Data Visualization with ggplot2

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)
Data exploration (1): Data Visualization with ggplot2

For the combination of two variables, using facet_grid()

ggplot(data=mpg)+
  geom_point(mapping=aes(x=displ,y=hwy)) +
  facet_grid(drv ~ cyl)
Data exploration (1): Data Visualization with ggplot2

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()
Data exploration (1): Data Visualization with ggplot2

Smooth all the class points

ggplot(data=mpg,mapping=aes(x=displ,y=hwy))+
geom_point(mapping=aes(color=class))+
geom_smooth()
Data exploration (1): Data Visualization with ggplot2

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)
Data exploration (1): Data Visualization with ggplot2
# 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)
Data exploration (1): Data Visualization with ggplot2

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")
Data exploration (1): Data Visualization with ggplot2

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")
Data exploration (1): Data Visualization with ggplot2

# 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")
Data exploration (1): Data Visualization with ggplot2

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>