-1

I have a dataset like:

region country urban_rural treatment      potential
----------------------------------------------------
Africa  Kenya     1         chlorine       compatible
Europe  England   2         non_clorine    not compatible
Africa  Kenya     1         other          potential

And I want to make a figure like :

enter image description here

can anyone help with this?

Ronak Shah
  • 355,584
  • 18
  • 123
  • 178
Yiru Chen
  • 1
  • 1
  • What have you tried so far? Can you share your code? – Axeman Mar 09 '21 at 01:08
  • In order for us to help you, please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). For example, to produce a minimal data set, you can use `head()`, `subset()`, or the indices. Then use `dput()` to give us something that can be put in R immediately. Also, please make sure you know what to do [when someone answers your question](https://stackoverflow.com/help/someone-answers). More info can be found at Stack Overflow's [help center](https://stackoverflow.com/help). Thank you! – iamericfletcher Mar 09 '21 at 01:54

1 Answers1

1

I hope I understood your question/problem correctly. I made up some more lines of dummy data to get a nicer plots. The graphs can (and probably should) be further polished.

library(dplyr)
library(ggplot2)
library(data.table) # for the dummy data read in from plain text

# reading in some dummy data I made up buy copy and pasting plus minor changes
df <- data.table::fread("region,country,urban_rural,treatment,potential
Africa,Kenya,1,chlorine,compatible
Europe,England,2,non_clorine,not compatible
Africa,Kenya,1,other,potential
Africa,Kenya,1,chlorine,compatible
Africa,Kenya,2,non_clorine,not compatible
Europe,England,1,other,potential
Europe,England,1,other,potential
Europe,England,1,non_clorine,compatible
Africa,Kenya,2,non_clorine,not compatible
Africa,Kenya,1,non_clorine,potential
Africa,Kenya,1,other,potential
Europe,England,1,chlorine,compatible
Africa,Kenya,2,non_clorine,not compatible
Europe,England,1,other,potential
Africa,Kenya,1,other,potential
Africa,Kenya,1,non_clorine,compatible") 


# one plot for Kenya with title and modified X axis label
df %>% 
  # build the counts to display in graph
  dplyr::count(region, country, treatment, potential) %>% 
  # filter Kenya 
  dplyr::filter(country == 'Kenya') %>% 
  # plot
  ggplot2::ggplot(aes(x = treatment, y = n, fill = potential )) +
  ggplot2::geom_bar(stat="identity") +
  # visual enhancement
  ggplot2::ggtitle("Africa") +
  ggplot2::xlab("Kenya")

enter image description here

# fast way to plot data for all countries
df %>% 
  # build the counts to display in graph
  dplyr::count(region, country, treatment, potential) %>%  
  # plot
  ggplot2::ggplot(aes(x = treatment, y = n, fill = potential )) +
  ggplot2::geom_bar(stat="identity") +
  # build the subplots from country variable
  ggplot2::facet_wrap(~country)

enter image description here

DPH
  • 3,362
  • 1
  • 6
  • 16
  • Sorry I didn't make it clear. But I want the x-axis to be multiple countries, and the treatment to be dodge position. I have edited the figure I want. – Yiru Chen Mar 09 '21 at 01:56
  • @YiruChen you could use my second example and just include a line to filter for region equals to Africa. dplyr::filter(region == 'Africa') %>% is the line you have to put before the first ggplot2 call (where now is written #plot) – DPH Mar 09 '21 at 02:03