Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 减小整体图例大小(元素和文本)_R_Ggplot2_Legend - Fatal编程技术网

R 减小整体图例大小(元素和文本)

R 减小整体图例大小(元素和文本),r,ggplot2,legend,R,Ggplot2,Legend,我正在用R ggplot2绘制一些数据。我有两个变量,以散点图的形式绘制,另外两个维度以颜色和形状的形式绘制。但是,绘图与外部图例的效果不太好(在x轴上变小) 我把传奇搬进去了,但现在传奇太大了!是否有一种方法可以使其更小,而不需要单独减小每个组件的大小(图例标题、图例标签、图例符号) 修改文本大小如何 theme(legend.title = element_text( size=2), legend.text=element_text(size=2)) 您必须减小图例元素的大小(覆盖传

我正在用R ggplot2绘制一些数据。我有两个变量,以散点图的形式绘制,另外两个维度以颜色和形状的形式绘制。但是,绘图与外部图例的效果不太好(在x轴上变小)

我把传奇搬进去了,但现在传奇太大了!是否有一种方法可以使其更小,而不需要单独减小每个组件的大小(图例标题、图例标签、图例符号)


修改文本大小如何

  theme(legend.title = element_text( size=2), legend.text=element_text(size=2))

您必须减小图例元素的大小(覆盖传递的参数
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))
库(ggplot2)

p对图例的文本有效,但符号和框的大小仍然太大。为了完美起见,您只忽略了更改图例中的行间距。图例的总大小相同,但线条更为独立。@DiogoSantos捕捉得好!为该函数添加了参数。
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)