其他资源:
scale类函数的命名方式
ggplot中scale名字、美学属性和函数列表
limits
需要注意:设置limits之后,limits之外的数据点都会强制为NA,不会在plot中展示。在之后进行的stat运算都不会包含这些超出的数据点。绘图过程中也会有警告信息,请务必关注。
默认limits通过计算data variables的range得到,包括position scales(如x和y aesthetics)和non-position scales(如color aesthetic)。
应用1:来自同一底层data或不同底层data的多个plot进行比较时,需要设置相同的limits,实现方法有如下几种:
# 来自同一底层data,用分面实现
ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_wrap(vars(year))
# 来自不同底层data,用scale_x/y_continous()实现
consistent_limits <- function(p){
p <- p + scale_x_continuous(limits = c(1, 7)) + scale_y_continuous(limits = c(10, 45))
}
mpg_99 <- mpg %>% filter(year == 1999)
mpg_08 <- mpg %>% filter(year == 2008)
base_99 <- ggplot(mpg_99, aes(displ, hwy)) + geom_point()
base_99_final <- consistent_limits(base_99)
base_08 <- ggplot(mpg_08, aes(displ, hwy)) + geom_point()
base_08_final <- consistent_limits(base_08)
# 来自不同底层data,用lims()实现,lims()的好处是写法简单还可以同时设置多个aesthetics
base_99_final <- base_99 + lims(x = c(1, 7), y = c(10, 45))
base_08_final <- base_08 + lims(x = c(1, 7), y = c(10, 45))
# 对离散型变量,用scale_x/y_discrete()实现
diamonds_cut <- ggplot(diamonds, aes(cut)) + geom_bar()
diamonds_cut + scale_x_discrete(limits=c("Fair","Good","Very Good"))
# 对离散型变量,用lims()实现
diamonds_cut + lims(x=c("Fair","Good","Very Good"))
应用2:来自同一底层data或不同底层data的多个plot需要保持图例一致,实现方法如下:
# 来自同一底层data,用分面实现,分面总是能保持plots在各个方面的一致性
ggplot(mpg, aes(displ, hwy, color=fl)) + geom_point() + facet_wrap(vars(year))
# 来自不同底层data,用scale_color_discrete()实现
base_99 <- ggplot(mpg_99, aes(displ, hwy, color=fl)) + geom_point()
base_08 <- ggplot(mpg_08, aes(displ, hwy, color=fl)) + geom_point()
base_99_legend <- base_99 + scale_color_discrete(limits=c("c", "d", "e", "p", "r"))
base_08_legend <- base_08 + scale_color_discrete(limits=c("c", "d", "e", "p", "r"))
# 来自不同底层data,用lims()实现
base_99_legend <- base_99 + lims(color=c("c", "d", "e", "p", "r"))
base_08_legend <- base_08 + lims(color=c("c", "d", "e", "p", "r"))
应用3: 来自不同底层data,需要保持color的一致性,又想在legend中只展示各自底层data中有的分类。
# 使用scale_color_discrete()的breaks参数实现,同时labels参数还可以改变legend的标签
base_99 + scale_color_discrete(limits=c("c", "d", "e", "p", "r"),breaks=c("d","p","r"),labels=c("diesel","premium","regular"))
base_08 + scale_color_discrete(limits=c("c", "d", "e", "p", "r"),labels=c("compressed","diesel","ethanol","premium", "regular"))