2013-08-29 30 views
6

tôi đã tạo ra âm mưu này với đoạn mã sau:Label âm mưu thanh với geom_text trong ggplot

library(ggplot2); library(reshape2); library(plyr) 

likert <- data.frame(age = c(rep("young", 5), rep("middle", 5), rep("old", 5)), 
        score1 = c(rep("unlikely", 1), rep("likely", 1), rep("very likely", 13)), 
        score2 = c(rep("disagree", 6), rep("neutral", 4), rep("agree", 5)), 
        score3 = c(rep("no", 5), rep("maybe", 7), rep("yes", 3))) 

meltedLikert <- melt(dlply(likert, .(age), function(x) llply(x, table))) 

names(meltedLikert) <- c("score", "count", "variable", "age") 

ggplot(meltedLikert[meltedLikert$variable != "age",], aes(variable, count, fill=score)) + 
    geom_bar(position="dodge", stat="identity") + 
    geom_text(data=data.frame(meltedLikert), aes(variable, count, group=score, label=meltedLikert$score), size=4) + 
    facet_grid(age ~ .) 

enter image description here

Làm thế nào tôi có thể gắn nhãn text vị trí vì vậy mỗi nhãn score ngồi trên thanh tương ứng cho variable đầu mỗi thanh?

Trả lời

10

Theo câu trả lời trong linked question, thêm position = position_dodge(width=0.9) đến geom_text dòng gọi lên các giá trị:

ggplot(meltedLikert[meltedLikert$variable != "age",], 
     aes(variable, count, fill=score)) + 
    geom_bar(position="dodge", stat="identity") + 
    geom_text(data=data.frame(meltedLikert), 
      aes(variable, count, group=score, label=meltedLikert$score), 
      position = position_dodge(width=0.9), 
      size=4) + 
    facet_grid(age ~ .) 

enter image description here

Tuy nhiên, tôi cũng muốn chỉ ra một vài thứ khác. Bạn không nên sử dụng meltedLikert$score trong cuộc gọi aes(); bạn chỉ nên tham khảo những thứ trong khung dữ liệu được chuyển thành data. Ngoài ra, meltedLikert đã là data.frame, do đó, hãy gọi data.frame() trên đó là không cần thiết (mặc dù không gây hại gì).

Cải tiến thực sự là cách bạn tạo bảng tính để bắt đầu. Xem xét việc này thay vì:

tabulatedLikert <- ldply(likert[-1], function(sc) { 
    as.data.frame(table(age = likert$age, score = sc)) 
}) 
ggplot(tabulatedLikert, aes(x=.id, y=Freq, fill=score)) + 
    geom_bar(position="dodge", stat="identity") + 
    geom_text(aes(label=score), position=position_dodge(width=0.9), size=4) + 
    facet_grid(age ~ .) 

enter image description here

Bạn có thể sửa thứ tự của các thanh bằng cách sửa chữa chúng trong dữ liệu gốc:

likert2 <- mutate(likert, 
        score1 = factor(score1, levels=c("unlikely", "likely", "very likely")), 
        score2 = factor(score2, levels=c("disagree", "neutral", "agree")), 
        score3 = factor(score3, levels=c("no", "maybe", "yes"))) 
tabulatedLikert2 <- ldply(likert2[-1], function(sc) { 
    as.data.frame(table(age = likert2$age, score = sc)) 
}) 
ggplot(tabulatedLikert2, aes(x=.id, y=Freq, fill=score)) + 
    geom_bar(position="dodge", stat="identity") + 
    geom_text(aes(label=score), position=position_dodge(width=0.9), size=4) + 
    facet_grid(age ~ .) 

enter image description here

Tất nhiên, vào thời điểm này , màu sắc không thực sự thêm bất cứ thứ gì vì mọi thứ đều được dán nhãn trực tiếp trên biểu đồ, vì vậy tôi sẽ loại bỏ hoàn toàn chúng.

ggplot(tabulatedLikert2, aes(x=.id, y=Freq, group=score)) + 
    geom_bar(position="dodge", stat="identity", fill="gray70") + 
    geom_text(aes(label=score), position=position_dodge(width=0.9), size=4) + 
    facet_grid(age ~ .) 

enter image description here

+0

Cám ơn trong câu trả lời sâu - Tôi đã học được rất nhiều ở đó – luciano

+0

Một possiblity: 'ggplot (tabulatedLikert2, aes (x = điểm số, y = Freq)) + geom_bar (stat =" identity ", fill =" gray70 ") + geom_text (aes (nhãn = điểm), size = 4) + facet_grid (tuổi ~ .id, scale =" free_x ")' – bdemarest

Các vấn đề liên quan