r - ggplot : y-axis (breaks) values from stacked proportional bar graph?

标签 r ggplot2 bar-chart

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

我喜欢用 ggplot 创建堆叠的比例条形图。我的问题是 y 轴的中断,这似乎与每个条形图 block 的百分比值相关,但范围并不像预期的那样从 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))
  }

最佳答案

您可以使用coord_cartesian(ylim=c(0,100))指定限制来执行数据的视觉缩放(这然后保持不变)。您还可以将其添加到 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))

关于r - ggplot : y-axis (breaks) values from stacked proportional bar graph?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14997846/

相关文章:

r - 在具有半透明效果的图形中嵌入字体 (PDF)

r - 如何对面板图进行网格化,使其在 R 中的 ggplot 中具有分类变量和不同的 x 变量

javascript - 条形图顶部中间的折线图点

java - 如何在 JFree BarChart 中添加 PieChart 子图?

android - Achartengine 条形标签...调用系列中的项目?

r - R中的偏回归图

r - 在 ggplot 中,如何为 stat_bin2d 获取两个图例 ("gradient"类型)?

r - 部署包含 R 函数的 API 的最简单方法是什么?

r - 如何为面板中的四个图设置光栅图例?

r - 如何通过将函数应用于 R 中的每个元素,将列联表映射到另一个列联表?