[英]How to avoid zig-zag plot when using geom_line with color and linetype
[英]How to plot the name of a line on a legend without using color, linetype or shape
在这个 plot 中,我想在图例上显示红色实线的名称(“c”)。 我该怎么做,因为那条线必须是“红色”,是“实心”并且没有“点形状”?
在这种情况下,如何在图例上显示“c”行的名称?
library(tidyverse)
df1 <- tibble(line_label = c('a', 'a', 'b', 'b'),
year = c(2010, 2012, 2010, 2012),
value = c(0.1, 0.2, 0.3, 0.4))
df2 <- tibble(line_label = c('c', 'c'),
year = c(2010, 2012),
value = c(0.15, 0.35))
p <- ggplot(data = df1, aes(x = year, y = value)) +
geom_line(aes(color = line_label), size = 1) +
scale_linetype_manual(values=c("solid", "dashed"),
guide = guide_legend(ncol = 2)) +
geom_point(aes(shape = line_label, color = line_label), size = 2) +
geom_line(data = df2, aes(x = year, y = value),
alpha = .8, size = 2, color = "#DA2537") +
scale_color_manual(values=c("#BFBFBF", "#173C70")) +
scale_x_continuous(breaks=seq(2010, 2012, by = 1))
plot(p)
由代表 package (v0.3.0) 于 2019 年 10 月 30 日创建
这是一种使用is_special
列思想的方法。
df_all = rbind(df1, df2)
df_all$is_special = ifelse(df_all$line_label == "c", "y", "n")
ggplot(data = df_all, aes(x = year, y = value, color = line_label)) +
geom_line(aes(linetype = line_label, size = is_special, alpha = is_special)) +
geom_point(aes(shape = line_label), size = 3) +
scale_color_manual(values=c("#BFBFBF", "#173C70", "#DA2537")) +
scale_linetype_manual(values=c("solid", "dashed", "solid")) +
scale_size_manual(values = c("n" = 1, "y" = 2), guide = "none") +
scale_alpha_manual(values = c("n" = 1, "y" = 0.8), guide = "none") +
scale_x_continuous(breaks=seq(2010, 2012, by = 1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.