我已经使用ggplot2创建了折线图,并且颜色图例是连续的。我想将其更改为离散的彩色线条。我有以下代码:
library(ggplot2)
ggplot(df, aes(L)) +
geom_line(aes(y = Updated, colour = S, group = S, linetype = "Updated")) +
geom_line(aes(y = Current, colour = S, group = S, linetype = "Current")) +
scale_color_gradient() +
labs(x = element_text("Span Length (ft)"), y = element_blank(),
title = "Moment Live Load Distribution Factors \n Exterior Girder - Multi Lane",
linetype = "AASHTO", color = "Spacing (ft)") +
scale_linetype_manual(values = c(1, 3),
labels = c("Updated", "Current")) +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5, margin = margin(0,0,20,0), size = 18),
legend.position="top",
legend.box.background = element_rect(colour = "black", size = 0.5),
legend.box.margin = margin(0,0,0,0), legend.background = element_blank())
我想将图例的连续部分转换为宽度与图例其余部分相同的彩色线条。谢谢!
答案 0 :(得分:1)
您有多个选项,具体取决于变量的含义:
将变量转换为因子:
library(ggplot2)
ggplot(mtcars) +
geom_line(aes(mpg, wt, colour = factor(cyl)))
由reprex package(v0.3.0)于2020-04-15创建
使用分类图例:
ggplot(mtcars) +
geom_line(aes(mpg, wt, colour = cyl)) +
scale_color_binned(guide = guide_coloursteps())
由reprex package(v0.3.0)于2020-04-15创建
请注意,guide = guide_coloursteps()
目前不执行任何操作,但可以帮助您自定义图例。