# ggplot2的一些有用的绘图案例

r 专栏收录该内容
14 篇文章 0 订阅

• 在不歪曲事实的情况下传达正确的信息
• 简单而优雅的表达信息内容
• 通过美学表达信息，而不是掩盖信息
• 没有信息负载

## 类型一：相关性

### 散点图

options(scipen=999)
library(ggplot2)
theme_set(theme_bw())
data("midwest", package = "ggplot2")


# Scatterplot

gg <- ggplot(midwest, aes(x=area, y=poptotal)) +
geom_point(aes(col=state, size=popdensity)) +
geom_smooth(method=“loess”, se=F) +
xlim(c(0, 0.1)) +
ylim(c(0, 500000)) +
labs(subtitle=“Area Vs Population”,
y=“Population”,
x=“Area”,
title=“Scatterplot”,
caption = “Source: midwest”)

plot(gg)

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19

### 带有环绕的散点图

# install 'ggalt' pkg
# devtools::install_github("hrbrmstr/ggalt")
options(scipen = 999)
library(ggplot2)
library(ggalt)
midwest_select <- midwest[midwest$poptotal > 350000 & midwest$poptotal <= 500000 &
midwest$area > 0.01 & midwest$area < 0.1, ]


# Plot

ggplot(midwest, aes(x=area, y=poptotal)) +
geom_point(aes(col=state, size=popdensity)) + # draw points
geom_smooth(method=“loess”, se=F) +
xlim(c(0, 0.1)) +
ylim(c(0, 500000)) + # draw smoothing line
geom_encircle(aes(x=area, y=poptotal),
data=midwest_select,
color=“red”,
size=2,
expand=0.08) + # encircle
labs(subtitle=“Area Vs Population”,
y=“Population”,
x=“Area”,
title=“Scatterplot + Encircle”,
caption=“Source: midwest”)

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22
• 23
• 24
• 25
• 26
• 27

### 气泡图

1.基于分类变量，修改散点颜色
2.基于另一个连续变量，表示散点的大小

# load package and data
library(ggplot2)
data(mpg, package="ggplot2")


# Diverging Barcharts

ggplot(mtcars, aes(x=car name, y=mpg_z, label=mpg_z)) +
geom_bar(stat=‘identity’, aes(fill=mpg_type), width=.5) +
scale_fill_manual(name=“Mileage”,
labels = c(“Above Average”, “Below Average”),
values = c(“above”="#00ba38", “below”="#f8766d")) +
labs(subtitle=“Normalised mileage from ‘mtcars’”,
title= “Diverging Bars”) +
coord_flip()

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20

### 棒棒糖图

library(ggplot2)
theme_set(theme_bw())


ggplot(mtcars, aes(x=car name, y=mpg_z, label=mpg_z)) +
geom_point(stat=‘identity’, fill=“black”, size=6) +
geom_segment(aes(y = 0,
x = car name,
yend = mpg_z,
xend = car name),
color = “black”) +
geom_text(color=“white”, size=2) +
labs(title=“Diverging Lollipop Chart”,
subtitle=“Normalized mileage from ‘mtcars’: Lollipop”) +
ylim(-2.5, 2.5) +
coord_flip()

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15

### 点图

library(ggplot2)
theme_set(theme_bw())


# Plot

ggplot(mtcars, aes(x=car name, y=mpg_z, label=mpg_z)) +
geom_point(stat=‘identity’, aes(col=mpg_type), size=6) +
scale_color_manual(name=“Mileage”,
labels = c(“Above Average”, “Below Average”),
values = c(“above”="#00ba38", “below”="#f8766d")) +
geom_text(color=“white”, size=2) +
labs(title=“Diverging Dot Plot”,
subtitle=“Normalized mileage from ‘mtcars’: Dotplot”) +
ylim(-2.5, 2.5) +
coord_flip()

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14

## 类型三: 排序

### 有序条形图

cty_mpg <- aggregate(mpg$cty, by=list(mpg$manufacturer), FUN=mean)
colnames(cty_mpg) <- c("make", "mileage")
cty_mpg <- cty_mpg[order(cty_mpg$mileage), ] cty_mpg$make <- factor(cty_mpg$make, levels = cty_mpg$make)
library(ggplot2)
theme_set(theme_bw())


# Draw plot

ggplot(cty_mpg, aes(x=make, y=mileage)) +
geom_bar(stat=“identity”, width=.5, fill=“tomato3”) +
labs(title=“Ordered Bar Chart”,
subtitle=“Make Vs Avg. Mileage”,
caption=“source: mpg”) +
theme(axis.text.x = element_text(angle=65, vjust=0.6))

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14

### 棒棒糖图

library(ggplot2)
theme_set(theme_bw())


# Plot

ggplot(cty_mpg, aes(x=make, y=mileage)) +
geom_point(size=3) +
geom_segment(aes(x=make,
xend=make,
y=0,
yend=mileage)) +
labs(title=“Lollipop Chart”,
subtitle=“Make Vs Avg. Mileage”,
caption=“source: mpg”) +
theme(axis.text.x = element_text(angle=65, vjust=0.6))

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14

### 点图

library(ggplot2)
library(scales)
theme_set(theme_classic())


# Plot

ggplot(cty_mpg, aes(x=make, y=mileage)) +
geom_point(col=“tomato2”, size=3) + # Draw points
geom_segment(aes(x=make,
xend=make,
y=min(mileage),
yend=max(mileage)),
linetype=“dashed”,
size=0.1) + # Draw dashed lines
labs(title=“Dot Plot”,
subtitle=“Make Vs Avg. Mileage”,
caption=“source: mpg”) +
coord_flip()

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17

## 类型四: 分布

### 直方图

library(ggplot2)
theme_set(theme_classic())


# Histogram on a Continuous (Numeric) Variable

g <- ggplot(mpg, aes(displ)) + scale_fill_brewer(palette = “Spectral”)

g + geom_histogram(aes(fill=class),
binwidth = .1,
col=“black”,
size=.1) + # change binwidth
labs(title=“Histogram with Auto Binning”,
subtitle=“Engine Displacement across Vehicle Classes”)

g + geom_histogram(aes(fill=class),
bins=5,
col=“black”,
size=.1) + # change number of bins
labs(title=“Histogram with Fixed Bins”,
subtitle=“Engine Displacement across Vehicle Classes”)

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19

### 分类变量直方图

library(ggplot2)
theme_set(theme_classic())


# Histogram on a Categorical variable

g <- ggplot(mpg, aes(manufacturer))
g + geom_bar(aes(fill=class), width = 0.5) +
theme(axis.text.x = element_text(angle=65, vjust=0.6)) +
labs(title=“Histogram on Categorical Variable”,
subtitle=“Manufacturer across Vehicle Classes”)

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9

###密度函数图

library(ggplot2)
theme_set(theme_classic())


# Plot

g <- ggplot(mpg, aes(cty))
g + geom_density(aes(fill=factor(cyl)), alpha=0.8) +
labs(title=“Density plot”,
subtitle=“City Mileage Grouped by Number of cylinders”,
caption=“Source: mpg”,
x=“City Mileage”,
fill="# Cylinders")

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11

###箱型图

library(ggplot2)
theme_set(theme_classic())


# Plot

g <- ggplot(mpg, aes(class, cty))
g + geom_boxplot(varwidth=T, fill=“plum”) +
labs(title=“Box plot”,
subtitle=“City Mileage grouped by Class of vehicle”,
caption=“Source: mpg”,
x=“Class of Vehicle”,
y=“City Mileage”)

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11

library(ggthemes)
g <- ggplot(mpg, aes(class, cty))
g + geom_boxplot(aes(fill=factor(cyl))) +
theme(axis.text.x = element_text(angle=65, vjust=0.6)) +
labs(title="Box plot",
subtitle="City Mileage grouped by Class of vehicle",
caption="Source: mpg",
x="Class of Vehicle",
y="City Mileage")


123456789

### 带点的箱型图

library(ggplot2)
theme_set(theme_bw())


# plot

g <- ggplot(mpg, aes(manufacturer, cty))
g + geom_boxplot() +
geom_dotplot(binaxis=‘y’,
stackdir=‘center’,
dotsize = .5,
fill=“red”) +
theme(axis.text.x = element_text(angle=65, vjust=0.6)) +
labs(title=“Box plot + Dot plot”,
subtitle=“City Mileage vs Class: Each dot represents 1 row in source data”,
caption=“Source: mpg”,
x=“Class of Vehicle”,
y=“City Mileage”)

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16

### Tufte 箱型图

library(ggthemes)
library(ggplot2)
theme_set(theme_tufte())  # from ggthemes


# plot

g <- ggplot(mpg, aes(manufacturer, cty))
g + geom_tufteboxplot() +
theme(axis.text.x = element_text(angle=65, vjust=0.6)) +
labs(title=“Tufte Styled Boxplot”,
subtitle=“City Mileage grouped by Class of vehicle”,
caption=“Source: mpg”,
x=“Class of Vehicle”,
y=“City Mileage”)

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13

### 小提琴图

library(ggplot2)
theme_set(theme_bw())


# plot

g <- ggplot(mpg, aes(class, cty))
g + geom_violin() +
labs(title=“Violin plot”,
subtitle=“City Mileage vs Class of vehicle”,
caption=“Source: mpg”,
x=“Class of Vehicle”,
y=“City Mileage”)

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11

## 类型五: 组成

### 饼图

library(ggplot2)
theme_set(theme_classic())


df <- as.data.frame(table(mpg$class)) colnames(df) <- c(“class”, “freq”) pie <- ggplot(df, aes(x = “”, y=freq, fill = factor(class))) + geom_bar(width = 1, stat = “identity”) + theme(axis.line = element_blank(), plot.title = element_text(hjust=0.5)) + labs(fill=“class”, x=NULL, y=NULL, title=“Pie Chart of class”, caption=“Source: mpg”) pie + coord_polar(theta = “y”, start=0) # Source: Categorical variable. # mpg$class

pie <- ggplot(mpg, aes(x = “”, fill = factor(class))) +
geom_bar(width = 1) +
theme(axis.line = element_blank(),
plot.title = element_text(hjust=0.5)) +
labs(fill=“class”,
x=NULL,
y=NULL,
title=“Pie Chart of class”,
caption=“Source: mpg”)

pie + coord_polar(theta = “y”, start=0)

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22
• 23
• 24
• 25
• 26
• 27
• 28
• 29
• 30
• 31

# plot

ggplot(economics_m, aes(x=date)) +
geom_line(aes(y=uempmed)) +
labs(title=“Monthly Time Series”,
subtitle=“Returns Percentage from Economics Dataset”,
caption=“Source: Economics”,
y=“Returns %”) + # title and caption
scale_x_date(labels = lbls,
breaks = brks) +
theme(axis.text.x = element_text(angle = 90, vjust=0.5),
panel.grid.minor = element_blank())

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22

### 关于年度数据的时间序列图

library(ggplot2)
library(lubridate)
theme_set(theme_bw())


economics_y <- economics[1:90, ]

# labels and breaks for X axis text

brks <- economics_y d a t e [ s e q ( 1 , l e n g t h ( e c o n o m i c s y date[seq(1, length(economics_y date), 12)]
lbls <- lubridate::year(brks)

# plot

ggplot(economics_y, aes(x=date)) +
geom_line(aes(y=uempmed)) +
labs(title=“Yearly Time Series”,
subtitle=“uempmed Percentage from Economics Dataset”,
caption=“Source: Economics”,
y=“uempmed %”) + # title and caption
scale_x_date(labels = lbls,
breaks = brks) +
theme(axis.text.x = element_text(angle = 90, vjust=0.5), panel.grid.minor = element_blank())

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20

### 同时展示多个时间序列

data(economics_long, package = "ggplot2")
library(ggplot2)
library(lubridate)
theme_set(theme_bw())


df <- economics_long[economics_longKaTeX parse error: Expected 'EOF', got '&' at position 47: …empmed"), ] df &̲lt;- df[lubrida…date) %in% c(1967:1981), ]

# labels and breaks for X axis text

brks <- df d a t e [ s e q ( 1 , l e n g t h ( d f date[seq(1, length(df date), 12)]
lbls <- lubridate::year(brks)

# plot

ggplot(df, aes(x=date)) +
geom_line(aes(y=value, col=variable)) +
labs(title=“Time Series of Returns Percentage”,
subtitle=“Drawn from Long Data format”,
caption=“Source: Economics”,
y=“Returns %”,
color=NULL) + # title and caption
scale_x_date(labels = lbls, breaks = brks) +
scale_color_manual(labels = c(“psavert”, “uempmed”),
values = c(“psavert”="#00ba38",
“uempmed”="#f8766d")) +
theme(axis.text.x = element_text(angle = 90, vjust=0.5, size = 8),
panel.grid.minor = element_blank())

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22
• 23
• 24
• 25
• 26
• 27
• 28

### 堆积图

library(ggplot2)
library(lubridate)
theme_set(theme_bw())


df <- economics[, c(“date”, “psavert”, “uempmed”)]
df <- df[lubridate::year(df\$date) %in% c(1967:1981), ]

# labels and breaks for X axis text

brks <- df d a t e [ s e q ( 1 , l e n g t h ( d f date[seq(1, length(df date), 12)]
lbls <- lubridate::year(brks)

# plot

ggplot(df, aes(x=date)) +
geom_area(aes(y=psavert+uempmed, fill=“psavert”)) +
geom_area(aes(y=uempmed, fill=“uempmed”)) +
labs(title=“Area Chart of Returns Percentage”,
subtitle=“From Wide Data format”,
caption=“Source: Economics”,
y=“Returns %”) +
scale_x_date(labels = lbls, breaks = brks) +
scale_fill_manual(name="",
values = c(“psavert”="#00ba38", “uempmed”="#f8766d")) +
theme(panel.grid.minor = element_blank())

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22
• 23

### 季节性时间序列

library(ggplot2)
library(forecast)
theme_set(theme_classic())


# Subset data

nottem_small <- window(nottem, start=c(1920, 1), end=c(1925, 12)) # subset a smaller timewindow

# Plot

ggseasonplot(AirPassengers) + labs(title=“Seasonal plot: International Airline Passengers”)
ggseasonplot(nottem_small) + labs(title=“Seasonal plot: Air temperatures at Nottingham Castle”)

• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10

## 类型八: 空间可视化

### 谷歌卫星地图

                                </div>

• 1
点赞
• 0
评论
• 3
收藏
• 一键三连
• 扫一扫，分享海报

09-06
12-07 971

07-22 9688
09-17 1510
10-13 2947
06-05 4万+
02-16 1717
07-04 4万+
07-08 5034
10-25 2888
01-10 2838
06-02 1077
12-15 428
04-11 315
02-10 106