R语言中theme的调整技巧汇总-持续更新
1.如何扩充图片的上右下左位置的空白区域:
unit(c(1.1, 1, 0.4, 0.5), “cm”) 表示使用厘米作为单位设置各个方向的边距,c(1.1, 1, 0.4, 0.5) 对应的顺序是:上、右、下、左。
theme(plot.margin = unit(c(1.1, 1, 0.4, 0.5), "cm")) # 增加上方的空白
2.如何调整图例中tick的颜色
# 修改图例的刻度颜色
p1 <- p1 + guides(
fill = guide_colorbar(
ticks.colour = "black", # 将图例刻度颜色设置为黑色
ticks.linewidth = 0.8 # 设置刻度线的宽度
)
)
3.如何调整图例中线条的粗细: guides(fill = guide_legend(override.aes = list(color = “black”, linewidth = 0.5)))
p <- ggplot(data, aes(x = .data[[group_var]], y = .data[[te_var]], fill = .data[[group_var]])) +
geom_errorbar(width = 0.08, linewidth = 1, position = position_dodge(0.9), stat = "boxplot") +
geom_boxplot(outlier.size = -1, width = 0.3, position = position_dodge(0.9), fatten = 1, linewidth = 1) +
theme_classic() +
labs(y = y_label, x = x_label) +
scale_y_continuous(limits = y_limits, breaks = y_breaks) +
theme(
panel.background = element_rect(fill = "white", colour = "black", linewidth = 0.5),
axis.title.y = element_text(size = 25, face = "bold", color = "black"),
axis.title.x = element_blank(),
axis.text = element_text(size = 17, face = "bold", color = "black"),
legend.position = c(0.91, 0.92),
legend.title = element_blank(),
legend.text = element_text(size = 8, face = "bold", family = "sans")
) +
scale_fill_manual(values = fill_values) +
# guides(fill = guide_legend(override.aes = list(alpha = 0.8, size =0.1))) # 调整图例符号的大小和透明度
guides(fill = guide_legend(override.aes = list(color = "black", linewidth = 0.5)))
4.如何调整图例中字体的类型,大小
legend.text = element_text(size = 8, face = "bold", family = "sans")