ggplot中的标签从R中的表中删除了条形图
我的代码如下表所示:ggplot中的标签从R中的表中删除了条形图,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,我的代码如下表所示: tt = structure(c(7L, 13L, 24L, 30L, 30L, 38L, 35L, 45L, 37L, 43L, 38L, 59L, 33L, 45L, 37L, 58L), .Dim = c(2L, 8L), .Dimnames = structure(list(param = c("A", "B"), xvar = c("5", "6", "7", "8", "9", "10", "11", "12")), .Names = c("param",
tt = structure(c(7L, 13L, 24L, 30L, 30L, 38L, 35L, 45L, 37L,
43L, 38L, 59L, 33L, 45L, 37L, 58L), .Dim = c(2L, 8L), .Dimnames = structure(list(param = c("A",
"B"), xvar = c("5", "6", "7", "8", "9", "10", "11", "12")), .Names = c("param", "xvar")), class = "table")
tt
xvar
param 5 6 7 8 9 10 11 12
A 7 24 30 35 37 38 33 37
B 13 30 38 45 43 59 45 58
dd = data.frame(tt)
ggplot(dd, aes(xvar, Freq, fill=param))+
geom_bar(stat='identity',position='dodge')+
geom_text(aes(label=Freq), position='dodge' , vjust=2)
但价值观并没有被“回避”而停留在中心。如何将其正确放置在杆上?谢谢您的帮助。使用
position=position\u dodge(…)
而不是position=“dodge”
dd上面的回答很好,但只有在ggplot调用中有fill=param
时,它才会起作用。你似乎没有在你的问题中定义dd
。我认为dd
是as.data.frama(tt)
是的,dd=data.frame(tt)。我想补充一点,谢谢。这正是我需要的。
dd <- as.data.frame(tt)
ggplot(dd, aes(xvar, Freq, fill=param))+
geom_bar(stat='identity',position='dodge')+
geom_text(aes(label=Freq), position=position_dodge(width=.9),vjust=2)