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主题系统

  • 11
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 如果你想在 ggplot2 图中显示图例,你可以使用 `guides()` 函数并设置 `override.aes` 参数。 下面是一个例子: ``` ggplot(data, aes(x = x, y = y, color = z)) + geom_point() + guides(color = guide_legend(override.aes = list(size = 4))) ``` 你也可以使用 `show.legend = TRUE` 参数来显示图例: ``` ggplot(data, aes(x = x, y = y, color = z)) + geom_point(show.legend = TRUE) ``` 如果你正在使用的是 ggplot2 的旧版本,你可以使用 `legend.title` 参数: ``` ggplot(data, aes(x = x, y = y, color = z)) + geom_point(legend.title = "legend title") ``` 希望这些建议能够帮助你解决问题! ### 回答2: 如果在使用ggplot2绘图时图例没有显示,可能有几个原因导致。 首先,要确保图例的设置正确。在ggplot2中,图例是通过添加一个“颜色”或“形状”参数来实现的。例如,你可以在aes()函数中的“颜色”参数中指定一个变量来创建一个基于颜色的图例。如果没有正确设置这些参数,图例将不会显示。在代码中确保有这样的设置。 其次,考虑图例是否被隐藏或覆盖。有时候,图例可能被隐式地设置为不可见或被其他元素覆盖。可以尝试使用theme()函数中的相应参数来调整图例的位置、方向和大小等属性。例如,使用theme(legend.position = "right")可以将图例放置在绘图区域的右侧。 另外,还可能是绘图的数据存在问题。如果数据中没有包含用于图例的变量或有缺失值,图例可能不会显示。确保数据中包含需要显示在图例中的变量,并且这些变量的值是完整的。 最后,如果以上方法都没有解决问题,可能是因为使用了较老版本的ggplot2包。尝试将ggplot2更新到最新版本,以获得最佳的兼容性和功能。 综上所述,如果ggplot2不显示图例,需要检查图例设置、位置和数据,并确保使用最新版本的ggplot2包。 ### 回答3: ggplot2不显示图例的原因可能有以下几个: 1. 没有设置图例的标签:在ggplot中,需要使用scale_系列函数来设置图例的标签。例如,使用scale_colour_manual()函数来设置颜色图例的标签,使用scale_linetype_manual()函数来设置线型图例的标签。如果没有设置图例的标签,就不会显示图例。 2. 图例位置被设置在不可见的位置:默认情况下,图例会显示在绘图区域的右上角。如果图例的位置被设置在不可见的位置,就不会显示图例。可以使用theme()函数的legend.position参数来设置图例的位置,例如legend.position = "top"将图例显示在绘图区域的顶部。 3. 图例被跳过或被隐藏:有时候,可以使用theme()函数的legend.title参数来控制图例的显示与隐藏。如果将legend.title设置为element_blank(),就会隐藏图例。另外,使用guides()函数的参数可以控制某个图层是否显示图例。 如果仍然无法显示图例,可能是由于其他设置或代码错误导致的。可以检查代码中的设置是否正确,或者提供更详细的代码和数据,以便更好地帮助解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值