我有以下数据集,我想将其绘制为堆积条形图,每种情况下都将'%'列作为数据标签,但我无法使我的数据标签正确匹配。
Club Winners Runners-Up %
RM 7 3 70%
Mil 5 3 63%
我使用以下代码创建条形图:
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()
有没有办法可以使用rperc矢量索引匹配标签?
转换后的dfc并收集:
Club Type % Value
RM Winner 70 7
Mil Runner-Up 63 5
RM Winner 70 3
Mil Runner-Up 63 3
编辑:
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")
答案 0 :(得分:0)
这是一个例子
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")