2013-06-17 23 views
45

Tôi không thể tìm ra cách thiết lập một chú thích cho cốt truyện này theo cách thủ công. Tất cả những gì tôi thực sự muốn là một huyền thoại đơn giản về quyền sử dụng ba màu và có một cái tên bên cạnh mỗi màu.Xây dựng một chú giải thủ công cho một ô phức tạp

enter image description here

Mã hiện nay trông như thế này:

a <-c("S1","S2","S3","S4","S5","S6","S7","S8","S9") #names 
b <-c(0.23,0.26,0.55,0.56,0.36,0.23,0.18,0.06,0.04) #mean t0 
c <-c(0.64,0.6,0.81,1.4,0.89,0.55,0.48,0.22,0.09) #mean t1 
d <-c(0.20,0.23,0.52,0.53,0.33,0.20,0.15,0.04,0.03) #SD low t0 
e <-c(0.26,0.29,0.58,.59,0.39,0.26,0.21,0.08,0.05) #SD high t0 
f <-c(0.67,0.63,0.86,1.44,0.93,0.59,0.51,0.25,0.10) #SD high t1 
g <-c(0.61,0.57,0.78,1.36,0.85,0.53,0.45,0.19,0.08) #SD low t1 
h <-c(0.41,0.34,0.26,0.84,0.53,0.32,0.30,0.16,0.05) #absolute change 

data <- data.frame(a,b,c,d,e,f,g,h) 

ggplot(data=data,aes(a)) + 
    geom_bar(stat="identity", aes(y=h),fill="#62c76b",colour="#333333")+ #green 
    geom_line(aes(y=b,group=1),size=1.0,colour="#f04546") + #red 
    geom_point(aes(y=b),size=3, colour="#f04546") +   #red 
    geom_errorbar(aes(ymin=d, ymax=e), colour="#f04546", width=0.1, size=.8) + 
    geom_line(aes(y=c,group=1),size=1.0,colour="#3591d1") + #blue 
    geom_point(aes(y=c),size=3, colour="#3591d1") +   #blue 
    geom_errorbar(aes(ymin=f, ymax=g), colour="#3591d1", width=0.1, size=.8) + 
    ylab("Symptom severity") + xlab("PHQ-9 symptoms") + 
    ylim(0,1.6) + 
    theme_bw() + 
    theme(axis.title.x = element_text(size = 15, vjust=-.2)) + 
    theme(axis.title.y = element_text(size = 15, vjust=0.3)) 
+3

Rất hữu ích - nơi tôi sẽ có mã từ nếu không muốn nói tài liệu và hướng dẫn? – Torvon

Trả lời

63

Bạn cần để ánh xạ các thuộc tính đến thẩm mỹ (màu sắc trong tuyên bố aes) để tạo ra một huyền thoại.

cols <- c("LINE1"="#f04546","LINE2"="#3591d1","BAR"="#62c76b") 
ggplot(data=data,aes(x=a)) + 
    geom_bar(stat="identity", aes(y=h, fill = "BAR"),colour="#333333")+ #green 
    geom_line(aes(y=b,group=1, colour="LINE1"),size=1.0) + #red 
    geom_point(aes(y=b, colour="LINE1"),size=3) +   #red 
    geom_errorbar(aes(ymin=d, ymax=e, colour="LINE1"), width=0.1, size=.8) + 
    geom_line(aes(y=c,group=1,colour="LINE2"),size=1.0) + #blue 
    geom_point(aes(y=c,colour="LINE2"),size=3) +   #blue 
    geom_errorbar(aes(ymin=f, ymax=g,colour="LINE2"), width=0.1, size=.8) + 
    scale_colour_manual(name="Error Bars",values=cols) + scale_fill_manual(name="Bar",values=cols) + 
    ylab("Symptom severity") + xlab("PHQ-9 symptoms") + 
    ylim(0,1.6) + 
    theme_bw() + 
    theme(axis.title.x = element_text(size = 15, vjust=-.2)) + 
    theme(axis.title.y = element_text(size = 15, vjust=0.3)) 

enter image description here

Tôi hiểu nơi Roland đến từ, nhưng vì đây là chỉ có 3 thuộc tính, và các biến chứng phát sinh từ thanh chồng và quán bar lỗi này có thể là hợp lý để rời khỏi dữ liệu ở định dạng rộng như nó Là. Nó có thể giảm nhẹ về độ phức tạp của using geom_pointrange.


Để thay đổi màu nền cho chú giải thanh lỗi trong bản gốc, hãy thêm + theme(legend.key = element_rect(fill = "white",colour = "white")) vào đặc điểm lô. Để hợp nhất các truyền thuyết khác nhau, bạn thường cần phải có ánh xạ nhất quán cho tất cả các phần tử, nhưng nó hiện đang tạo ra một tạo phẩm của nền đen cho tôi. Tôi nghĩ rằng guide = guide_legend(fill = NULL,colour = NULL) sẽ thiết lập nền thành null cho huyền thoại, nhưng nó không. Có lẽ đáng giá một câu hỏi khác.

ggplot(data=data,aes(x=a)) + 
    geom_bar(stat="identity", aes(y=h,fill = "BAR", colour="BAR"))+ #green 
    geom_line(aes(y=b,group=1, colour="LINE1"),size=1.0) + #red 
    geom_point(aes(y=b, colour="LINE1", fill="LINE1"),size=3) +   #red 
    geom_errorbar(aes(ymin=d, ymax=e, colour="LINE1"), width=0.1, size=.8) + 
    geom_line(aes(y=c,group=1,colour="LINE2"),size=1.0) + #blue 
    geom_point(aes(y=c,colour="LINE2", fill="LINE2"),size=3) +   #blue 
    geom_errorbar(aes(ymin=f, ymax=g,colour="LINE2"), width=0.1, size=.8) + 
    scale_colour_manual(name="Error Bars",values=cols, guide = guide_legend(fill = NULL,colour = NULL)) + 
    scale_fill_manual(name="Bar",values=cols, guide="none") + 
    ylab("Symptom severity") + xlab("PHQ-9 symptoms") + 
    ylim(0,1.6) + 
    theme_bw() + 
    theme(axis.title.x = element_text(size = 15, vjust=-.2)) + 
    theme(axis.title.y = element_text(size = 15, vjust=0.3)) 

enter image description here


Để thoát khỏi nền đen trong truyền thuyết, bạn cần phải sử dụng đối số override.aes đến guide_legend. Mục đích của việc này là để cho phép bạn chỉ định một khía cạnh cụ thể của chú giải có thể không được gán đúng.

ggplot(data=data,aes(x=a)) + 
    geom_bar(stat="identity", aes(y=h,fill = "BAR", colour="BAR"))+ #green 
    geom_line(aes(y=b,group=1, colour="LINE1"),size=1.0) + #red 
    geom_point(aes(y=b, colour="LINE1", fill="LINE1"),size=3) +   #red 
    geom_errorbar(aes(ymin=d, ymax=e, colour="LINE1"), width=0.1, size=.8) + 
    geom_line(aes(y=c,group=1,colour="LINE2"),size=1.0) + #blue 
    geom_point(aes(y=c,colour="LINE2", fill="LINE2"),size=3) +   #blue 
    geom_errorbar(aes(ymin=f, ymax=g,colour="LINE2"), width=0.1, size=.8) + 
    scale_colour_manual(name="Error Bars",values=cols, 
         guide = guide_legend(override.aes=aes(fill=NA))) + 
    scale_fill_manual(name="Bar",values=cols, guide="none") + 
    ylab("Symptom severity") + xlab("PHQ-9 symptoms") + 
    ylim(0,1.6) + 
    theme_bw() + 
    theme(axis.title.x = element_text(size = 15, vjust=-.2)) + 
    theme(axis.title.y = element_text(size = 15, vjust=0.3)) 

enter image description here

+0

Andy, cảm ơn rất nhiều. (1) Có cách nào để "hợp nhất" 2 dòng với huyền thoại 1 thanh không? (2) Có cách nào để tự thay đổi chú thích không? Ví dụ. màu của đường viền màu xám xung quanh LINE1 và LINE2? Tôi không tìm thấy bất cứ điều gì trong tài liệu ggplot2 (nơi tất cả các mã của tôi là từ). – Torvon

+0

@Torvon, để có huyền thoại được hợp nhất, bạn thường cần phải có ánh xạ nhất quán cho mỗi geom. Tôi sẽ cập nhật với một ví dụ, nhưng điều này tạo ra một tạo tác lạ (có lẽ bởi vì các dòng không có nội thất đầy). Nó có thể là giá trị câu hỏi khác để xem nếu nó có thể được sửa đổi theo ý thích của bạn. Tôi sẽ nói mặc dù tôi không nghĩ rằng truyền thuyết khác nhau là một ý tưởng tuyệt vời. Nó là khá đơn giản với hai huyền thoại IMO. –