简体   繁体   English

将轴刻度线标签添加到堆积的条形图

[英]Adding Axis Tick Labels to Stacked Bar Plot

I have a dataset for the performance of six people. 我有一个用于六个人的表现的数据集。 I wanted to display a "minimum" and "target" performance - in this example, 100 and 140 respectively. 我想显示“最低”和“目标”效果-在此示例中分别为100和140。

require("ggplot2")
stones<-c("Mick","Keith","Brian","Bill","Charlie","Ronnie")
rMat<-data.frame(c(rep(100,6),rep(40,6),20,21,27,46,138,168),
        c(rep(1:6,3)),c(rep(stones,3)))
colnames(rMat)<-c("X1","X2","X3")
colors <- c("white","#F2F2F2","#E5E5FF", "white","#F2F2F2","#CCCCFF", "white","#F2F2F2","#B2B2FF","white","#F2F2F2", "#9999FF", "white","#F2F2F2","#7F7FFF","white","#F2F2F2","#6666FF")
gp<-ggplot(NULL,aes(X2,X1),label=rROC[,1])+theme_bw()
gp<-gp+geom_bar(data=rMat,stat="identity",fill=colors,colour="gray")
gp<-gp+coord_flip()
gp<-gp+ylab("Performance")+xlab("")
gp<-gp+scale_x_discrete(labels=rMat[,3])
gp

I've split the data into three rows per person (for 0 up to minimum, minimum to target, and then the remainder. 我已将数据每人分成三行(从0到最小值,到目标的最小值,然后是其余部分。

There are more than six y-axis tick labels, and as a result, the names don't match up (for example, Ronnie has the highest performance, not Mick). y轴刻度标记超过六个,结果名称不匹配(例如,罗尼(Ronnie)的表现最高,而不是米克(Mick)。

I previously had one row per person (with working labels), like this: 我以前每人一行(带有工作标签),像这样:

perf<-c(160,161,167,186,278,308)
stones<-c("Mick","Keith","Brian","Bill","Charlie","Ronnie")
rMat<-data.frame(perf,c(1:6),stones)

However I wasn't able to split the bars as in the top example. 但是,我无法像上面的示例一样拆分条形图。 Please could anyone suggest how to get the labels and split bars working? 请任何人提出如何使标签和分隔条起作用的建议吗?

Thanks 谢谢

Something like this? 像这样吗

pmin <- 40
pmax <- 100
stones<-c("Mick","Keith","Brian","Bill","Charlie","Ronnie")
p    <- c(20,21,27,46,138,168)
gg   <- data.frame(stones,p)
gg$stones <- factor(gg$stones, level=unique(gg$stones))
gg$status <- ifelse(gg$p<pmin,-1,ifelse(gg$p<=pmax,0,1))
ggp <- ggplot(gg)
ggp <- ggp + geom_bar(aes(x=stones, y=p, fill=factor(status)),stat="identity")
ggp <- ggp + geom_hline(yintercept=pmin, linetype=2, colour="red")
ggp <- ggp + geom_hline(yintercept=pmax, linetype=2, colour="blue")
ggp <- ggp + scale_fill_discrete("Status", labels=c("Below Min","Within Range","Above Max"))
ggp <- ggp + labs(x="",y="Performance")
ggp <- ggp + theme_bw()
ggp <- ggp + coord_flip()
ggp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM