R语言ggplot2画图:图例位置设置指南
可以使用ggplot2中的theme函数中的'legend.position'参数来设置图例的位置。以下是一个示例代码:
library(ggplot2)
# 创建数据集
data <- data.frame(
x = c(1, 2, 3, 4, 5),
y = c(3, 5, 2, 6, 4),
group = c('A', 'B', 'C', 'D', 'E')
)
# 画图
ggplot(data, aes(x = x, y = y, color = group)) +
geom_line() +
theme(legend.position = 'bottom') # 设置图例位置在底部
在theme函数中,可以将'legend.position'参数设置为'top'、'bottom'、'left'或'right'来分别将图例放置在图的顶部、底部、左侧或右侧。
原文地址: https://www.cveoy.top/t/topic/nCQF 著作权归作者所有。请勿转载和采集!