0

I want to create a plot that shows the relationship between countries (categorical), their government type (4 categories, including NA), and the proportion of covid deaths to population. I want to show the 30 countries with the highest death proportion and if there is a relationship with the government type.

Right now the countries are plotted in alphabetical order, but I would like to plot the death proportion in descending order. I can't seem to figure out how to do this. Thanks!

library(tidyverse)
library(lubridate)
library(readr)

Governmental System, Country, Proportion of Deaths to Population

covid_data <- read_csv(here::here("data/covid_data.csv"))
covid_data <- covid_data %>% 
  mutate(death_proportion = total_deaths / population)
covid_data[with(covid_data, order(-death_proportion)), ] %>% 
  head(30) %>% 
  ggplot(aes(x = death_proportion, 
             y = country,
             color = government)) +
  geom_point()

geom_point() plot

  • The "best" way to show data isn't really a specific programming problem. If you have general questions about data visualization, you should instead ask at [stats.se] where it's more likely to be [on topic](https://stats.stackexchange.com/help/on-topic). Or maybe [datascience.se]. Otherwise edit your question to make it clear what programming question you have. – MrFlick Mar 15 '21 at 20:38
  • Thank you for the response. I was asking for a way to plot the data in descending order of death proportion. I will edit my question! – Russell Chow Mar 15 '21 at 21:00
  • This might help: https://stackoverflow.com/questions/29850782/ordering-faceted-dotplot You'll need to set the order of the factors to be what you want. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data that can be used to test and verify possible solutions. – MrFlick Mar 15 '21 at 21:02
  • I will take a look. Thanks! – Russell Chow Mar 15 '21 at 21:11

1 Answers1

0

I think you just need to use forcats::fct_reorder to set the order of you countries by the plotting variable.

Check this example:

library(tidyverse)

mtcars %>% 
  rownames_to_column(var = "car_name") %>% 
  mutate(car_name = fct_reorder(car_name, desc(mpg))) %>% 
  ggplot(aes(x = mpg, 
             y = car_name,
             color = factor(cyl))) +
  geom_point()

Created on 2021-03-16 by the reprex package (v1.0.0)

Dan Adams
  • 4,971
  • 9
  • 28