如何仅为一系列数据创建 ggplot 图例,然后为其他水平线添加图例?
How to create an legend for ggplot for just one series of data and then add legend for additional horizontal lines?
我想准备一个简单的图,其中包含一些点和带有图例的水平线。下面的代码生成了所需的图和图例,但图例符号是形状和线条的组合,而我只想要形状的形状和线条的线条。
dat <- iris %>% select(Sepal.Length)
dat$Type <- "Sepal.Length"
ggplot() +
geom_point(data = dat, aes(x = as.numeric(row.names(dat)), y = Sepal.Length, colour = Type), shape = 10, size = 2) +
geom_hline(aes(yintercept = 6, colour = "Some line"), linetype = "dashed")
使用 scale_*_manual 指定自定义线型和形状,如下所示:
dat %>%
ggplot() +
geom_point(aes(x = as.numeric(row.names(dat)), y = Sepal.Length, shape = Type), size = 2) +
geom_hline(aes(yintercept = 6, linetype = 'Some line')) +
scale_linetype_manual(values = c('Some line' = 'dashed')) +
scale_shape_manual(values = c('Sepal.Length' = 10))
我想准备一个简单的图,其中包含一些点和带有图例的水平线。下面的代码生成了所需的图和图例,但图例符号是形状和线条的组合,而我只想要形状的形状和线条的线条。
dat <- iris %>% select(Sepal.Length)
dat$Type <- "Sepal.Length"
ggplot() +
geom_point(data = dat, aes(x = as.numeric(row.names(dat)), y = Sepal.Length, colour = Type), shape = 10, size = 2) +
geom_hline(aes(yintercept = 6, colour = "Some line"), linetype = "dashed")
使用 scale_*_manual 指定自定义线型和形状,如下所示:
dat %>%
ggplot() +
geom_point(aes(x = as.numeric(row.names(dat)), y = Sepal.Length, shape = Type), size = 2) +
geom_hline(aes(yintercept = 6, linetype = 'Some line')) +
scale_linetype_manual(values = c('Some line' = 'dashed')) +
scale_shape_manual(values = c('Sepal.Length' = 10))