Created
February 20, 2016 08:26
-
-
Save jrnold/44cf78427e3e4f483b17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("dplyr") | |
library("tidyr") | |
library("ggplot2") | |
# Create data frame with columns: country, variable, value | |
long_df_by_country <- | |
df %>% | |
select(country, var1, var2, var3) %>% | |
gather(variable, value, - country) | |
# For each country and variable, run a t-test | |
# do() is used to run an arbitrary command for each group | |
# the broom function tidy() converts the results of t.test to a data frame | |
# With %>% you can use . to refer to the result of the previous command | |
# This creates a data frame with columns: country, variable, and various columns with results from t.tests | |
t_test_results <- | |
long_df_by_country %>% | |
group_by(country, variable) %>% | |
do(tidy(t.test(.$variable))) | |
# To plot the countries and variables in the same plot | |
ggplot(t_test_results, aes(x = country, y = estimate, ymin = conf.low, ymax = conf.high, color = variable)) + | |
geom_pointrange(position = "dodge") | |
# To plot with countries and variables facetted | |
ggplot(t_test_results, aes(x = country, y = estimate, ymin = conf.low, ymax = conf.high)) + | |
geom_pointrange(position = "dodge") + | |
facet_wrap(~ variable) | |
# Both of these plots would be better if country was sorted by the average of one of these variables | |
# using reorder() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment