Reputation: 61
I'm trying to order each groups in ascending order of the 'first' time point but can't seem to override the alphabetical ordering
ggplot(total, aes(fill=reorder(timepoint, sumofpct), y=sumofpct, x=CountryCode))
+ geom_bar(position="dodge", stat="identity")+scale_fill_manual(values = c("violetred3",'steelblue'))
+ geom_text(aes(label=sumofpct), hjust=1, color='black',size=5, position=position_dodge(width=0.9))
+ xlab('')+ ylab('Survey response (%)')
+ theme_classic()+theme(axis.text = element_text(size=25),axis.title=element_text(size=20),legend.text = element_text(size = 25))+coord_flip()
Is there a way to order this so that it is in ascending order of 'first'?
Data structure
structure(list(CountryCode = c("AUS", "CAN", "DEU", "DNK", "ESP",
"FRA", "ITA", "JPN", "KOR", "NHL", "NOR", "SGP", "SWE", "UK",
"AUS", "CAN", "DEU", "DNK", "ESP", "FRA", "ITA", "JPN", "KOR",
"NHL", "NOR", "SGP", "SWE", "UK"), Date = c("Mar 30 - Apr 05 (2010)",
"Mar 30 - Apr 05 (2010)", "Mar 30 - Apr 05 (2010)", "Apr 06 - Apr 12 (2010)",
"Mar 30 - Apr 05 (2010)", "Mar 30 - Apr 05 (2010)", "Mar 30 - Apr 05 (2010)",
"Mar 30 - Apr 05 (2010)", "Mar 30 - Apr 05 (2010)", "Apr 06 - Apr 12 (2010)",
"Apr 06 - Apr 12 (2010)", "Mar 30 - Apr 05 (2010)", "Mar 30 - Apr 05 (2010)",
"Mar 30 - Apr 05 (2010)", "Mar 22 - Mar 28 (2000)", "Mar 22 - Mar 28 (2000)",
"Apr 05 - Apr 11 (2000)", "Mar 22 - Mar 28 (2000)", "Apr 05 - Apr 11 (2000)",
"Apr 05 - Apr 11 (2000)", "Apr 05 - Apr 11 (2000)", "Mar 29 - Apr 04 (2000)",
"Mar 22 - Mar 28 (2000)", "Feb 08 - Feb 14 (2000)", "Mar 22 - Mar 28 (2000)",
"Mar 22 - Mar 28 (2000)", "Apr 05 - Apr 11 (2000)", "Apr 05 - Apr 11 (2000)"
), sumofpct = c(94, 95, 92, 90, 96, 95, 97, 83, 95, 89, 92, 91,
91, 96, 89, 95, 90, 89, 95, 93, 95, 84, 94, 85, 91, 86, 88, 93
), timepoint = c("first", "first", "first", "first", "first",
"first", "first", "first", "first", "first", "first", "first",
"first", "first", "last", "last", "last", "last", "last", "last",
"last", "last", "last", "last", "last", "last", "last", "last"
)), row.names = c(NA, -28L), groups = structure(list(CountryCode = c("AUS",
"CAN", "DEU", "DNK", "ESP", "FRA", "ITA", "JPN", "KOR", "NHL",
"NOR", "SGP", "SWE", "UK"), .rows = structure(list(c(1L, 15L),
c(2L, 16L), c(3L, 17L), c(4L, 18L), c(5L, 19L), c(6L, 20L
), c(7L, 21L), c(8L, 22L), c(9L, 23L), c(10L, 24L), c(11L,
25L), c(12L, 26L), c(13L, 27L), c(14L, 28L)), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 14L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
Thanks!
Upvotes: 0
Views: 129
Reputation: 17648
you can try
ggplot(df, aes(x=reorder(CountryCode, sumofpct, max), y=sumofpct, fill=factor(timepoint, levels = c("last", "first")))) +
geom_col(position = "dodge") + # recommended over geom_bar
geom_text(aes(label=sumofpct), hjust=1, color='black',size=5, position=position_dodge(width=0.9)) +
scale_fill_manual("timepoint", values = c("violetred3",'steelblue'), guide = guide_legend(reverse = TRUE))+
coord_flip()
Instead of reorder
you can use forcats::fct_reorder2(CountryCode, timepoint=="first", -sumofpct)
Upvotes: 1