I have tried everything to sort my bars in geom(bar) in ggplot by spending. This is my code: I have tried manually ordering the data, forcats, changing as an aes in ggplot and can't get it to work.
#Load any packages
library(ggplot2)
library(dplyr)
library(readr)
library(janitor)
library(RColorBrewer)
library(forcats)
#Create data frame
MarketSize <- data.frame(Country = c("United States", "Japan", "Germany", "France", "Italy", "United Kingdom", "Spain", "Canada", "South Korea",
"Australia", "China", "Brazil", "Russia", "India", "Other"),
Spending_2020 = c(528.7, 88.2, 54.9, 36.3, 33.3, 30.2, 25.7, 22.8, 16.2, 11.8, 134.4, 28.7, 17.5, 21.5, 15)
)
# Arrange by Spending outside ggplot
MarketSize$Country <- fct_rev(fct_infreq(MarketSize$Country))
#Create plot, labels etc.
MarketSize_2020 <- ggplot(MarketSize, aes(x = Country, y = Spending_2020)) +
geom_col(fill="steelblue", width=0.75) +
geom_text(aes(label=Spending_2020), vjust=0.5, hjust=-0.01, size=3.5) +
coord_flip() +
labs(title = "Market Spending on pharmaceuticals, 2020",
x = "Country or region",
y = "Total spending on pharmaceuticals (US $BN)") +
theme_classic()
#View plot
MarketSize_2020
Can anyone help? Thanks in advance
I have tried ordering using forcats package and cannot get it to work!