ggplot:堆积比例条形图中的y轴(断点)值?

时间:2013-02-21 08:41:53

标签: r ggplot2 bar-chart

问题解决了!感谢大家! (本文底部的解决方案)

我喜欢使用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)))

最后,这是我的结果:

enter image description here

如您所见,y轴中断对应于prc变量的百分比值。

我希望y轴范围从0到100,而每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))
  }

2 个答案:

答案 0 :(得分:3)

您可以使用coord_cartesian(ylim=c(0,100))指定limits来执行数据的视觉缩放(然后保持不变)。您也可以将其添加到scale_y_continuous(limits = c(0, 100), breaks = (seq(0,100,by = 10))),但在比例上设置限制将仅使用这些限制内的数据,因此是原始数据的子集。在您的示例中,它将返回相同的绘图,但它可以显着更改绘图(例如箱图)。

dataset<- textConnection("fg grp  prc
1  g1 85.23
2  g1 14.77
1  g2 73.33
2  g2 26.67
1  g3 85.53
2  g3 14.47
1  g4 87.18
2  g4 12.82
1  g5 72.22
2  g5 27.78")

mydat<- read.table(dataset,header=TRUE) 
mydat$fg <- as.factor(mydat$fg)

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))) + coord_cartesian(ylim=c(0,100))+ scale_y_continuous(breaks=(seq(0,100,by=10))) 
由于评论,

编辑

这不起作用:

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)))+scale_y_continuous(breaks = seq(0, 100, 10))
+coord_cartesian(ylim=c(0,100))

这样做:

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)))+scale_y_continuous(breaks = seq(0, 100, 10)) +
coord_cartesian(ylim=c(0,100))

答案 1 :(得分:2)

您可以使用scale_y_continuous函数(参数breaks)指定中断:

mydat <- as.data.frame(mydat)
mydat$fg <- as.factor(mydat$fg)

library(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))) +
  scale_y_continuous(breaks = seq(0, 100, 10))         # the new command

enter image description here