I have a data frame called complete1
library(tidyverse)
complete1 = tibble(Location = c("usa","de","italy","uk"),Count = c(8,11,2,3))
complete1
resulted to
complete1
# A tibble: 4 × 2
Location Count
<chr> <dbl>
1 usa 8
2 de 11
3 italy 2
4 uk 3
I have now a vector of countries :
levels = c("uk","usa","italy","de")
when I plot the data frame using ggplot2 I receive a plot (that I do not know how it sorts each country based on the x axis)
countries_bar =ggplot(data=complete1, aes(x = Location, y = Count)) +
geom_bar(stat = "identity", fill = "lightgrey") +
coord_flip() +
geom_text(aes(label = Count), hjust = 4.2) +
labs(title = "",
x = "",
y = "Responses from Countries") +
theme_minimal()+
theme(panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
countries_bar
resulting to :
I want the countries in the y axis to be sorted based on order of levels vector.From top to bottom to display the bars of “uk”,”usa”,”italy”,”de”.How can I do that in R ?