gpt4 book ai didi

r - 减少整体图例大小(元素和文本)

转载 作者:行者123 更新时间:2023-12-03 14:57:57 24 4
gpt4 key购买 nike

我正在用 R ggplot2 绘制一些数据。我将两个变量绘制为散点图,另外两个维度绘制为颜色和形状。但是,该图不适用于外部图例(在 x 轴上很小)。

我把传说搬到里面了,但现在传说太大了!有没有办法让它更小而不涉及分别减小每个单独组件的大小(图例标题、图例标签、图例符号)?

library(ggplot2)
p1 = ggplot(allPars, aes(x = log10(growthRate), y = log10(k), col = Background, shape = Timepoint))+
geom_point(size = 2)+
theme(legend.position = c(0.5,0.5))+
xlab("Log10 Growth Rate")+
ylab("Log10 K")
fig1 = plot_grid(p1, labels = "AUTO")
save_plot(filename = "~/projects/phd/Chapter4/fig4.pdf", plot = fig1, scale = 1)

enter image description here

最佳答案

您必须减小图例元素的大小(覆盖传递的参数 size =2 )并减小字体大小。

创建带有大图例的示例图

library(ggplot2)
p <- ggplot(mtcars,
aes(drat, mpg, color = factor(gear), shape = factor(vs))) +
geom_point(size = 2) +
theme_classic() +
theme(legend.position = c(0.1, 0.7))

减小形状元素的大小
# Overwrite given size (2) to 0.5 (super small)
p <- p + guides(shape = guide_legend(override.aes = list(size = 0.5)))

减少颜色元素的大小
p <- p + guides(color = guide_legend(override.aes = list(size = 0.5)))

减小图例字体的大小
p <- p + theme(legend.title = element_text(size = 3), 
legend.text = element_text(size = 3))

您还可以编写自定义函数来修改您的绘图:
addSmallLegend <- function(myPlot, pointSize = 0.5, textSize = 3, spaceLegend = 0.1) {
myPlot +
guides(shape = guide_legend(override.aes = list(size = pointSize)),
color = guide_legend(override.aes = list(size = pointSize))) +
theme(legend.title = element_text(size = textSize),
legend.text = element_text(size = textSize),
legend.key.size = unit(spaceLegend, "lines"))
}

# Apply on original plot
addSmallLegend(p)

最终的情节看起来像这样

enter image description here

关于r - 减少整体图例大小(元素和文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52297978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com