2016-08-17 150 views
-1

這裏是一個傻data.frame添加輪廓顏色,點屬性

df <- read.table(textConnection(
"pole medal bag  x  y 
north gold paper 0.852 0.423 
north gold plastic 0.277 0.055 
north silver paper 0.257 0.211 
north silver plastic 0.457 0.614 
north bronze paper 0.825 0.299 
north bronze plastic 0.672 0.126 
south gold paper 0.482 0.764 
south gold plastic 0.603 0.869 
south silver paper 0.327 0.451 
south silver plastic 0.147 0.672 
south bronze paper 0.140 0.466 
south bronze plastic 0.833 0.325 
"), header = TRUE) 

我知道如何繪製的散點圖中使用某種顏色的方法,這些數據和圖形來表示兩個因素屬性;例如:

library(ggplot2) 
ggplot(data = df, aes(x = x, y = y)) + 
geom_point(size = 4, aes(shape = pole, color = bag)) 

enter image description here

我想補充一點特徵以指示第三個因素屬性(在這種情況下medal)。想到的一種可能性是一個彩色輪廓。

有沒有一種方便的方法來做到這一點? (問題的一個棘手方面是輪廓的調色板必須與用於點填充的調色板不同,因爲對於每個點,輪廓和填充必須在視覺上可區分。)

更新:

當我嘗試格里高爾的建議下,點看的權利,但傳說是一團糟:

enter image description here

+1

你可能需要建立自己的填充顏色和顏色的配色方案,使這看起來OK。加上一些傳奇作品。如:scale_fill_manual(values = c(「pink」,「white」))+ \t scale_color_manual(values = c(「brown」,「gold」,「grey74」))+ \t guides(fill = guide_legend(override。 aes = list(color = c(「pink」,「white」))),color = guide_legend(override.aes = list(shape = 21)))' – aosmith

+0

這個問題爲什麼低估? – kjo

+0

@ kjo,可能是因爲這在'geom_point'的文檔中討論了一段時間。另外[見例如這裏](http://stackoverflow.com/questions/15965870/fill-and-border-colour-in-geom-point-scale-colour-manual-in-ggplot)和 – Axeman

回答

3

可以呀!沒有爲?geom_point在幫助一個例子:

# For shapes that have a border (like 21), you can colour the inside and 
# outside separately. Use the stroke aesthetic to modify the width of the 
# border 
ggplot(mtcars, aes(wt, mpg)) + 
    geom_point(shape = 21, colour = "black", fill = "white", size = 5, stroke = 5) 

在你的情況,你會希望使用形狀像21這樣(與輪廓圓)和24(含大綱三角形):

ggplot(data = df, aes(x = x, y = y)) + 
    geom_point(aes(shape = pole, color = medal, fill = bag), 
      size = 4, stroke = 2) + 
    scale_shape_manual(values = c(21, 24)) 

請注意,當使用兩者時,fill對應於點的中心和color的輪廓。您可以通過設置stroke來更改輪廓的重量。

正如在評論中指出的那樣,你必須做一些額外的調整來獲得正確的圖例。添加到上面的陰謀:

fill_col = c("pink", "white") 
outline_col = c("brown", "gold", "grey74") 

scale_fill_manual(values = fill_col) + 
scale_color_manual(values = outline_col) + 
guides(fill = guide_legend(override.aes = list(color = fill_col)), 
     color = guide_legend(override.aes = list(shape = 21)))