问题解决!感谢大家!(这篇文章底部的解决方案)
我喜欢用 ggplot 创建一个堆叠的比例条形图。我的问题是 y 轴的中断,这似乎与每个条形图块的百分比值有关,但不像预期的那样介于 0 到 100 之间。
这是我的数据框:
fg grp prc
1 1 g1 85.23
2 2 g1 14.77
3 1 g2 73.33
4 2 g2 26.67
5 1 g3 85.53
6 2 g3 14.47
7 1 g4 87.18
8 2 g4 12.82
9 1 g5 72.22
10 2 g5 27.78
这就是我调用绘图函数的方式:
require(ggplot2)
ggplot(mydat, aes(x=grp, y=prc, fill=fg)) +
geom_bar(stat="identity", colour="black", show_guide=FALSE) +
scale_fill_manual(values=c("#235a80", "#80acc8")) +
labs(title=NULL, x="Cluster-Gruppen", y=NULL) +
theme(axis.line = element_line(colour="gray"),
axis.text = element_text(size=rel(1.3)),
axis.title = element_text(face="italic", size=rel(1.4)))
最后,这是我的结果:
如您所见,y 轴中断对应于 prc 变量的百分比值。
我想有一个从 0 到 100 的 y 轴范围,每隔 10 个位置(seq(0,100,by=10)
)中断一次。我需要以任何方式准备我的数据吗?我如何设法“修复” y 轴?
提前致谢
这就是我计算数据和工作解决方案的方式!
clusterDiskriminanz <- function(myData, groups, gcnt) {
disc <- lda(groups ~ ., data=myData, na.action="na.omit", CV=TRUE)
ct <- table(groups, disc$class)
dg <- diag(prop.table(ct, 1))
# print barplot for correct percentage for each category of groups
newdat <- NULL
tmpdat <- NULL
filldat <- NULL
perc <- round(100*dg,2)
percrest <- round(100-perc,2)
# looks strange, but for testing purposes
# I add data this way. Perhaps I also lack
# a bit of functions which may do this better and faster
for (i in 1:gcnt) {
newdat <- rbind(newdat, c(paste("g",i,sep="")))
newdat <- rbind(newdat, c(paste("g",i,sep="")))
tmpdat <- rbind(tmpdat, perc[i])
tmpdat <- rbind(tmpdat, percrest[i])
filldat <- rbind(filldat, "1")
filldat <- rbind(filldat, "2")
}
# create data frame! prc-values are treated as numeric
# now! need to convert $g to factors though!
mydat <- data.frame(filldat, newdat, tmpdat)
names(mydat) <- c("fg", "grp", "prc")
mydat$fg <- factor(mydat$fg)
# ggplot-stuff comes here...
require(ggplot2)
ggplot(mydat, aes(x=grp, y=prc, fill=fg)) +
geom_bar(stat="identity", colour="black", show_guide=FALSE) +
scale_fill_manual(values=c("#235a80", "#80acc8")) +
labs(title=NULL, x="Cluster-Gruppen", y=NULL) +
geom_hline(yintercept=totalcorrect, linetype=2, colour="white", alpha=0.8) +
# Achsenbeschriftung etwas größer machen
theme(axis.line = element_line(colour="gray"),
axis.text = element_text(size=rel(1.3)),
axis.title = element_text(face="italic", size=rel(1.4))) +
scale_y_continuous(breaks = seq(0, 100, 10)) +
coord_cartesian(ylim=c(0,100))
}