ggplot2_Guides图例与增加坐标轴


作者:Li_Yuhui
四川大学研究生在读

本文是ggplot2_可视化_速成的第5节
第2节:coor_xxx()坐标系变换
第3节:scale_xxx()标度调整
第4节:facet_xxx()分面系统
第6节:themes主题系统


Guides图例与增加坐标轴

图例函数:

  • guide_colorbar()/guide_colourbar() 用于连续变量的图例
  • guide_legend() 用于离散变量的图例,也可以用于连续变量
  • guides() 将_colorbar和_legend嵌套进去,方便映射,如guides(fill = guide_colorbar())
    可以在scale_xxx()标度中指定guide类型,guide = "colorbar"或guide = “legend”
    常用公共参数:
作用对象 参数 描述
整个图例 direction 指定图例箱体排列方向,"horizontal"水平排列,或"vertical"垂直排列
reverse 逻辑值,是否翻转图例顺序,默认从小到大自上而下,翻转后从小到大自下而上
order 为数字,表示给图例编号,方便多个图例排列
图例标题 title 指定标题名称
title.position 标题相对图例箱体的位置, 水平图例为"left"或"right", 垂直图例为"top"或"bottom"
title.hjust 为数字,指定图例标题水平位置偏移
title.vjust 为数字,指定图例标题垂直位置偏移
图例刻度标签 label 为逻辑值,是否显示图例刻度标签
label.position 指定图例刻度标签相对箱体位置, 水平图例为"left"或"right", 垂直图例为"top"或"bottom"
label.hjust 为数字,指定图例刻度标签水平位置偏移
label.vjust 为数字,指定图例刻度标签垂直位置偏移
default.unit 表示指定箱体尺寸单位,用grid::unit()

guide_colorbar

**_colorbar()参数: **

作用对象 参数 描述
图例箱体 barwidth 指定箱体宽度,为数字或grid::unit()指定,默认单位为mm
barheight 指定箱体高度,为数字或grid::unit()指定
nbin 指定分箱数,数字越大则渐变约平缓
raster 逻辑值,表示是否将图例以删格形式呈现,不常用,栅格数据
箱体边框 frame.colour 表示指定箱体边框颜色,默认无边框
frame.linetype 表示指定箱体边框线型
frame.linewidth 表示指定箱体边框线宽
刻度线 ticks 逻辑值,表示是否显示刻度线
ticks.colour 指定刻度线颜色
ticks.linewidth 指定刻度线线宽
draw.ulim 逻辑值,表示是否显示最大值(upper)刻度线
draw.llim 逻辑值,表示是否显示最小值(low)刻度线
library(ggplot2)
library(reshape2)
df <- melt(outer(1:4, 1:4), varnames = c("X1", "X2"))

p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
p2 <- p1 + geom_point(aes(size = value))

p1 + scale_fill_continuous(guide = "colorbar") # 默认形式
p1 + guides(fill = guide_colorbar())  # 具体映射

p1 + scale_fill_distiller(palette = "YlGn", direction = 1) +  
  guides(fill =  guide_colorbar(title = "值", nbin = 100, # 指定图例名称,水平放置,增加分箱数为100
                                  barwidth = 0.5, barheight = 10,# 指定图例箱体尺寸,宽为0.5mm,高为10mm
                                  ticks.colour = "red", # 更改刻度线颜色
                                  frame.colour = "blue",frame.linewidth = 0.5, # 增加箱体边框
                                  draw.ulim = TRUE, draw.llim = TRUE # 显示最大,最小刻度线
                                )) 
p2 + scale_fill_continuous(guide = "colorbar") + scale_size(guide = "legend") # 在标度中控制图例
p2 + guides(fill = "colorbar", size = "legend") # 与上面结果一样

p2 + scale_fill_continuous(guide = guide_colorbar(direction = "horizontal")) +
  scale_size(guide = guide_legend(direction = "vertical")) # 更改图例方向
  

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

guide_legend

_legend()参数:

作用对象 参数 描述
箱体尺寸 key.width 指定单个箱体宽度, 为数字或grid::unit()指定, 默认单位为mm
key.height 指定单个箱体高度, 为数字或grid::unit()指定
分箱排列 nrow 为数字,表示指定图例箱体排列行数
ncol 为数字,表示指定图例箱体排列列数
byrow 逻辑值,表示图例箱体是否按行排列,默认FALSE按列排
library(ggplot2)
library(reshape2)
df <- melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
p2 <- p1 + geom_point(aes(size = value))

p1 + scale_fill_continuous(guide = guide_legend()) # 连续标度中设置离散图例
p1 + scale_fill_distiller(type = "qual", palette = "Set3") + 
  guides(fill = guide_legend(title = "左", title.position = "left", # 指定图例名称为"左", 位置为箱体的左边
                             key.width = 5, key.height = 10, nrow = 2, ncol = 2, byrow = TRUE # 修改箱体尺寸,并矩形排列,按行排
                             )) 

p1 + guides(fill = guide_legend( 
  title.theme = element_text(size = 15, face = "italic", colour = "red", angle = 0)) # 在图例中修改图例主题,一般在主题函数内修改
  ) 

p1 + scale_fill_continuous(breaks = c(5, 10, 15),
  labels = paste("long", c(5, 10, 15)),
  guide = guide_legend(
    direction = "horizontal", # 水平排列箱体
    title.position = "top",  # 图例标题置于顶部
    label.position = "bottom", # 图例刻度标签置于底部
    label.hjust = 0.5, # 刻度标签水平位置偏移
    label.vjust = 1, # 刻度标签垂直位置偏移
    label.theme = element_text(angle = 90) # 图例主题中修改刻度标签角度 
  )
)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

guides多个图例

guides多个图例:
guides()内部嵌套guide_legend()guide_colorbar(),添加一个映射参数,如:

 guides(
   colour = guide_colourbar(order = 1),
   shape = guide_legend(order = 2),
   size = guide_legend(order = 3)
 )
library(ggplot2)
dat <- data.frame(x = 1:5, y = 1:5, p = 1:5, q = factor(1:5), r = factor(1:5))
p <- ggplot(dat, aes(x, y, colour = p, size = q, shape = r)) + geom_point()
p

p + guides(colour = guide_colorbar(), size = guide_legend(), shape = guide_legend()) # 默认按参数顺序排列多个图例
p + scale_colour_continuous(guide = "colorbar") +
  scale_size_discrete(guide = "legend") +
  scale_shape(guide = "legend") + 
  guides(colour = "none") # 删除一个图例

# 设定多个图例
ggplot(mpg, aes(displ, cty)) +
  geom_point(aes(size = hwy, colour = cyl, shape = drv)) +
  guides(
   colour = guide_colourbar(order = 1), # order指定图例排列顺序
   shape = guide_legend(order = 2),
   size = guide_legend(order = 3)
 ) 

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

多图例合并

library(ggplot2)
# 图例合并:
## 多个不同标度图例合并:
### 当图例类型一致,图例标题一致时,会自动合并图例
dat <- data.frame(x = 1:5, y = 1:5, p = 1:5, q = factor(1:5), r = factor(1:5))
p <- ggplot(dat, aes(x, y, colour = p, size = q, shape = r)) + geom_point()
p + guides(colour = guide_legend("这是图例标题"), size = guide_legend("这是图例标题"), shape = guide_legend("这是图例标题")) +
  theme(legend.position = "bottom") # 主题函数中调节图例位置

## 多种几何对象图例合并:
### 若都是同一个变量映射的,且标度类型一致,标度标题相同,标度values等长,给标度新增labels参数,labels相同,则会自动合并图例
state1 <- c(rep(c(rep("N", 7), rep("Y", 7)), 2))
year <- rep(c(2003:2009), 4)
group1 <- c(rep("C", 14), rep("E", 14))
group2 <- paste(state1, group1, sep = "")
beta <- c(0.16,0.15,0.08,0.08,0.18,0.48,0.14,0.19,0.00,0.00,0.04,0.08,0.27,0.03,0.11,0.12,0.09,0.09,0.10,0.19,0.16,0.00,0.11,0.07,0.08,0.09,0.19,0.10)
    lcl <- c(0.13,0.12,0.05,0.05,0.12,0.35,0.06,0.13,0.00,0.00,0.01,0.04,0.20,0.00,0.09,0.09,0.06,0.06,0.07,0.15,0.11,0.00,0.07,0.03,0.05,0.06,0.15,0.06)
    ucl <- c(0.20,0.20,0.13,0.14,0.27,0.61,0.28,0.27,0.00,1.00,0.16,0.16,0.36,0.82,0.14,0.15,0.13,0.13,0.15,0.23,0.21,0.00,0.15,0.14,0.12,0.12,0.23,0.16)
data <- data.frame(state1,year,group1,group2,beta,lcl,ucl)

ggplot(data = data,aes(x= year, y = beta, colour = group2, shape = group2)) +    
  geom_point(size = 4) +
  geom_errorbar(aes(ymin = lcl, ymax = ucl), colour = "black", width = 0.5) +
  scale_colour_manual(name = "Treatment & State",
                      labels = c("Control, Non-F", "Control, Flwr", "Exclosure, Non-F", "Exclosure, Flwr"),
                      values = c("blue", "red", "blue", "red")) +   
  scale_shape_manual(name = "Treatment & State",
                     labels = c("Control, Non-F", "Control, Flwr", "Exclosure, Non-F", "Exclosure, Flwr"),
                     values = c(19, 19, 17, 17))

### 映射变量相同,在标度labs函数中设置相同的标度名称
ggplot(iris) + 
  aes(x = Sepal.Length, y = Sepal.Width, 
      color = Species, linetype = Species, shape = Species) +
  geom_line() +
  geom_point() +
  labs(color  = "Guide name", linetype = "Guide name", shape = "Guide name")

### 下一个例子
x <- seq(0, 10, by = 0.2)
y1 <- sin(x)
y2 <- cos(x)
y3 <- cos(x + pi / 4)
y4 <- sin(x + pi / 4)
df1 <- data.frame(x, y = y1, Type = as.factor("sin"), Method = as.factor("method1"))
df2 <- data.frame(x, y = y2, Type = as.factor("cos"), Method = as.factor("method1"))
df3 <- data.frame(x, y = y3, Type = as.factor("cos"), Method = as.factor("method2"))
df4 <- data.frame(x, y = y4, Type = as.factor("sin"), Method = as.factor("method2"))

df.merged <- rbind(df1, df2, df3, df4)

y5 <- sin(x - pi / 4)
df5 <- data.frame(x, y = y5, Type = as.factor("sin"), Method = as.factor("method3"))
df.merged <- rbind(df1, df2, df3, df4, df5)

df.merged$int <- paste(df.merged$Type, df.merged$Method, sep=".") # 给数据源新增一列变量

ggplot(df.merged, aes(x, y, colour = int, linetype=int, shape=int)) +
  geom_line() +
  geom_point() +
  scale_colour_discrete("") +
  scale_linetype_manual("", values=c(1,2,1,2,3)) +
  scale_shape_manual("", values=c(17,17,16,16,15))

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

新增坐标轴

所有的新增坐标轴都是基于现有坐标轴变换而来的

  • sec_axis(trans = NULL, name = waiver(), breaks = waiver(), labels = waiver())
  • dup_axis(trans = ~., name = derive(), breaks = derive(), labels = derive())
  • derive()

参数解释:

  • trans 表示指定变换公式
  • name 表示指定新增坐标轴的名称
  • breaks 表示指定新增坐标轴刻度点位置
  • labels 表示指定新增坐标轴刻度标签
  • derive 表示继承现有坐标轴,基本上就是复制,没有变换关系,如果有变换关系,还是用sec_axis()
library(ggplot2)
p <- ggplot(mtcars, aes(cyl, mpg)) +
  geom_point()
p + scale_y_continuous(sec.axis = sec_axis(~.+10)) # 在标度函数中新增第2个y轴,变换关系为:原y轴 + 10
p + scale_y_continuous("英里/每加仑", sec.axis = sec_axis(~.+10, name = "公里/L")) # 新增y轴,轴名称为:公里每升,原y轴为:英里/加仑
p + scale_y_continuous(sec.axis = ~.^2) # 变换关系为:平方
p + scale_y_continuous(sec.axis = ~.^2 * 3 + 4*.)# 变换关系为:3*y^2 + 4*y

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

themes主题系统

猜你喜欢

转载自blog.csdn.net/weixin_43528109/article/details/83859566
今日推荐