ggplot2分面图表轴标签与侧边标签自定义求助
ggplot2分面图表轴标签与侧边标签自定义求助
我用ggplot2做了一张分面折线图,代码如下:
ggplot(df_long, aes(x = Elevation , y = value, color = variable)) + geom_line() + facet_wrap(~variable, nrow = 3 ) + labs(x = c("Elevation" , "Rivers", "NDVI"), y = NULL, color = NULL) + theme_minimal() + theme(legend.position = "none", strip.background = element_blank(), strip.placement = "outside", strip.text.y = element_text(angle = 0), panel.spacing = unit(1, "lines"))+ coord_cartesian(ylim = c(0, 1))
生成的图表是这样的:

现在遇到了两个头疼的问题:
- 我本来想让三个分面的x轴分别显示“Elevation”“NDVI”“Rivers”,结果现在只有第一个分面显示了Elevation,其他分面的x轴标签完全不对;
- 图里标了X的那些侧边标签我想去掉,换成在右侧统一显示“Ibex, Snow Leopard and Cooccurrence”这一个标签,就像Rota等人2016年论文里的那种布局效果。
有没有大佬能帮我解决这两个问题呀?
针对性解决方案
问题1:给每个分面设置独立的x轴标签
facet_wrap没办法直接给每个分面分配不同的x轴标签,我们得换个思路,用facet_grid配合数据预处理来实现:
首先给你的数据框新增一列,用来对应每个variable对应的x轴标签,假设你的三个variable值分别对应Elevation、NDVI、Rivers,就像这样:
library(dplyr) df_long <- df_long %>% mutate(x_axis_label = case_when( variable == "你的第一个variable值" ~ "Elevation", variable == "你的第二个variable值" ~ "NDVI", variable == "你的第三个variable值" ~ "Rivers" ))
然后修改绘图代码,用facet_grid来分面,同时调整主题隐藏不需要的标签:
ggplot(df_long, aes(x = Elevation , y = value, color = variable)) + geom_line() + # 按行分variable,按列分x_axis_label,开启x轴自由缩放 facet_grid(variable ~ x_axis_label, scales = "free_x", space = "free_x") + labs(y = NULL, color = NULL) + theme_minimal() + theme( legend.position = "none", strip.background = element_blank(), # 隐藏上方的x_axis_label分面标签,我们要把它变成x轴标签 strip.text.x = element_blank(), # 先把原来的侧边标签隐藏,后面统一加 strip.text.y = element_blank(), panel.spacing = unit(1, "lines"), # 去掉默认的x轴总标题 axis.title.x = element_blank() ) + coord_cartesian(ylim = c(0, 1))
这样每个分面的x轴就会显示对应的标签了。
问题2:添加右侧统一的侧边标签
要在右侧添加统一的垂直文本,我们可以用annotate配合关闭绘图裁剪的方式,或者用grid包来精准控制:
方法1:用annotate直接添加
在上面的绘图代码里加入annotate和调整边距、裁剪的设置:
ggplot(df_long, aes(x = Elevation , y = value, color = variable)) + geom_line() + facet_grid(variable ~ x_axis_label, scales = "free_x", space = "free_x") + labs(y = NULL, color = NULL) + # 添加右侧垂直文本,x设为Inf让它靠右,angle=270让文本垂直 annotate("text", x = Inf, y = 0.5, label = "Ibex, Snow Leopard and Cooccurrence", angle = 270, hjust = 0.5, size = 4) + theme_minimal() + theme( legend.position = "none", strip.background = element_blank(), strip.text.x = element_blank(), strip.text.y = element_blank(), panel.spacing = unit(1, "lines"), axis.title.x = element_blank(), # 调整右侧边距,给文本留出空间 plot.margin = margin(10, 50, 10, 10) ) + # 关闭绘图区域裁剪,让超出区域的文本显示出来 coord_cartesian(ylim = c(0, 1), clip = "off")
方法2:用grid包精准控制位置
如果觉得annotate的位置不够灵活,可以先绘好图,再用grid包添加文本:
# 先保存绘图对象 p <- ggplot(df_long, aes(x = Elevation , y = value, color = variable)) + geom_line() + facet_grid(variable ~ x_axis_label, scales = "free_x", space = "free_x") + labs(y = NULL, color = NULL) + theme_minimal() + theme( legend.position = "none", strip.background = element_blank(), strip.text.x = element_blank(), strip.text.y = element_blank(), panel.spacing = unit(1, "lines"), axis.title.x = element_blank(), plot.margin = margin(10, 50, 10, 10) ) + coord_cartesian(ylim = c(0, 1), clip = "off") # 先打印图表,再添加文本 print(p) grid::grid.text("Ibex, Snow Leopard and Cooccurrence", x = unit(0.95, "npc"), y = unit(0.5, "npc"), rot = 270, gp = grid::gpar(size = 10))
这样就能完美实现你想要的效果啦!
备注:内容来源于stack exchange,提问作者Hussain Ali
相关产品推荐
相关产品推荐

