簡體   English   中英

ggplot2:使用stat_smooth時無法覆蓋第二個圖例美觀度

[英]ggplot2: cannot override 2nd legend aesthetics when using stat_smooth

南瓜頭盔(3.1.2),ggplot2_1.0.0。

library(ggplot2)
some_diamonds <- subset(diamonds, color %in% c('D', 'E', 'F'))
g <- ggplot(some_diamonds, aes(carat, depth, linetype=cut, colour=color)) +
    stat_smooth(se=F) + scale_colour_grey()

所需的顏色美感在color的圖例中正確顯示(並且至關重要的是,在圖本身中),但是cut的顏色默認為stat_smooth的默認值(藍色)。 通常的技巧在這種情況下似乎不起作用,例如:

g <- ggplot(some_diamonds, aes(carat, depth, linetype=cut, colour=color)) + 
   stat_smooth(se=F) 
g + scale_colour_grey() + guides(colour = guide_legend(override.aes = 
   list(colour='black')

實際上,我什至不知道如何訪問第二個圖例的任何元素。 這個

g + scale_colour_grey(guide=guide_legend(title="Title", order=2)) 

只需將第一個圖例的標題更改為“標題”; 同樣

g + scale_colour_grey() + 
  guides(colour=guide_legend(override.aes=list(colour='black'), order=2))

無論order的值如何(甚至省略),都覆蓋cut而不是color

我想念什么?

您已經非常接近了! 在這里有用的只是簡單地將guides()參數的名稱更改為您要更改的功能的名稱,即linetype

g + guides(linetype = guide_legend(title = "Variable: cut",  # changes the second legend
                                   override.aes = list(colour = "black")), 
           color = guide_legend(title = "Variable: color"))  # changes the first legend

更改圖例顏色的情節

暫無
暫無

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

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