绘制列明智的数据ggplot

时间:2017-10-11 16:19:42

标签: r ggplot2

假设:

x <- data.frame(Day = c(1,2,3,4,5,6,7,8,9,10),
                var1 = c(5,4,2,3,4,5,1,2,3,4),
                var2 = c(3,6,2,3,4,5,7,8,1,2),
                var3 = c(1,2,3,4,6,2,4,7,8,4),
                var4 = c(1,3,7,5,3,7,2,3,1,2))

我希望创建一个条形图,在X轴上显示第1天到第10天,变量在Y上显示。

以下是评论中提供的解决方案:

x$avg <- rowMeans(x[2:5]) # your question asked for an average

require(ggplot2)

ggplot(data = x, aes(x = Day, y = avg)) + 
  geom_bar(stat="identity") + # use identity to map the bar to actual value
  stat_summary(aes(label=round(..y..,2)), # labels the bar with the value
               fun.y=mean, geom="text", size=6,
               vjust = -0.5)

1 个答案:

答案 0 :(得分:0)

这是一种方法:

x$avg <- rowMeans(x[2:5]) # your question asked for an average

require(ggplot2)

ggplot(data = x, aes(x = Day, y = avg)) + 
  geom_bar(stat="identity") + # use identity to map the bar to actual value
  stat_summary(aes(label=round(..y..,2)), # labels the bar with the value
               fun.y=mean, geom="text", size=6,
               vjust = -0.5)