![]() |
没有腹肌的沙滩裤 · openvpn实现两地内网互访_学无止境的技 ...· 6 月前 · |
![]() |
咆哮的葫芦 · winapi - What is the ...· 1 年前 · |
![]() |
豪气的馒头 · python 运行powershell命令-掘金· 1 年前 · |
感觉ggplot 绘图中的图例/legend,完全可以作为一个单独的内容讲很久,特此来总结一下。
使用
legend.position = "none"
可以方便我们移除图例,但有时候可能并不需要这么无情,比如移除指定某个类型的图例,通常几何对象可以设置多种分类(color, fill, shape..),我们可以使用
guide
函数:
ggplot(chic,
aes(x = date, y = temp,
color = season, shape = season)) +
geom_point() +
labs(x = "Year", y = "Temperature (°F)") +
guides(color = "none")
另外
scale_xx_discrete
函数亦可指定
guide
参数,比如
scale_shape_discrete(guide = "none")
。
theme(legend.title = element_blank())
,我们也可以在
labs
中,按照aes 定义的对应内容,直接创建空白的名称:
ggplot(chic, aes(x = date, y = temp, color = season)) +
geom_point() +
labs(x = "Year", y = "Temperature (°F)",
color = "")
我们亦可以使用
scale_xx_discrete
定义,如
scale_color_discrete(name = "Seasons\nindicated\nby colors:")
。
改变图例标题的方法有很多,关于子标签,可以使用
scale_xx_discrete
定义
labels
:
ggplot(chic, aes(x = date, y = temp, color = season)) +
geom_point() +
labs(x = "Year", y = "Temperature (°F)") +
scale_color_discrete(
name = "Seasons:",
labels = c("Mar—May", "Jun—Aug", "Sep—Nov", "Dec—Feb")
theme(legend.title = element_text(
family = "Playfair", color = "chocolate", size = 14, face = 2
image.png
除此之外,我们还可以利用函数,更加方便的对legend 内容进行操作,其实这个我也在[[86-R可视化18-自定义分类或连续数据坐标轴文本]]提过:
p <- ggplot(data = cell_reduction_df) +
geom_point(aes(x = UMAP_1, y = UMAP_2, color = cell_anno),
size = 0.5)
p + scale_color_discrete(
name = "Seasons:",
labels = function(x) {paste0("test", x)}
包括四种:“top”, “right” (which is the default), “bottom”, and “left”:
ggplot(chic, aes(x = date, y = temp, color = season)) +
geom_point() +
labs(x = "Year", y = "Temperature (°F)") +
theme(legend.position = "top")
之前的是让图例在外围到处溜达,现在让图例进入主图中。
通过调整图例位置
legend.position
在0-1 之间,可以将其内嵌:
ggplot(chic,
aes(x = date, y = temp,
color = season, shape = season)) +
geom_point() +
labs(x = "Year", y = "Temperature (°F)") + theme_classic() +
theme(legend.position = c(.15, .15),
legend.background = element_rect(fill = "transparent"))
element_rect 用来设置非数据类型内容的绘图选项,可以指定图例背景为透明,好看一些:
默认下,图例显示是竖直的(自上而下),我们可以将其改变为水平
horizontal
:
ggplot(chic, aes(x = date, y = temp, color = season)) +
geom_point() +
labs(x = "Year", y = "Temperature (°F)") +
theme(legend.position = c(.5, .97),
legend.background = element_rect(fill = "transparent")) +
guides(color = guide_legend(direction = "horizontal"))
image.png
其实不只是图例,aes 中设定的属性都可以进行排序。规则是现将排序的列转为因子类型,并对levels 属性进行调整:
chic$season <-
factor(chic$season,
levels = c("Winter", "Spring", "Summer", "Autumn"))
ggplot(chic, aes(x = date, y = temp, color = season)) +
geom_point() +
labs(x = "Year", y = "Temperature (°F)")
image.png
guides
函数的
color
属性专门设置图例颜色标记,比如标记大小:
ggplot(chic, aes(x = date, y = temp, color = season)) +
geom_point() +
labs(x = "Year", y = "Temperature (°F)") +
theme(legend.key = element_rect(fill = NA),
legend.title = element_text(color = "chocolate",
size = 14, face = 2)) +
scale_color_discrete("Seasons:") +
guides(color = guide_legend(override.aes = list(size = 6)))
其中aes 中设置了分类变量,R 会默认设置为guide_legend() :
而连续变量则使用guide_colorbar() :
我们也可以将连续变量修改为分类的样子:
ggplot(chic,
aes(x = date, y = temp, color = temp)) +
geom_point() +
labs(x = "Year", y = "Temperature (°F)", color = "Temperature (°F)") +
guides(color = guide_legend())
image.png
其他的样式还有:
image.png
image.png
除非在aes 中指定变量,否则颜色并不会创建图例,但我们可以借助
scale_color_discrete
:
ggplot(chic, aes(x = date, y = o3)) +
geom_line(aes(color = "line")) +
geom_point(aes(color = "points")) +
labs(x = "Year", y = "Ozone") +
scale_color_discrete("Type:")
如果想要自定义颜色,其实也可以不在aes 中定义color 属性:
ggplot(chic, aes(x = date, y = o3)) +
geom_line(color = "darkorange2") +
geom_point(color = "grey") +
labs(x = "Year", y = "Ozone") + scale_color_discrete("Type:")
image.png
但是会牺牲掉图例的显示(这里我也没有弄清有什么方便的方法),或者使用函数
scale_color_manual
:
ggplot(chic, aes(x = date, y = o3)) +
geom_line(aes(color = "line")) +
geom_point(aes(color = "points")) +
labs(x = "Year", y = "Ozone") +
scale_color_manual(name = NULL,
guide = "legend",
values = c("points" = "darkorange2",
"line" = "gray"))
ggplot(chic, aes(x = date, y = temp, color = season)) +
geom_point() +
labs(x = "Year", y = "Temperature (°F)") +
theme(legend.key = element_rect(fill = "darkgoldenrod1"),
legend.title = element_text(family = "Playfair",
color = "chocolate",
size = 14, face = 2)) +
scale_color_discrete("Seasons:")
我们还可以对图例的标记背景进行修改:
ggplot(chic, aes(x = date, y = temp, color = season)) +
geom_point() +
labs(x = "Year", y = "Temperature (°F)") +
theme(legend.key = element_rect(fill = NA),
legend.title = element_text(color = "chocolate",
size = 14, face = 2)) +
scale_color_discrete("Seasons:") +
guides(color = guide_legend(override.aes = list(size = 6)))
image.png
默认下,如果是多个图都指定了某个分组:
图例标记也会非常智能的显示的。我们可以不希望显示,在几何对象中使用
show.legend = FALSE
:
ggplot(chic, aes(x = date, y = temp, color = season)) +
![]() |
咆哮的葫芦 · winapi - What is the difference between NtFsControlFile() and DeviceIoControl() functions? - Stack Overflow 1 年前 |
![]() |
豪气的馒头 · python 运行powershell命令-掘金 1 年前 |