Skip to content

Instantly share code, notes, and snippets.

@rishisidhu
Created April 25, 2020 06:17
Show Gist options
  • Save rishisidhu/ed1fd0564fb88af436cdc1a4bff1b1a5 to your computer and use it in GitHub Desktop.
Save rishisidhu/ed1fd0564fb88af436cdc1a4bff1b1a5 to your computer and use it in GitHub Desktop.
An example of how to cluster and stack bars in a bar plot by different variables
dummy_data_1 <- tribble(
~Country, ~"Time", ~"Score", ~"Gender",
"INDIA", "AM", 20, "male",
"INDIA", "PM", 20, "female",
"INDIA", "AM", 50, "male",
"INDIA", "PM", 40, "male",
"INDIA", "AM", 20, "female",
"USA", "AM", 30, "male",
"USA", "PM", 30, "male",
"USA", "PM", 30, "female",
"USA", "AM", 35, "female",
"Canada", "PM", 10, "female",
"Canada", "PM", 10, "female",
"Canada", "AM", 20, "female",
"Canada", "AM", 30, "male",
)
p <- ggplot(dummy_data_1, aes(x = Time, y = Score, fill = Gender)) +
geom_bar(position = "stack", stat = "identity") +
facet_wrap( ~ Country)
print(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment