2
votes

Problem solves! Thanks to all of you! (solution at bottom of this post)

I like to create a stacked, proportional bar graph with ggplot. My problem are the breaks of the y-axis, which seem to relate to the percentage value of each bar tile, but does not range from 0 to 100 as expected.

Here's my data frame:

   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

This is how I call the plot function:

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)))

And finally, this is my result:

enter image description here

As you can see, the y-axis-breaks correspond to the percentage values of the prc-variable.

I would like to have an y-axis-range from 0 to 100 instead, with breaks at every 10th position (seq(0,100,by=10)). Do I need to prepare my data in any way? How do I manage to "fix" the y-axis?

Thanks in advance

This is the way how I calculate the data and the working solution!

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
I suppose that variable prc is treated as factor (only this way I can get the the y scale the same as yours). Try str(mydat) to see type of all columns. I would suggest to delete from your function line newdat<-cbind(filldat,newdat,tmpdat) and use mydat<-data.frame(filldat,newdat,tmpdat)Didzis Elferts
Thanks a lot, now I got it! The str-command helped showing me the missing information. Indeed, $prc was treated as factor, and simply using mydat<-data.frame(filldat,newdat,tmpdat) and converting $fg to factors worked.Daniel

2 Answers

3
votes

You can use coord_cartesian(ylim=c(0,100)) to specify the limits to perform a visual zoom of the data (which is then left unchanged). You could also add it to scale_y_continuous(limits = c(0, 100), breaks = (seq(0,100,by = 10))), but setting the limits on a scale will only use the data that's inside these limits, so a subset of the original data. In your example it will return the same plot, but it can change a plot (for example a boxplot) substantially.

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))) 

EDIT due to a comment:

This doesn't work:

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))

This does:

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))
2
votes

You can specify the breaks with the scale_y_continuous function (parameter 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