目的
创建堆积区域图或堆叠"圆形图(见图)。不需要饼图。
条形图的数据和代码
#Data set:
Numbers 16%
Frosts 2%
Doors 6%
Shelfs 10%
Earning -3%
par(mai=c(2, 1, 1, 1), lwd=2)
barplot(as.numeric(c(16, 2, 6, 10, -3)), col = c("lightblue"), main="Bar plot",
names.arg=c("Numbers","Frosts","Earning", "Doors","Shelfs"), xpd=TRUE, las=2, lwd=2,
axes=FALSE, axis.lty=1, cex.axis=1, cex.names=1, cex.main=1, ylim=c(-4, 18), xlim=c(0, 5))
两个输出选项
答案 0 :(得分:2)
这应该可以让你大部分时间
library(ggplot2)
df<- data.frame(value=as.numeric(c(16, 2, 6, 10, -3)),
cat=c("Numbers","Frosts","Earning","Doors","Shelfs"))
ggplot(df[order(df$value),], aes(x=1, y=abs(value), fill=factor(ifelse(value>0, 0, 1)))) +
geom_bar(stat="identity", colour="grey") +
geom_text(aes(label=paste(cat, value)), position = "stack", vjust = 3) +
scale_fill_manual(values=c("white", "red"))
ggplot(df[order(df$value),], aes(x=1, y=abs(value), fill=factor(ifelse(value>0, 0, 1)))) +
geom_bar(stat="identity", colour="grey") +
geom_text(aes(label=paste(cat, value)), position = "stack", vjust = -1) +
scale_fill_manual(values=c("white", "red")) +
coord_polar()
你可能需要摆弄vjust值来改变标签的位置,或者为它们计算自定义y映射,但这是一个好的开始。
答案 1 :(得分:1)
右侧“相关”链接的topmost应该为您提供构建堆积条形图所需的大部分信息,但是根据您的需要进行调整,它将是这样的:
# A vertical matrix containing the values
md <- matrix(c(-3, 16, 2, 6, 10), ncol=1)
d <- barplot(md, col=c(2, rep(0, 4)))
# Finding the vertical position for the labels
ypos <- apply(md, 2, cumsum)
ypos <- ypos - md/2
ypos <- t(ypos)
# I haven't checked if the values and names match
text(d/3, ypos, adj=c(0, NA),
paste(c("Earning","Numbers","Frosts","Doors","Shelfs"), md, sep=": "))
答案 2 :(得分:1)