2013-07-18 40 views
0

我從下面的腳本情節。GGPLOT2指南/傳說中的形狀

require(ggplot2) 

df.shape <- data.frame(
AX = runif(10), 
AY = runif(10), 
BX = runif(10, 2, 3), 
BY = runif(10, 2, 3) 
) 

p <- ggplot(df.shape) 
p <- p + geom_point(aes(x = AX, y = AY, shape = 15)) + 
    geom_point(aes(x = BX, y = BY, shape = 19)) + 
    scale_shape_identity() + 
    guides(shape = guide_legend(override.aes = list(shape = 15, shape = 19))) 
print(p) 

這不會產生一個圖例,描述哪個形狀是「A」,哪個形狀是「B」。請注意,正方形和圓形可能彼此接近,所以我通常不能根據位置定義變量。如何顯示「形狀」圖例?

+2

'scale_shape_identity(指南= 「傳奇」) ' – baptiste

+0

太棒了!顯示形狀圖例,我如何將圖例文本與「A」和「B」(例如)相匹配。我已經試過'導塊(形狀= guide_legend(legend.text = C( 「A」, 「B」))) – Hugh

+0

'scale_shape_identity(導向= 「傳奇」,場所= C(15,19),標籤= LETTERS [ 1:2])' – baptiste

回答

3

我會用reshape重塑我的長格式的數據:

dt <- reshape(df.shape ,direction='long', varying=list(c(1, 3), c(2, 4)), 
     ,v.names = c('X','Y'), times = c('A','B')) 

然後我繪製它只是像這樣

ggplot(dt) + 
    geom_point(aes(x = X, y = Y, shape = time),size=5) + 
    scale_shape_manual(values=c(15,19)) 

enter image description here

+0

這個傳說很棒,但是這些觀點似乎被繪製在錯誤的地方。 – Hugh

+0

@Hugh很好。我修好了它。 – agstudy