Preliminary Code: Run this code chunk once. Press 1 for ALL when prompted.
install.packages("devtools")
devtools::install_github("hrbrmstr/waffle")
After you run the above code, check your packages tab in the bottom
right window for waffle. It should say you have version
1.0.1. If it says you have a lower version (like 0.7), then you need to
run the code again to make sure you get the correct version! If you know
you ran the code above and it worked, try refreshing your packages
window or restarting your R project to see if the correct version
appears.
The next set up is to prepare the environment for your project including some formatting and any packages you will use.
First, we are going to set up your data. For birth_year
put the year you were born and for birth_month put the
number that corresponds with the month you were born.
last_year and last_month are set to your birth
month minus 1 and your birth year plus 80.
birth_year <- 1987
birth_month <- 5 #May
last_year <- birth_year + 80
last_month <- birth_month - 1
Next, we are going to create the framework for your life in months. All you have to do for the next chunk is run the code in the next chunk - it will generate a data frame that contains all the years and months from your birth until the month and year 80 years after your birth.
life_data <- expand_grid(
month = month.name,
year = birth_year:last_year) %>%
mutate(month = fct_relevel(month, month.name)) %>%
arrange(year, month) %>%
group_by(year) %>%
mutate(month_number = row_number()) %>%
ungroup() %>%
filter(!(year == birth_year & month_number < birth_month)) %>%
filter(!(year == last_year & month_number > last_month))
You will adapt the code below to define your eras and colors for each era. Put the “year,month”, “era name”, “color” - you can use the name of the color or the hex code for the color (use the color picker here to find the hex codes: https://colorpicker.me/).
You do not have to use the eras defined here - you can add, change, or remove lines as needed.
eras <- tribble(
~year_month, ~era, ~fill_color,
"2005,5", "Childhood in TX and CA", "#ad9074", #modify to you
"2011,8", "Moved to Texas", "#f6beab",
#you can add more lines and events as you desire
"2013,9", "Started HS", "#a43712",
"2017,8", "Graduated from PA", "#ba8457",
"2018,8", "Worked for Cypress Mountain", "#127fa4",
"2019,3", "Started CC at San Diego Mesa College", "#abe3f6",
"2021,8", "Transfered to CSUSM", "#477afd",
"2022,9", "Transfered to CSUMB", "#7491ad" #only change color
)
Now run the following code - this will add your eras to your life in months.
era_names <- paste0(eras$era)
life_data_era <- life_data %>%
rowwise() %>%
mutate(year_month = paste0(c(year, month_number),
collapse = ",")) %>%
ungroup() %>%
left_join(eras, by = "year_month") %>%
fill(era, fill_color) %>%
mutate(fill_color = fct_inorder(fill_color),
era = fct_inorder(era))
You can look at the data using the following code:
View(life_data_era)
Now, let’s create the waffle chart! Run the code to create the graph, but change the title name to match your name:
waffle_life<-life_data_era %>%
count(era) %>% ## the count of each era is the number of months in that era
ggplot(aes(fill = era, values = n)) +
waffle::geom_waffle(color = "#F7F7F7", n_rows = 12, size = 1, flip = FALSE, na.rm=FALSE) + ## make each row a year/12 months
scale_fill_manual(name = "", values = eras$fill_color) +
coord_equal() +
theme(legend.text = element_text(size = 24),
legend.position="bottom",
axis.ticks = element_blank(),
plot.background = element_rect(fill = "#F7F7F7",
color = "#F7F7F7")) +
ggtitle("Alexandra Aboukater;s Life in Months") + #Change the Title Name
theme(plot.title = element_text(face = "bold", size = 40))
waffle_life
If you get an error that “geom_waffle” is not found, you may need to
restart you R Session (under Session).
Now we can save the image - Change out your professor’s name with your name. You can adjust the ratio for height/width as needed.
ggsave("AlexandraAboukhater_Life_in_Months.png", plot = waffle_life,
device = "png", width = 25, height = 15, dpi = 300)