我正在尝试根据与图表上绘制的值不同的值在我的情节中订购啤酒品牌。以下是我到目前为止的情况:
ggplot(rn, aes(Beer, Move)) +
geom_bar(stat = "identity", aes(fill = Beer)) +
geom_text(aes(label = paste0(ifelse(Move >= 0, "+", "-"), round(Move, 0)), hjust = ifelse(Move >= 0, -0.1, 1.1)), size = 3) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
coord_flip() +
facet_grid( ~ Week)
我想通过两个Vol
类型的总Week
的降序值来订购下图中y轴上的标签。
rn <- structure(list(Beer = c("TOTAL\nMANUFACTURER", "TOTAL\nMANUFACTURER",
"BUD ICE", "BUD ICE", "BUD LIGHT", "BUD LIGHT", "BUD LIGHT LIME",
"BUD LIGHT LIME", "BUD LIGHT\nPLATINUM", "BUD LIGHT\nPLATINUM",
"BUDWEISER", "BUDWEISER", "BUDWEISER\nSELECT", "BUDWEISER\nSELECT",
"BUSCH", "BUSCH", "BUSCH LIGHT", "BUSCH LIGHT", "BUSCH\nSIGNATURE\nCOPPER LAGER",
"BUSCH\nSIGNATURE\nCOPPER LAGER", "KING COBRA", "KING COBRA",
"MICHELOB ULTRA", "MICHELOB ULTRA", "NATTY DADDY", "NATTY DADDY",
"NATURAL ICE", "NATURAL ICE", "NATURAL LIGHT", "NATURAL LIGHT",
"ROLLING ROCK", "ROLLING ROCK", "STELLA ARTOIS", "STELLA ARTOIS"
), Move = c(10.9988965633466, 10.1917719480268, 18.7942890269773,
17.8379704000208, 8.52818879847615, 6.57322359471904, -1.6643480768426,
-12.1827006261446, -27.2318264955458, -25.2438712289667, 1.9069242423403,
0.638652351445872, 35.3911234177604, 32.8505336536518, 21.185972764471,
22.6051124119408, 38.9175541069643, 38.3366719548672, 27.7826940408763,
12.5811399710158, 7.61387785160614, 5.3730476632517, 2.523089374317,
6.65243800531876, 15.6173328636012, 9.99422424529812, 15.8994933396845,
16.417584385266, -3.80149571073229, -8.0531186783228, 5.89476792019757,
9.26717373953234, 2.99948475741683, -9.23164967143357), Vol = c(1648111.1,
5206063.1, 97212.8, 299075.2, 488052.9, 1603105.5, 35031.9, 108055.8,
24700.5, 76511.2, 218627.9, 686473.6, 7531, 24379.5, 168583.9,
521768.3, 283476.3, 886896.1, 4086.9, 12142.7, 18267.6, 55111.6,
28543.5, 90719, 24303.8, 62871.5, 58335.5, 176122.4, 176848.2,
557538.9, 10981.1, 33924.5, 3527.3, 11367.3), Week = c("3 Week Aggregate",
"12 Week Aggregate", "3 Week Aggregate", "12 Week Aggregate",
"3 Week Aggregate", "12 Week Aggregate", "3 Week Aggregate",
"12 Week Aggregate", "3 Week Aggregate", "12 Week Aggregate",
"3 Week Aggregate", "12 Week Aggregate", "3 Week Aggregate",
"12 Week Aggregate", "3 Week Aggregate", "12 Week Aggregate",
"3 Week Aggregate", "12 Week Aggregate", "3 Week Aggregate",
"12 Week Aggregate", "3 Week Aggregate", "12 Week Aggregate",
"3 Week Aggregate", "12 Week Aggregate", "3 Week Aggregate",
"12 Week Aggregate", "3 Week Aggregate", "12 Week Aggregate",
"3 Week Aggregate", "12 Week Aggregate", "3 Week Aggregate",
"12 Week Aggregate", "3 Week Aggregate", "12 Week Aggregate")), .Names = c("Beer",
"Move", "Vol", "Week"), class = c("data.table", "data.frame"), row.names = c(NA,
-34L))
答案 0 :(得分:0)
rn <- structure(list(Beer = c("TOTAL\nMANUFACTURER", "TOTAL\nMANUFACTURER",
"BUD ICE", "BUD ICE", "BUD LIGHT", "BUD LIGHT", "BUD LIGHT LIME",
"BUD LIGHT LIME", "BUD LIGHT\nPLATINUM", "BUD LIGHT\nPLATINUM",
"BUDWEISER", "BUDWEISER", "BUDWEISER\nSELECT", "BUDWEISER\nSELECT",
"BUSCH", "BUSCH", "BUSCH LIGHT", "BUSCH LIGHT", "BUSCH\nSIGNATURE\nCOPPER LAGER",
"BUSCH\nSIGNATURE\nCOPPER LAGER", "KING COBRA", "KING COBRA",
"MICHELOB ULTRA", "MICHELOB ULTRA", "NATTY DADDY", "NATTY DADDY",
"NATURAL ICE", "NATURAL ICE", "NATURAL LIGHT", "NATURAL LIGHT",
"ROLLING ROCK", "ROLLING ROCK", "STELLA ARTOIS", "STELLA ARTOIS"),
Move = c(10.9988965633466, 10.1917719480268, 18.7942890269773,
17.8379704000208, 8.52818879847615, 6.57322359471904, -1.6643480768426,
-12.1827006261446, -27.2318264955458, -25.2438712289667, 1.9069242423403,
0.638652351445872, 35.3911234177604, 32.8505336536518, 21.185972764471,
22.6051124119408, 38.9175541069643, 38.3366719548672, 27.7826940408763,
12.5811399710158, 7.61387785160614, 5.3730476632517, 2.523089374317,
6.65243800531876, 15.6173328636012, 9.99422424529812, 15.8994933396845,
16.417584385266, -3.80149571073229, -8.0531186783228, 5.89476792019757,
9.26717373953234, 2.99948475741683, -9.23164967143357),
Vol = c(1648111.1,5206063.1, 97212.8, 299075.2, 488052.9, 1603105.5, 35031.9, 108055.8,
24700.5, 76511.2, 218627.9, 686473.6, 7531, 24379.5, 168583.9,
521768.3, 283476.3, 886896.1, 4086.9, 12142.7, 18267.6, 55111.6,
28543.5, 90719, 24303.8, 62871.5, 58335.5, 176122.4, 176848.2,
557538.9, 10981.1, 33924.5, 3527.3, 11367.3), Week = c("3 Week Aggregate",
"12 Week Aggregate", "3 Week Aggregate", "12 Week Aggregate",
"3 Week Aggregate", "12 Week Aggregate", "3 Week Aggregate",
"12 Week Aggregate", "3 Week Aggregate", "12 Week Aggregate",
"3 Week Aggregate", "12 Week Aggregate", "3 Week Aggregate",
"12 Week Aggregate", "3 Week Aggregate", "12 Week Aggregate",
"3 Week Aggregate", "12 Week Aggregate", "3 Week Aggregate",
"12 Week Aggregate", "3 Week Aggregate", "12 Week Aggregate",
"3 Week Aggregate", "12 Week Aggregate", "3 Week Aggregate",
"12 Week Aggregate", "3 Week Aggregate", "12 Week Aggregate",
"3 Week Aggregate", "12 Week Aggregate", "3 Week Aggregate",
"12 Week Aggregate", "3 Week Aggregate", "12 Week Aggregate")), .Names = c("Beer",
"Move", "Vol", "Week"), class = "data.frame", row.names = c(NA,
-34L))
我唯一改变的就是这一行
## add this and use make.unique to avoid duplicate factor label warnigs
## everything else stays the same
rn$Beer <- with(rn, factor(Beer, levels = make.unique(Beer[order(ave(Vol, Beer, FUN = sum))])))
library('ggplot2')
ggplot(rn, aes(Beer, Move)) +
geom_bar(stat = "identity", aes(fill = Beer)) +
geom_text(aes(label = paste0(ifelse(Move >= 0, "+", "-"), round(Move, 0)), hjust = ifelse(Move >= 0, -0.1, 1.1)), size = 3) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
coord_flip() +
facet_grid( ~ Week)