相关疑难解决方法(0)

在ggplot2中的堆积条形图上显示数据值

我想在ggplot2中的堆积条形图上显示数据值.这是我尝试过的代码

Year      <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4))
Category  <- c(rep(c("A", "B", "C", "D"), times = 4))
Frequency <- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)
Data      <- data.frame(Year, Category, Frequency)
library(ggplot2)
p <- qplot(Year, Frequency, data = Data, geom = "bar", fill = Category,     theme_set(theme_bw()))
p + geom_text(aes(label = Frequency), size = 3, hjust = 0.5, vjust = 3, position =     "stack") 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想在每个部分的中间显示这些数据值.在这方面的任何帮助将受到高度赞赏.谢谢

graphics r ggplot2

97
推荐指数
3
解决办法
15万
查看次数

如何在ggplot2中的geom_bar()上放置标签(没有手动y_position)

我,这是我的问题.

我想用ggplot2创建一个geom_bar,条形图上有标签.

这是一个可重现的例子和生成的图:

prop = c(rep("A",2),rep("B",2),rep("C",2))
n = c(rep(29,2),rep(63,2),rep(25,2))
var = c(rep(c("sortant","entrant"),3))
value = c(10,19,43,20,10,15)
mydata = data.frame(cbind(prop,n,var,value))

> mydata
prop  n     var value
   A 29 sortant    10
   A 29 entrant    19
   B 63 sortant    43
   B 63 entrant    20
   C 25 sortant    10
   C 25 entrant    15

mydata$n = as.integer(as.character(mydata$n))
mydata$value = as.integer(as.character(mydata$value))

ggplot(data = mydata)+aes(x = prop, y = value, fill = var)+geom_bar(stat = 'identity')+
  scale_fill_brewer(palette = 'Paired')+geom_text(aes(label = value))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想制作这个情节,而不是手动创建"y_pos"(如下面的例子),因为我每周都要用不同的数据制作这个情节.

mydata2 = mydata
mydata2$y_pos = c(7,20,20,50,7,17)

>mydata2 …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

1
推荐指数
1
解决办法
582
查看次数

标签 统计

ggplot2 ×2

r ×2

graphics ×1