【R数据可视化手册-散点图】
data & package
rm(list = ls())
library(ggplot2)
library(gcookbook) # 为了使用数据
library(MASS) # 为了使用数据
library(hexbin) # 为了使用函数
数据:heigheweight、diamonds、ChickWeight、countries、faithful
基本语法
ggplot(data, aes(x, y)) + geom_point()
ggplot(heightweight, aes(x = ageYear, y = heightIn)) +
geom_point()
修改参数
点型、大小、颜色
geom_point()默认的点型shape = 16, size = 2, fill = “black”。对于分组变量,可以直接映射给shape/colour等参数
# 将分组变量映射给colour、shape
ggplot(heightweight, aes(x = ageYear, y = heightIn, colour = sex, shape = sex)) +
geom_point()
自定义的调色板、点型
可以进一步调用RColorBrewer的调色板,或者使用自定义的点型。R绘图系统中,编号1-20的颜色通过colour设置,编号21-25的点型可以同时调节colour(边线颜色)、fill(填充色)
将分组变量映射给shape后,可调用scale_shape_manual()修改点型
ggplot(heightweight, aes(x = ageYear, y = heightIn, shape = sex)) +
geom_point(size = 3) +
scale_shape_manual(values = c(1, 4))
# 将一组分类变量同时映射给点型、颜色
ggplot(heightweight, aes(x = ageYear, y = heightIn, colour = sex, shape = sex)) +
geom_point() +
scale_shape_manual(values = c(1, 2)) + # 使用自定义点型
scale_colour_brewer(palette = "Set1") # 调用调色板
也可以使用空心/实心表示不同变量
hw <- heightweight
hw$weightGroup <- cut(hw$weightLb, breaks = c(-Inf, 100, Inf), labels = c("<100", ">100"))
# 添加自定义分组
ggplot(hw, aes(x = ageYear, y = heightIn, shape = sex, fill = weightGroup)) +
geom_point(size = 2.5) +
scale_shape_manual(values = c(21, 24)) +
scale_fill_manual(values = c(NA, "black"), guide = guide_legend(override.aes = list(shape = 21)))
映射连续变量
可以将连续变量映射给size或colour,但由于人感受颜色、大小变化的精度较低,绘制的图形常具有误导性;可修改点的大小和颜色使图形更具可读性
# 使用自定义的连续色阶表示变量值
ggplot(heightweight, aes(x = ageYear, y = heightIn, fill = weightLb)) +
geom_point(shape = 21, size = 2.5) +
scale_fill_gradient(low = "black", high = "white", # 设置区分度大的连续色阶
breaks = seq(70, 170, by =