控制 R 中 ggplot2 图例项的大小
在本文中,我们将看到如何在 R 编程语言中控制 ggplot2 Legend Items 的大小。要创建 R 图,我们使用ggplot()函数,并且为了制作散点图geom_point()函数被添加到 ggplot()函数。
让我们首先创建一个没有任何修改的常规图,以便差异明显。
例子:
R
# Load Package
library("ggplot2")
# Create a DataFrame
data <- data.frame(Xdata = rnorm(10),
Ydata = rnorm(10),
LegendData = c("ld-01", "ld-02", "ld-03",
"ld-04", "ld-05", "ld-06",
"ld-07", "ld-08", "ld-09",
"ld-10"))
# Create a Scatter Plot
ggplot(data, aes(Xdata, Ydata, color = LegendData)) +
geom_point()
R
# Load Package
library("ggplot2")
# Create a DataFrame
data <- data.frame(Xdata = rnorm(10),
Ydata = rnorm(10),
LegendData = c("ld-01", "ld-02", "ld-03",
"ld-04", "ld-05", "ld-06",
"ld-07", "ld-08", "ld-09",
"ld-10"))
# Create a Scatter Plot and change
# the size of legend
ggplot(data, aes(Xdata, Ydata, color = LegendData)) +
geom_point()+
guides(color = guide_legend(override.aes = list(size = 10)))
输出:
要更改图例的大小,我们必须向 geom_point() 函数添加 guides() 和 guide_legend()函数。在 guides()函数内部,我们采用参数color ,它调用 guide_legend() 指南函数作为值。在 guide_legend()函数,我们采用一个名为override.aes的参数,该参数具有指定图例键的美学参数的列表。在此列表中,我们指定图例的大小。
Syntax : guides(…)
Parameter :
- … : either a string or call to a guide function. here we call guide_legend() guide function.
Return : each scale can be set scale-by-scale
Syntax : guide_legend(override.aes = list())
Parameter :
- override.aes : A list specifying aesthetic parameters of legend key. Inside this list, we specify the size of legend to size object.
Return : Legend Guides for various scales
例子:
电阻
# Load Package
library("ggplot2")
# Create a DataFrame
data <- data.frame(Xdata = rnorm(10),
Ydata = rnorm(10),
LegendData = c("ld-01", "ld-02", "ld-03",
"ld-04", "ld-05", "ld-06",
"ld-07", "ld-08", "ld-09",
"ld-10"))
# Create a Scatter Plot and change
# the size of legend
ggplot(data, aes(Xdata, Ydata, color = LegendData)) +
geom_point()+
guides(color = guide_legend(override.aes = list(size = 10)))
输出: