R图形:为聚簇/躲避条形图添加标签

时间:2014-04-25 13:26:39

标签: r plot bar-chart

这是一个基本的绘图问题:

我需要在聚簇/躲避条形图中添加标签。我已经使用text()查看了几个示例,但似乎无法正确定位标签。

teachers <- c("A", "B","C", "D", "E")
mean_pre_scores <- c(10, 11, 12, 10,9)
mean_post_scores <- c(12,15,17,13,12)
pre_post <- data.frame(mean_pre_scores, mean_post_scores)

pre_post <- as.matrix(pre_post)
barplot((t(pre_post)), beside = T, names = teachers, legend = c("pre", "post"), 
        ylim = c(0,20), args.legend = list(x="bottomright"),  axes = T, main = "Unit 1 Test",
        col=c(26,51))

我想修改此图,以便将值显示在条形图上方。了解如何在条形图中显示值也很有帮助。

4 个答案:

答案 0 :(得分:3)

我认为这就是你所追求的:

z <- barplot((t(pre_post)), beside = T, names = teachers, 
    legend = c("pre", "post"), ylim = c(0,20), 
    args.legend = list(x="topright"),  axes = T, 
    main = "Unit 1 Content Pre Test", col=c(26,51))

text(cex=1, x=c(z[1, ], z[2, ]), y=c(pre_post) + par("cxy")[2]/2, c(pre_post), xpd=TRUE) 

enter image description here

要在栏内移动文字,只需使用减法,如下所示:

text(cex=1, x=c(z[1, ], z[2, ]), y=c(pre_post) - par("cxy")[2]/2, c(pre_post), xpd=TRUE) 

答案 1 :(得分:1)

当你在&#34;上面说&#34;时,你的意思是直接在它们之上吗?还是在顶轴上?

这是一个以顶部为榜样的例子。

现在很明显,您想要条形图上的数值而不是顶部轴上的字母。无论如何,我会把它留在这里,并在下面发布第二个结果。

> barplot((t(pre_post)), beside = T, legend = c("pre", "post"), ylim = c(0,20),
          args.legend = list(x="bottomright"),  axes = T, col=c(26,51))
> axis(3, at = c(2, 5, 8, 11, 14), labels = teachers)
> title(main = "Unit 1 Content Pre Test", line = 3)
> box()

enter image description here

这里有正确的结果。我把传说移到右上角,它不会覆盖任何酒吧。这种方式更容易阅读。

> barplot((t(pre_post)), beside = TRUE, legend = c("pre", "post"), 
          main = "Unit 1 Content Pre Test", ylim = c(0,20), 
          args.legend = list(x="topright"), col=c(26,51))
> text(x = c(1.5,2.5,4.5,5.5,7.5,8.5,10.5,11.5,13.5,14.5), 
       y = txt.height+1, labels = as.character(txt.height))
> box()

enter image description here

答案 2 :(得分:1)

ggplot2解决方案:

# get the data into the right format
pre_post <- data.frame(teachers,mean_pre_scores, mean_post_scores)
library(reshape2)
prepost <- melt(pre_post, id = "teachers")

# create the plot
ggplot(prepost, aes(x = teachers, y = value, fill = variable)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(aes(label = value), vjust = -0.5, position = position_dodge(0.9)) +
  theme_bw()

结果:

enter image description here

答案 3 :(得分:0)

改编自这篇文章(R graphics: Add labels to stacked bar chart

对于内部标签:

teachers <- c("A", "B","C", "D", "E")
mean_pre_scores <- c(10, 11, 12, 10,9)
mean_post_scores <- c(12,15,17,13,12)
pre_post.df <- data.frame(mean_pre_scores, mean_post_scores)

pre_post <- as.matrix(pre_post.df)
b<-barplot((t(pre_post)), beside = T, names = teachers, legend = c("pre", "post"), 
     ylim = c(0,20), args.legend = list(x="bottomright"),  axes = T, main = "Unit 1 Content Pre Test",
     col=c(26,51))


ypos.inside<-apply(pre_post, 2, function(x) x -1 )
ypos.inside <- t(ypos.inside)

text(b, ypos.inside, pre_post)

enter image description here

对于外部标签:

teachers <- c("A", "B","C", "D", "E")
mean_pre_scores <- c(10, 11, 12, 10,9)
mean_post_scores <- c(12,15,17,13,12)
pre_post.df <- data.frame(mean_pre_scores, mean_post_scores)

pre_post <- as.matrix(pre_post.df)
b<-barplot((t(pre_post)), beside = T, names = teachers, legend = c("pre", "post"), 
     ylim = c(0,20), args.legend = list(x="bottomright"),  axes = T, main = "Unit 1 Content Pre Test",
     col=c(26,51))

ypos.outside<-apply(pre_post, 2, function(x) x +1 )
ypos.outside <- t(ypos.outside)
text(b, ypos.outside, pre_post)

enter image description here