簡體   English   中英

如何在不使用顏色、線型或形狀的情況下 plot 圖例上的線名

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM