This is the pair coding activity related to 4 Data viz I.
Task 1: Open the R project for the lab
Task 3: Load in the library and read in the data
The data should already be in your project folder. If you want a fresh copy, you can download the data again here: data_pair_coding.
We are using the package tidyverse
today, and the datafile we should read in is dog_data_clean_wide.csv
.
Task 4: Create an appropriate plot
Pick any single or two categorical variables from the Binfet dataset and choose one of the appropriate plot choices. Things to think about:
Converting some variables into factors
dog_data_wide <- dog_data_wide %>%
mutate(Year_of_Study = factor(Year_of_Study,
levels = c("First", "Second", "Third", "Fourth", "Fifth or above")))
Now we can plot
ggplot(dog_data_wide, aes(x = Year_of_Study, fill = Year_of_Study)) +
geom_bar() +
scale_fill_brewer(
palette = "Dark2",
guide = "none") +
scale_x_discrete(name = "Year of Study") +
scale_y_continuous(name = "Count",
expand = expansion(mult = c(0, 0.05))) +
theme_classic()
Converting some variables into factors
dog_data_wide <- dog_data_wide %>%
mutate(GroupAssignment = factor(GroupAssignment,
levels = c("Direct", "Indirect", "Control")))
Now we can plot
ggplot(dog_data_wide, aes(x = GroupAssignment , fill = Live_Pets)) +
geom_bar(position = "fill") +
labs(x = "Experimental Group", y = "Count", fill = "Pets at Home") +
scale_fill_manual(values = c('deeppink', 'springgreen2'), na.value = 'orangered',
labels = c("Yes", "No")) +
scale_y_continuous(expand = expansion(mult = c(0, 0.05))) +
theme_classic() +
theme(legend.position = "bottom")