1

I have the following data set that I want to plot as a stacked bar chart with the '%' column as a data label in each case but I cannot get my data labels to match up correctly.

Club Winners Runners-Up  %
RM      7        3      70%
Mil     5        3      63%

I am using the following code to create the bar chart:

dfc <- df[1:20,] %>%
transform(.,Club = reorder(Club, Winners))%>%
gather(., "Type", "value", c(2,3))

rperc <- paste0(as.character(format(round(dfc$Percentage[1:20],0),nsmall=0)), "%") %>% rev()

levels(dfc$Type)[levels(dfc$Type)=="Winners"] <- "Winners"
levels(dfc$Type)[levels(dfc$Type)=="Runner_Up"] <- "Runner_Up"

ggp <- ggplot(data=dfc, aes(x=reorder(Club,value), y=value,alpha=Type)) + 
geom_bar(stat="identity", position="stack") +
scale_alpha_manual(values=c(1, .3)) +
theme(axis.text.x  = element_text(size=14),
axis.text.y  = element_text(hjust=1, size=12)) +
ylab("CL Trophies (and Runner Up)") +
xlab("Club") + 
ggtitle("UEFA Champions League Record") +
annotate("text", x=1:20, y=0.5, label=rperc, color="white", angle=0) +
coord_flip() 

Is there a way I could index match the labels using the rperc vector?

Edit: enter image description here

dfc after transform and gather:

Club Type      %    Value
 RM  Winner    70     7
 Mil Runner-Up 63     5
 RM  Winner    70     3
 Mil Runner-Up 63     3

Edit:

dfc <- 
structure(list(Club = structure(c(20L, 19L, 17L, 16L, 18L, 15L, 
    13L, 14L, 10L, 9L, 11L, 12L, 3L, 6L, 8L, 7L, 2L, 4L, 5L, 1L, 
20L, 19L, 17L, 16L, 18L, 15L, 13L, 14L, 10L, 9L, 11L, 12L, 3L, 
6L, 8L, 7L, 2L, 4L, 5L, 1L), .Label = c("Aston Villa", "Borussia Dortmund", 
"Celtic", "Chelsea", "Feyenoord", "Hamburg", "Marseille", "Steaua Bucure<U+0219>ti", 
"Benfica", "Juventus", "Nottingham Forest", "Porto", "Internazionale", 
"Manchester United", "Ajax", "Barcelona", "Bayern Munich", "Liverpool", 
"Milan", "Real Madrid"), class = "factor", scores = structure(c(4, 
1, 5, 5, 2, 1, 1, 1, 1, 1, 3, 2, 5, 3, 1, 7, 2, 2, 10, 1), .Dim = 20L, .Dimnames = list(
    c("Ajax", "Aston Villa", "Barcelona", "Bayern Munich", "Benfica", 
    "Borussia Dortmund", "Celtic", "Chelsea", "Feyenoord", "Hamburg", 
    "Internazionale", "Juventus", "Liverpool", "Manchester United", 
    "Marseille", "Milan", "Nottingham Forest", "Porto", "Real Madrid", 
    "Steaua Bucure<U+0219>ti")))), Years.won = c("1956, 1957, 1958, 1959, 1960, 1966, 1998, 2000, 2002, 2014", 
"1963, 1969, 1989, 1990, 1994, 2003, 2007", "1974, 1975, 1976, 2001, 2013", 
"1992, 2006, 2009, 2011, 2015", "1977, 1978, 1981, 1984, 2005", 
"1971, 1972, 1973, 1995", "1964, 1965, 2010", "1968, 1999, 2008", 
"1985, 1996", "1961, 1962", "1979, 1980", "1987, 2004", "1967", 
"1983", "1986", "1993", "1997", "2012", "1970", "1982", "1956, 1957, 1958, 1959, 1960, 1966, 1998, 2000, 2002, 2014", 
"1963, 1969, 1989, 1990, 1994, 2003, 2007", "1974, 1975, 1976, 2001, 2013", 
"1992, 2006, 2009, 2011, 2015", "1977, 1978, 1981, 1984, 2005", 
"1971, 1972, 1973, 1995", "1964, 1965, 2010", "1968, 1999, 2008", 
"1985, 1996", "1961, 1962", "1979, 1980", "1987, 2004", "1967", 
"1983", "1986", "1993", "1997", "2012", "1970", "1982"), Years.runner.up = c("1962, 1964, 1981", 
"1958, 1993, 1995, 2005", "1982, 1987, 1999, 2010, 2012", "1961, 1986, 1994", 
"1985, 2007", "1969, 1996", "1967, 1972", "2009, 2011", "1973, 1983, 1997, 1998, 2003, 2015", 
"1963, 1965, 1968, 1988, 1990", "&\n—", "&\n—", "1970", "1980", 
"1989", "1991", "2013", "2008", "&\n—", "&\n—", "1962, 1964, 1981", 
"1958, 1993, 1995, 2005", "1982, 1987, 1999, 2010, 2012", "1961, 1986, 1994", 
"1985, 2007", "1969, 1996", "1967, 1972", "2009, 2011", "1973, 1983, 1997, 1998, 2003, 2015", 
"1963, 1965, 1968, 1988, 1990", "&\n—", "&\n—", "1970", "1980", 
"1989", "1991", "2013", "2008", "&\n—", "&\n—"), Percentage = c(76.9230769230769, 
63.6363636363636, 50, 62.5, 71.4285714285714, 66.6666666666667, 
60, 60, 25, 28.5714285714286, 100, 100, 50, 50, 50, 50, 50, 50, 
100, 100, 76.9230769230769, 63.6363636363636, 50, 62.5, 71.4285714285714, 
66.6666666666667, 60, 60, 25, 28.5714285714286, 100, 100, 50, 
50, 50, 50, 50, 50, 100, 100), Type = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("Winners", "Runner_Up"), class = "factor"), 
    value = c(10L, 7L, 5L, 5L, 5L, 4L, 3L, 3L, 2L, 2L, 2L, 2L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 4L, 5L, 3L, 2L, 2L, 2L, 
    2L, 6L, 5L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L)), row.names = c(NA, 
-40L), .Names = c("Club", "Years.won", "Years.runner.up", "Percentage", 
"Type", "value"), class = "data.frame")
Thierry
  • 18,049
  • 5
  • 48
  • 66
Marcus
  • 95
  • 9
  • 5
    it would be a lot easier if you added dput(dfc) and a picture of your current output – Thierry Sep 22 '15 at 08:48
  • Hi Thierry, link to image added and dfc added – Marcus Sep 22 '15 at 10:08
  • 1
    Hi Marcus. please add the output of `dput(dfc)` because then it is easy for use to copy-paste that into an R session. – Thierry Sep 22 '15 at 11:32
  • Is there a reason you're not using geom_text? And it really is easier for others to help you if you make your example [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Heroka Sep 22 '15 at 12:18
  • @Thierry I have added the dput(dfc) output – Marcus Sep 22 '15 at 12:32

1 Answers1

0
  1. precalculate the desired order instead of doing it in the ggplot call
  2. store the labels inside the dataframe
  3. use geom_text() instead of annotate

Here is an example

library(ggplot2)
library(dplyr)
dfc <- dfc %>% mutate(
  Club = reorder(Club, value),
  Percentage = sprintf("%.0f%%", Percentage)
)
ggplot(data = dfc, aes(x = Club)) + 
  geom_bar(
      aes(y = value, alpha = Type), 
      stat="identity", 
      position="stack"
) + 
  geom_text(colour = "white", y = 0, aes(label = Percentage)) + 
  coord_flip() +
  scale_alpha_manual(values=c(1, .3)) +
  theme(axis.text.x  = element_text(size=14),
  axis.text.y  = element_text(hjust=1, size=12)) +
  ylab("CL Trophies (and Runner Up)") +
  xlab("Club") + 
  ggtitle("UEFA Champions League Record")

enter image description here

Thierry
  • 18,049
  • 5
  • 48
  • 66