3

I would like to have labels on only the top part of my stacked bar plot.

Here is my data frame:

#create data frame 
building <- c("Burj \nKhalifa", "Zifeng \nTower", "Bank of \nAmerica Tower", 
              "Burj Al Arab", "Emirates \nTower One", "New York \nTimes Tower",
              "Emirates \nTower Two", "Rose Rayhaan \nby Rotana", "The \nPinnacle", 
              "Minsheng \nBank Building")
occupiable<- c(585, 317, 235, 198, 241, 220, 213, 237, 265, 237)
nonoccupiable <- c(244, 133, 131, 124, 113, 99, 97, 96, 95, 94)
df.build <- data.frame(building, occupiable, nonoccupiable)

#melt data frame for stack bar plot
df.build2 <- melt(df.build, id.vars="building")

And my stacked bar plot:

#comparision true and percived values
ggplot(df.build2, aes(x=reorder(building, -value), y=value, fill=variable)) +
  geom_bar(stat="identity") +
  xlab("") +
  ylab("") +
 #geom_text(aes(label = c("29%" "30%", "36%", "39%", "32%", "31%", "31%", "29%", "29%", "28%")), size = 3, hjust = 0.5, vjust = 3, position = "stack") +
  theme(legend.position="top") +
  ggtitle("Porównanie wartości prawdziwych i odczuwalnych") 

enter image description here

I would like to have labels like this in geom_text() in my code for plotting (height of blue bar/height of whole bar). It should be placed in the blue area. How can I do it?

Jaap
  • 81,064
  • 34
  • 182
  • 193
jjankowiak
  • 3,010
  • 6
  • 28
  • 45
  • Take a look at [this](http://stackoverflow.com/questions/13576139/how-to-display-value-in-a-stacked-bar-chart-by-using-geom-text) question, the top-scored answer looks very similar to your desired solution. – tonytonov Apr 21 '14 at 10:49
  • Ok, but I want to have only a half of labels, (only for blue bars). So if I define `percentage <- nonoccupiable/(nonoccupiable+occupiable)` and add to ggplot line `geom_text(aes(y=percentage, label = paste(round(percentage*100,0),"%",sep="")), size = 3, hjust = 0.5, vjust = 3)` I get error: `Aesthetics must either be length one, or the same length as the dataProblems:percentage`. – jjankowiak Apr 21 '14 at 10:58

2 Answers2

5

There are 2 problems, you are missing a comma betweeen the first two elements of your label vector; and, your label vector is too short. Even though you have what looks like 10 bars, you infact have 20 (since they are stacked). To get around this precede your labels by 10 blank character strings:

geom_text(
 aes(label = c(rep("",10),
 "29%", "30%", "36%", "39%", "32%", "31%", "31%", "29%", "29%", "28%")),
size = 3, hjust = 0.5, vjust = 3, position = "stack")

Which gives:

stacked barchart

James
  • 65,548
  • 14
  • 155
  • 193
4

Another solution (I also changed the angle of the x-axis text):

# creating percentage variables
df.build$occ.perc <- round(df.build$occupiable / (df.build$occupiable + df.build$nonoccupiable) * 100)
df.build$nonocc.perc <- round(df.build$nonoccupiable / (df.build$occupiable + df.build$nonoccupiable) * 100)

# melt data frame for stack bar plot![enter image description here][1]
df.build2 <- cbind(
  melt(df.build, id = c("building"), measure = c(2:3)),
  melt(df.build, id = c("building"), measure = c(4:5), value.name = "perc")
)
df.build2 <- df.build2[,-c(4,5)]
df.build2$perc <- ifelse(df.build2$variable=="occupiable", df.build2$perc==NA, df.build2$perc)

# creating the plot
ggplot(df.build2, aes(x=reorder(building, -value), y=value, fill=variable)) +
  geom_bar(stat="identity") +
  xlab("") +
  ylab("") +
  geom_text(aes(label = perc), size = 3, hjust = 0.5, vjust = 2, position = "stack") +
  theme(legend.position="top", axis.text.x = element_text(angle = 45, vjust=0.5)) +
  ggtitle("Porównanie wartości prawdziwych i odczuwalnych")

the result: enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193