So I have got a data frame with football player's names, nationalities and stats from a football game. I wanted to find best 10 players in each country, sum their "Special" stat, select top 10 countries with the highest sum and then plot it.
fifka3 <- fifka %>% group_by(Nationality) %>%
top_n(n = 10, wt=Special) %>% summarize(Top10 = sum(Special)) %>% top_n(10)
When I plot it with:
ggplot(data=fifka3, aes(x=fct_infreq(Nationality),y=Top10)) +
geom_bar(stat="identity") +
mytheme_1() ##just my theme function to save time
the function fct_infreq()
doesn't change the order of the factors on the plot and I have no clue why. Is it because I created the df "fifka3" from "fifka" using group_by()
and the df "fifka3" still contains other factors like presented below? And what can I do to change the order within the ggplot()
function?
str(fifka3)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 10 obs. of 2 variables:
$ Nationality: Factor w/ 165 levels "Afghanistan",..: 3 13 19 35 54 59 78 122 127 139
$ Top10 : int 23883 21409 23788 23008 21691 21581 21530 21595 22696 21483`