2

问题解决!感谢大家!(这篇文章底部的解决方案)

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

2 回答 2

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))
于 2013-02-21T08:55:08.373 回答
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

在此处输入图像描述

于 2013-02-21T08:47:57.510 回答