在ggplot中带有geom_text的标签栏图 [英] Label bar plot with geom_text in ggplot
问题描述
我已经用以下代码创建了这个图:
library(ggplot2);库(reshape2); library(plyr)
likert< - data.frame(age = c(rep(young,5),rep(middle,5),rep(old,5) ),
score1 = c(rep(不太可能,1),rep(可能,1),rep(很可能,13)),
score2 = c(rep(不同意,6),代表(中立,4),代表(同意,5)),
分数3 = c(代表(否,5),代表(可能,7) ,rep(是,3)))
已熔化-Likert < - melt(dlply(likert,。(age),function(x)llply(x,table)))
名称(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变量,计数,group =分数,label = meltedLikert $分数),size = 4)+
facet_grid(age〜。)
我该怎么做l位置文本,因此每个得分
的标签位于每个条的顶部的变量
顶部的相应栏上?
根据链接问题,将 position = position_dodge(width = 0.9)
添加到 geom_text
p>
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〜。)
不过,我也想指出其他一些薄弱点GS。您不应在 aes()
调用中使用 meltedLikert $ score
;您应该只参考数据框中以 data
传递的内容。另外, meltedLikert
已经是 data.frame
,所以调用 data.frame()
就没有必要(尽管没有任何伤害)。
真正的改进在于如何创建制表符。请考虑以下内容:
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〜。)
您可以通过在原始数据中修复它们来修复这些条的排序:
likert2 < - mutate(likert,
score1 = factor(score1,levels = c(impossible,likely,非常可能)),
得分2 =因子(得分2,水平= c(不同意,中立,同意)),
得分3 =因子(得分3,水平= c ,也许,是)))
tabulatedLikert2 < - ldply(likert2 [-1],function(sc){
as.dat a.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(年龄〜。)
当然,在这一点上,颜色实际上并没有添加任何东西,因为所有东西都直接贴在图表,所以我只是完全摆脱它们。
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〜。)
< img src =https://i.stack.imgur.com/q3qkk.pngalt =在这里输入图片描述>
I've created this plot with 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 ~ .)
How can I label position text so each label of score
sits over the corresponding bar for variable
top of each bar?
As per the answer in the linked question, adding position = position_dodge(width=0.9)
to the geom_text
call lines up 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 ~ .)
However, I also wanted to point out a few other things. You should no use meltedLikert$score
in the aes()
call; you should only refer to things in the data frame that is passed as data
. Also, meltedLikert
is already a data.frame
, so calling data.frame()
on it is not necessary (though doesn't do any harm).
The real improvement is in how you create your tabulation to begin with. Consider this instead:
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 ~ .)
You can fix the ordering of the bars by fixing them in the original 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 ~ .)
Of course, at this point, the colors don't actually add anything since everything is labeled directly on the graph, so I'd just get rid of them entirely.
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 ~ .)
这篇关于在ggplot中带有geom_text的标签栏图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!