2011-03-03 115 views
10

我該如何創建一個圖例,告知紅叉十字是什麼意思?ggplot2 stat_summary的圖例

ggplot(results, aes(x=factor, y=proportionPositive)) + 
geom_boxplot() + 
stat_summary(fun.data = "mean_cl_normal", colour = "red", shape=4) 

enter image description here

回答

15

這裏是做的一種方法:

  1. 地圖美學到一個形狀,即AES(形狀= 「意思是」)
  2. 創建手工形狀規模,即scale_shape_manual()
# Create dummy data 
results <- data.frame(
    factor=factor(rep(1:10, 100)), 
    proportionPositive=rnorm(1000)) 

# Plot results 
ggplot(results, aes(x=factor, y=proportionPositive)) + 
     geom_boxplot() + 
     stat_summary(fun.data = "mean_cl_normal", 
       aes(shape="mean"), 
       colour = "red", 
       geom="point") + 
     scale_shape_manual("", values=c("mean"="x")) 

enter image description here

+1

如果你想使用PCH symbolsl有可能使用Unicode數字。爲了做到這一點,只需用一個Unicode值替換'x':「\ U」然後是Unicode值(例如'「\ U22C4」')。參見[Unicode表] [1] [1]:http://www.fileformat.info/info/unicode/char/22c4/index.htm – Facottons

0

,使它看起來像一個默認的傳說(從@Andrie代碼借款):

ggplot(results, aes(x=factor, y=proportionPositive)) + 
     geom_boxplot() + 
     stat_summary(fun.data = "mean_cl_normal", 
       aes(shape=""), # Leave empty 
       colour = "red", 
       geom="point") + 
     scale_shape_manual("mean", values= "") # Will show mean on top of the line