2011-03-03 36 views
10

Làm cách nào để tạo chú thích thông báo rằng chữ thập đỏ là giá trị trung bình?ggplot2 huyền thoại cho 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

Trả lời

15

Dưới đây là một cách để làm việc đó:

  1. Bản đồ một thẩm mỹ để một hình dạng, tức là aes (hình dạng = "có nghĩa là")
  2. Tạo một quy mô hình thủ công , tức là 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

Nếu bạn muốn sử dụng pch symbolsl nó có thể sử dụng số Unicode. Để làm điều đó, chỉ cần thay thế 'x' bằng giá trị Unicode: "\ U" rồi giá trị Unicode (ví dụ: '" \ U22C4 "'). Xem [Bảng Unicode] [1] [1]: http://www.fileformat.info/info/unicode/char/22c4/index.htm – Facottons

0

Để làm cho nó xuất hiện như một huyền thoại mặc định (vay từ mã @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 
Các vấn đề liên quan