Tag label graph with geom_text in ggplot

I created this graph with the following code:

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

How can I tag position text so that each score label score above the corresponding row for the variable top of each row?

+6
source share
1 answer

According to the answer in a related question, adding position = position_dodge(width=0.9) to the geom_text string calls the values:

 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

However, I also wanted to point out a few other things. You should not use meltedLikert$score in aes() call; you should only refer to things in the data frame that are passed as data . In addition, meltedLikert already has data.frame , so calling data.frame() on it is not needed (although it does not hurt).

The real improvement is how you start creating your tabs. Instead, consider this:

 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

You can correct the order of the bars by correcting them in the source data:

 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

Of course, at the moment, colors actually do not add anything, since everything is marked directly on the chart, so I’ll just get rid of them completely.

 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

+10
source

Source: https://habr.com/ru/post/952770/


All Articles