2015-12-21 55 views
6

如何只有一個類別的圖例部分?我試圖與override.aes混亂沒有成功。或者,可以將期望的輸出視爲僅具有形狀但不具有比例的圖例。ggplot2只有一個類別/只有形狀和沒有比例的圖例

ggplot(iris) + 
    geom_point(aes(x=Sepal.Width, y=Sepal.Length, color=Species, size=Sepal.Length))+ 
    scale_size_continuous("Legend with \n only 1 circle ",range = c(5,10))+ 
    guides(size = guide_legend(override.aes=list(range= c(1,5)))) 

image1

product我試圖讓該類型的例證:map2

點進行縮放,但傳說不報告的規模。

+0

我沒有一個完整的答案左右,但這個可以幫助你上手。我繪製了額外的geom_point()(來自第一行的虹膜)ggplot(虹膜)+ geom_point(aes(x = Sepal.Width,y = Sepal.Length,color = Species),size = 2)+ scale_size_continuous \ n只有1個圓圈的圖例「,label =」「)+ geom_point(data = test,aes(x = Sepal.Width,y = Sepal.Length,size = Sepal.Length),color ='red')+ 指南(size = guide_legend(override.aes = list(size = 3))) – MLavoie

回答

5

只需在規模上創建一個break即可。您也可以爲其添加自定義標籤(這裏是"")。您還可以使用您選擇的中斷來控制圖例中點的大小。

scale_color_discrete()行是否存在,因爲否則1點傳說將在上面,而不是你在你想要的圖片。

require(ggplot2) 
g <- ggplot(iris) + geom_point(aes(x = Sepal.Width, y = Sepal.Length, 
            color = Species, size = Sepal.Length)) + 
    scale_color_discrete(name = "Color") + 
    scale_size_continuous(name = "Legend with \n only 1 circle", 
         breaks = 5, labels = "") 

enter image description here

2

雖然@choff解決方案是我給的例子最好的解決辦法,這裏是略有不同的一個最後我用我需要有過圓的範圍大小控制。

ggplot(iris) + 
    geom_point(aes(x=Sepal.Width, y=Sepal.Length, color=Species, size=Sepal.Length))+ 
    scale_size_continuous("Legend with \n only 1 circle ",range = c(5,10), labels=c("","","","",""))+ 
    guides(size =guide_legend(override.aes=list(size=c(4,0,0)))) + 
    theme(legend.key = element_blank()) 
2

從你的目標映射圖,它看起來像你想你的傳奇由顏色和形狀圖表符號分開。大小僅用於設置符號的大小。此外,您的數據可能會有一個專欄,分別列出那些沒有投資的國家。因此,我們可以在虹膜中添加一列,通過兩個值將行分開,將顏色和形狀映射到該列,然後在單個組合圖例中顯示這兩種美學的圖例。代碼如下:

sp <- ggplot(transform(iris, Flower_size = ifelse(Petal.Width < 1, "Small Flower","Big Flower"))) 
sp <- sp + geom_point(aes(x=Sepal.Width, y=Sepal.Length, fill=Flower_size, shape=Flower_size, size=Sepal.Length), colour=NA) 
sp <- sp + scale_size_continuous(range = c(4,7)) 
sp <- sp + scale_shape_manual(values=c(21, 22)) 
sp <- sp + scale_fill_manual(values=c("grey", "orange")) 
sp <- sp + labs(fill="", shape="") 
sp <- sp + guides(size=FALSE, fill=guide_legend(override.aes=list(size=7))) 
sp <- sp + theme(legend.text=element_text(size=12)) 
plot(sp) 

enter image description here

相關問題