Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何使用geom_文本在堆叠条形图中显示值?_R_Ggplot2 - Fatal编程技术网

R 如何使用geom_文本在堆叠条形图中显示值?

R 如何使用geom_文本在堆叠条形图中显示值?,r,ggplot2,R,Ggplot2,我想在堆叠栏中显示百分比数字。然而,有一组的百分比很低。两个值相互重叠。我改为“position='identity'。还是不行……有什么想法吗 x4.can.m <- structure(list(canopy = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("0%", "1 to 84%", "85% +"), class = "factor"), YearQ

我想在堆叠栏中显示百分比数字。然而,有一组的百分比很低。两个值相互重叠。我改为“position='identity'。还是不行……有什么想法吗

x4.can.m <- structure(list(canopy = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("0%", "1 to 84%", 
"85% +"), class = "factor"), YearQuarter = structure(c(1L, 1L, 
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L), .Label = c("2011-09-01", 
"2011-12-01", "2012-03-01", "2012-06-01", "2012-09-01"), class = "factor"), 
    value = c(0.51, 0.01, 0.48, 0.52, 0.01, 0.47, 0.53, 0.01, 
    0.47, 0.57, 0.01, 0.41, 0.61, 0.01, 0.38)), .Names = c("canopy", 
"YearQuarter", "value"), row.names = c(NA, -15L), class = "data.frame")


x4.can.bar <- ggplot(data=x4.can.m, aes(x=factor(YearQuarter), y=value,fill=canopy)) + geom_bar(stat="identity",position = "stack",ymax=100)

x4.can.bar+scale_y_continuous(formatter='percent')+
 labs(y="Percentage",x="Year Quarter") + 
 geom_text(aes(label =paste(round(value*100,0),"%",sep="")),size = 3, hjust = 0.5, vjust = 4,position ="identity")

x4.can.m一个解决方案是将堆栈条更改为道奇条

x4.can.bar <- ggplot(data=x4.can.m, aes(x=factor(YearQuarter), y=value,fill=canopy)) + 
                    geom_bar(stat="identity",position = "dodge",ymax=100) +
             geom_text(aes(label =paste(round(value*100,0),"%",sep=""),ymax=0), 
                       position=position_dodge(width=0.9), vjust=-0.25)
x4.can.bar

x4.can.bar您需要为标签的放置指定合理的值-如果在
ggplot
调用之外执行此操作,将比尝试在调用内执行此操作容易得多

可以通过获取每个叠层构件的中点来执行此操作

使用
plyr
ddply
这是一个简单的方法,即在每个
YearQuarter

library(plyr)
x4.can.m <- ddply(x4.can.m, .(YearQuarter), mutate, csum = cumsum(value)-value/2)

x4.can.bar <- ggplot(data=x4.can.m, aes(x=factor(YearQuarter), y=value,fill=canopy)) +  
 geom_bar(stat="identity",position = "stack",ymax=100)

x4.can.bar + 
 scale_y_continuous(expand = c(0,0), labels = percent) +
 labs(y="Percentage",x="Year Quarter")+
 geom_text(aes(y = csum,label =paste(round(value*100,0),"%",sep="")),
           size = 3, hjust = 1, vjust = 0)
库(plyr)

x4.can.m@mnel,谢谢。升级到ggplot2_0.9.2.1的最简单方法是什么。我仍然在使用R_2.15.1…..在一个新的R会话中运行
install.packages('ggplot2')
melt()函数在R_2.15.0.2中消失了吗?没有。
ggplot2
用于将
restrape2
加载到搜索路径上的较新版本
导入
restrape2
,因此这些函数可用于
ggplot2
。运行
library(重塑2)
以显式加载。