簡體   English   中英

問題:qqplot圖例線型不同

[英]Problem: qqplot legend different linetypes

legend <- c("score" = "black", "answer" = "red")

plot <- df_l %>% ggplot(aes(date, score, color = "score")) + geom_line() +
  geom_vline(aes(xintercept = getDate(df_all %>% filter(name == List[5])), color = "answer"), linetype = "dashed", size = 1,) +
  scale_color_manual(name = "Legend", values = legend) +
  scale_x_date(labels = date_format("%m/%y"), breaks = date_breaks("months")) +
  theme(axis.text.x = element_text(angle=45)) +
  labs(title = "", x = "", y = "", colors = "Legend")

我得到了上面的結果,但無法弄清楚如何解決圖例中兩條線總是混淆的問題。 一個圖例當然應該只顯示細黑線,而另一個則顯示黑色虛線。 提前致謝!

您遇到的問題是geom_vline會導致圖例項是垂直線,而geom_line會產生水平線項。 一種解決方案是通過在geom_line ...而不是geom_vline中指定color=審美來手動創建圖例類型。 然后,您可以使用geom_blank創建一種“虛擬”geom,作為color=美學的持有 object 。 然后,您可以通過 scale_color_manual 為這兩個項目指定scale_color_manual 這是一個例子:

set.seed(12345)
df <- data.frame(x=1:100,y=rnorm(100))

ggplot(df, aes(x,y)) + theme_bw() +
  geom_line(aes(color='score')) +
  geom_vline(aes(xintercept=4), linetype=2, color='red', show.legend = FALSE) +

  geom_blank(aes(color='my line')) +

  scale_color_manual(name='Legend', values=c('my line'='red','score'='black'))

在此處輸入圖像描述

這為顏色創造了一個傳奇......但不幸的是,“我的線”是純紅色的,當它應該是虛線時。 要解決這個問題,您只需以相同的方式應用linetype=美學。

ggplot(df, aes(x,y)) + theme_bw() +
  geom_line(aes(color='score', linetype='score')) +
  geom_vline(aes(xintercept=4), linetype=2, color='red', show.legend = FALSE) +

  geom_blank(aes(color='my line', linetype='my line')) +

  scale_linetype_manual(name='Legend', values=c('my line'=2,'score'=1)) +
  scale_color_manual(name='Legend', values=c('my line'='red','score'='black'))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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