ggplot2 不显示 geom_abline 内特征的图例
ggplot2 does not display legends for features within geom_abline
我无法让 ggplot 显示仅在 geom_abline 中定义的颜色和线型的图例。
我在下面添加了一个非常简单的示例。
它生成如图所示的图(无图例)。
我也试过在情节中添加 scale_color_manual,但这似乎并没有起到作用。
有人知道如何让 ggplot2 显示图例吗?
library(ggplot2);
xy_data = data.frame(x=runif(100, min=-0.5, max=0.5),
y=runif(100, min=-0.5, max=0.5));
slope_data = data.frame(slope = c(-1, -0.5, 0.5, 1.0),
model = paste0("m", seq(1,4)),
robust = rep(c("Robust", "Non-robust"), 2))
merged_data = merge(xy_data, slope_data)
slope_plot =
ggplot()+
geom_point(data=xy_data, aes(x=x, y=y))+
geom_abline(data=slope_data, aes(intercept=0, slope=slope, color=model, linetype=robust))
ggsave(plot=slope_plot, file="no_legend_slope_plot.png")
你可以试试:
slope_plot = ggplot() +
geom_point(data=xy_data, aes(x=x, y=y)) +
geom_abline(data=slope_data, aes(intercept=0, slope=slope, color=model, linetype=robust), show_guide=TRUE)
我无法让 ggplot 显示仅在 geom_abline 中定义的颜色和线型的图例。
我在下面添加了一个非常简单的示例。 它生成如图所示的图(无图例)。
我也试过在情节中添加 scale_color_manual,但这似乎并没有起到作用。 有人知道如何让 ggplot2 显示图例吗?
library(ggplot2);
xy_data = data.frame(x=runif(100, min=-0.5, max=0.5),
y=runif(100, min=-0.5, max=0.5));
slope_data = data.frame(slope = c(-1, -0.5, 0.5, 1.0),
model = paste0("m", seq(1,4)),
robust = rep(c("Robust", "Non-robust"), 2))
merged_data = merge(xy_data, slope_data)
slope_plot =
ggplot()+
geom_point(data=xy_data, aes(x=x, y=y))+
geom_abline(data=slope_data, aes(intercept=0, slope=slope, color=model, linetype=robust))
ggsave(plot=slope_plot, file="no_legend_slope_plot.png")
你可以试试:
slope_plot = ggplot() +
geom_point(data=xy_data, aes(x=x, y=y)) +
geom_abline(data=slope_data, aes(intercept=0, slope=slope, color=model, linetype=robust), show_guide=TRUE)