Renaming multiple variables and Plotting categorical variable (coded as 0 and 1)

Good morning from the Philippines.

I am working on a survey data and encountered a couple of concerns.

  1. I need to rename several variables. I did it using names() command. Is there a “shorter” way especially on multiple variables?

  2. I must plot the number of facilities per level and per province. There are three levels (1,2,3) and coded as 0 (No) and 1 (Yes). I converted the levels into numeric and yet I’m not getting the right results.

AFHF_raw ← import(here(“data”, “Encoding sheet for JPARAP Monitoring.xlsx”)) %>%
clean_names() %>%
filter(x1!=“Province”) %>%
select (-x4)

#list of facilities
unique(AFHF_raw$Facility)

#Renaming multiple variables
names(AFHF_raw)[3] = “Operations”
names(AFHF_raw)[4] = “Type_Facility”
names(AFHF_raw)[5] = “Others_Facility”
names(AFHF_raw)[6] = “Hosp_Function”
names(AFHF_raw)[7] = “Trained_ARH”
names(AFHF_raw)[8] = “Foundational course”
names(AFHF_raw)[9] = “AJA”
names(AFHF_raw)[10] = “ADEPT”
names(AFHF_raw)[11] = “HYO”
names(AFHF_raw)[12] = “Others”
names(AFHF_raw)[13] = “AFHF Certified”
names(AFHF_raw)[14] = “Level1”
names(AFHF_raw)[15] = “Level2”
names(AFHF_raw)[16] = “Level3”
names(AFHF_raw)[18] = “DemandGen Activities”
names(AFHF_raw)[19] = “H_Q4(2023)_1014-M”
names(AFHF_raw)[20] = “H_Q1(2024)_1014-M”
names(AFHF_raw)[21] = “H_Q4(2023)_1014-F”
names(AFHF_raw)[22] = “H_Q1(2024)_1014-F”
names(AFHF_raw)[23] = “H_Q4(2023)-Q1(2024)_1014”
names(AFHF_raw)[24] = “H_Q4(2023)-1519_M”
names(AFHF_raw)[25] = “H_Q1(2024)-1519_M”
names(AFHF_raw)[26] = “H_Q4(2023)-1519_F”
names(AFHF_raw)[27] = “H_Q1(2024)-1519_F”
names(AFHF_raw)[28] = “H_Q4(2023)-Q1(2024)_1519”
names(AFHF_raw)[29] = “HM_Q4(2023)_1014-M”
names(AFHF_raw)[30] = “HM_Q1(2024)_1014-M”
names(AFHF_raw)[31] = “HM_Q4(2023)_1014-F”
names(AFHF_raw)[32] = “HM_Q1(2024)_1014-F”
names(AFHF_raw)[33] = “HM_Q4(2023)-Q1(2024)_1014”
names(AFHF_raw)[34] = “HM_Q4(2023)-1519_M”
names(AFHF_raw)[35] = “HM_Q1(2024)-1519_M”
names(AFHF_raw)[36] = “HM_Q4(2023)-1519_F”
names(AFHF_raw)[37] = “HM_Q1(2024)-1519_F”
names(AFHF_raw)[39] = “W/Recordingtool”
names(AFHF_raw)[40] = “Recordingtool”
names(AFHF_raw)[41] = “PAS_1st”
names(AFHF_raw)[42] = “PAS_2ndinitiatives”
names(AFHF_raw)[43] = “PAS_Techguidanceleaders”
names(AFHF_raw)[44] = “PAS_policies”
names(AFHF_raw)[45] = “PAS_perfaudit”
names(AFHF_raw)[46] = “PAS_2ndinitiatives”

#missing values
AFHF_raw[is.na(AFHF_raw)] ← 0

AFHF_clean ← AFHF_raw %>%
mutate_at(c(“H_Q4(2023)_1014-M”,
“H_Q1(2024)_1014-M”,
“H_Q4(2023)_1014-F”,
“H_Q1(2024)_1014-F”,
“H_Q4(2023)-Q1(2024)_1014”,
“H_Q4(2023)-1519_M”,
“H_Q1(2024)-1519_M”,
“H_Q4(2023)-1519_F”,
“H_Q1(2024)-1519_F”,
“H_Q4(2023)-Q1(2024)_1519”,
“HM_Q4(2023)_1014-M”,
“HM_Q1(2024)_1014-M”,
“HM_Q4(2023)_1014-F”,
“HM_Q1(2024)_1014-F”,
“HM_Q4(2023)-Q1(2024)_1014”,
“HM_Q4(2023)-1519_M”,
“HM_Q1(2024)-1519_M”,
“HM_Q4(2023)-1519_F”,
“HM_Q1(2024)-1519_F”,
“HM_Q4(2023)-Q1(2024)_1519”,
“AFHF Certified”,
“Level1”,
“Level2”,
“Level3”),
as.numeric)

#create new column as total of those counselled on HEEADDSSS
AFHF_clean ← AFHF_clean %>%
mutate(HEEADDSSS_counselled = H_Q4(2023)-Q1(2024)_1014 +
H_Q4(2023)-Q1(2024)_1519)

#create new column as total of those counselled AND managed on HEEADDSSS
AFHF_clean ← AFHF_clean %>%
mutate(HEEADDSSS_counselledNDmanaged = HM_Q4(2023)-Q1(2024)_1014 +
HM_Q4(2023)-Q1(2024)_1519)

#AFHF Level
AFHF_long ← AFHF_clean %>%
adorn_totals(“col”, select = c(Level1,Level2,Level3)) %>%
pivot_longer(
cols = Level1:Level3,
names_to = “Level”,
values_to = “No”) %>%
mutate(Level=ifelse(Level== “Level1”, “1”, Level)) %>%
mutate(Level=ifelse(Level== “Level2”, “2”, Level)) %>%
mutate(Level=ifelse(Level== “Level3”, “3”, Level)) %>%
filter(No==“1”) #there are no Level2 and Level3 facilities for now

#Number of facilities per level and per province
ggplot(AFHF_long, aes(x = Level, y = No, fill = Province)) +
geom_bar(aes(x = Level, y = No, fill = Province), stat = “identity”, position = “dodge”) +
facet_wrap(~Province, scales = “free_y”, nrow = 4) +
labs(title = “Number and Level Facilities Visited, Samar and Southern Leyte”,
x = “Level”,
y = “Number of Facilities”) +
scale_y_continuous() +
theme_bw()

*Below is my data:

dput(head(AFHF_clean))
structure(list(Province = c(“Samar”, “Samar”, “Southern Leyte”,
“Southern Leyte”, “Southern Leyte”, “Southern Leyte”), Facility = c(“Samar Provincial Hospital”,
“Catbalogan City Teen Center”, “Maasin City Health Unit I”, “Maasin City Health Unit II”,
“Maasin City Health Unit III”, “Macrohon RHU”), Operations = c(“Mon-Fri, 8-5”,
“Adolescent (Wed-Thu, 1-4pm)\r\nFamily Planning (Mon-Tues-Fri)”,
“Mon-Fri, 8-5”, “Mon-Fri, 8-5”, “Tue & Wed 8-5pm”, “Mon-Fri, 8-5”
), Type_Facility = c(“3”, “2”, “2”, “2”, “2”, “1”), Others_Facility = c(“0”,
“0”, “0”, “0”, “0”, “0”), Hosp_Function = c(“1”, “0”, “0”, “0”,
“0”, “0”), Trained_ARH = c(“1”, “1”, “1”, “1”, “1”, “1”), Foundational course = c(“1”,
“1”, “1”, “1”, “1”, “1”), AJA = c(“1”, “0”, “1”, “0”, “0”, “0”
), ADEPT = c(“1”, “1”, “1”, “1”, “1”, “1”), HYO = c(“1”, “0”,
“0”, “1”, “1”, “1”), Others = c(“Adolescent MHPSS\r\nUsapang BIBA\r\nFPCBT1 and FPCBT2\r\nPPIUD”,
“FPCBT1”, “FPCBT1, FPCBT2”, “FPCBT1, FPCBT2”, “FPCBT1, FPCBT2”,
“FPCBT 1, FPCBT 2, IIUD, MHPSS, Usapang BIBA”), AFHF Certified = c(1,
1, 1, 1, 1, 1), Level1 = c(1, 1, 1, 1, 1, 1), Level2 = c(0, 0,
0, 0, 0, 0), Level3 = c(0, 0, 0, 0, 0, 0), Regular_DemandGen = c(“1”,
“1”, “0”, “1”, “1”, “1”), DemandGen Activities = c(“Buntis Congress and Teenspired (both annually)”,
“Information Booth/Campaign (at least once a month)”, “0”, “HIV and CSE at School and Barangay (as requested by the school or SK)”,
“Healthy Young Ones (Health Education)”, “Youth Development Session (monthly or weekly)”
), H_Q4(2023)_1014-M = c(4, 6, 5, 12, 18, 0), H_Q1(2024)_1014-M = c(28,
2, 5, 18, 3, 1), H_Q4(2023)_1014-F = c(15, 2, 4, 11, 10, 2),
H_Q1(2024)_1014-F = c(52, 9, 6, 21, 6, 0), H_Q4(2023)-Q1(2024)_1014 = c(99,
19, 20, 62, 37, 3), H_Q4(2023)-1519_M = c(24, 1, 3, 10,
5, 0), H_Q1(2024)-1519_M = c(75, 2, 7, 20, 5, 2), H_Q4(2023)-1519_F = c(87,
17, 8, 12, 5, 4), H_Q1(2024)-1519_F = c(172, 15, 7, 17,
1, 1), H_Q4(2023)-Q1(2024)_1519 = c(358, 35, 25, 59, 16,
7), HM_Q4(2023)_1014-M = c(4, 3, 5, 12, 18, 0), HM_Q1(2024)_1014-M = c(28,
2, 5, 18, 3, 1), HM_Q4(2023)_1014-F = c(13, 1, 4, 11, 10,
2), HM_Q1(2024)_1014-F = c(52, 9, 6, 21, 6, 0), HM_Q4(2023)-Q1(2024)_1014 = c(97,
15, 20, 62, 37, 3), HM_Q4(2023)-1519_M = c(24, 1, 3, 10,
5, 0), HM_Q1(2024)-1519_M = c(75, 2, 7, 20, 5, 2), HM_Q4(2023)-1519_F = c(28,
15, 8, 12, 5, 4), HM_Q1(2024)-1519_F = c(172, 15, 7, 17,
1, 1), HM_Q4(2023)-Q1(2024)_1519 = c(299, 33, 25, 59, 16,
7), W/Recordingtool = c(“1”, “1”, “1”, “1”, “1”, “1”),
Recordingtool = c(“HEADDSS\r\nAHDP Monthly Accomplishment Report”,
“Adolescent Services Logbook”, “Adolescent Friendly Health Logbook”,
“Adolescent Friendly Health Logbook”, “Adolescent Friendly Health Logbook”,
“Adolescent Friendly Health Logbook”), PAS_1st = c(“0”, “1”,
“1”, “1”, “1”, “1”), PAS_2ndinitiatives = c(“0”, “1”, “1”,
“1”, “1”, “1”), PAS_Techguidanceleaders = c(“0”, “0”, “0”,
“0”, “0”, “0”), PAS_policies = c(“0”, “1”, “1”, “1”, “1”,
“1”), PAS_perfaudit = c(“0”, “0”, “0”, “0”, “0”, “0”), part_4_general_findings_and_recommendations_encode_the_statements_either_in_verbatim_or_summarize_the_points = c(“Process algorithm”,
“FP Caravan (counselling and service provision)\r\nReferral facility (15 BHS for assessment by Apr 2024)”,
“Patient Flow; Directory of referal”, “CHU has good partnership with schools and barangay within the community”,
“Health Education conducted at community”, “Adolescent Service Flow Chart; FP is offered to adolescent regardles pregnant or sexually active, support in demand generation of CSE”
), x48 = c(“Additional staff for PPIUD Training”, “Facility is under construction”,
“Lack of manpower”, “Retired doctor”, “Lack of manpower”,
“None”), x49 = c(“Detection and management of suicidal cases”,
“Process flow on consultation/accessing services”, “Consistency of recording in logbook; EO for update”,
“EO for update”, “Consistency of recording in logbook; EO for update”,
“They should only have one adoslecent logbook for general consultation, abused or sexually violated”
), HEEADDSSS_counselled = c(457, 54, 45, 121, 53, 10), HEEADDSSS_counselledNDmanaged = c(396,
48, 45, 121, 53, 10)), row.names = c(NA, 6L), class = “data.frame”)

I hope you can continue to guide me.
Thank you very much.

Regards,
Echo

Hi Echo,

I would start by using the rename() function from the dplyr package to rename your variables in a single step. Alternatively, you could try to use a data dictionary or data labels to do this, but those would be more complex approaches.

Here is an example using the rename() function:

# loading packages
library(tidyverse)

# renaming variables
iris |>
    rename(
        sepal_length = Sepal.Length,
        sepal_width = Sepal.Width,
        petal_length = Petal.Length,
        petal_width = Petal.Width,
        species = Species
    )
#>     sepal_length sepal_width petal_length petal_width    species
#> 1            5.1         3.5          1.4         0.2     setosa
#> 2            4.9         3.0          1.4         0.2     setosa
#> 3            4.7         3.2          1.3         0.2     setosa
#> 4            4.6         3.1          1.5         0.2     setosa
#> 5            5.0         3.6          1.4         0.2     setosa
#> 6            5.4         3.9          1.7         0.4     setosa
#> 7            4.6         3.4          1.4         0.3     setosa
#> 8            5.0         3.4          1.5         0.2     setosa
#> 9            4.4         2.9          1.4         0.2     setosa
#> 10           4.9         3.1          1.5         0.1     setosa
#> 11           5.4         3.7          1.5         0.2     setosa
#> 12           4.8         3.4          1.6         0.2     setosa
#> 13           4.8         3.0          1.4         0.1     setosa
#> 14           4.3         3.0          1.1         0.1     setosa
#> 15           5.8         4.0          1.2         0.2     setosa
#> 16           5.7         4.4          1.5         0.4     setosa
#> 17           5.4         3.9          1.3         0.4     setosa
#> 18           5.1         3.5          1.4         0.3     setosa
#> 19           5.7         3.8          1.7         0.3     setosa
#> 20           5.1         3.8          1.5         0.3     setosa
#> 21           5.4         3.4          1.7         0.2     setosa
#> 22           5.1         3.7          1.5         0.4     setosa
#> 23           4.6         3.6          1.0         0.2     setosa
#> 24           5.1         3.3          1.7         0.5     setosa
#> 25           4.8         3.4          1.9         0.2     setosa
#> 26           5.0         3.0          1.6         0.2     setosa
#> 27           5.0         3.4          1.6         0.4     setosa
#> 28           5.2         3.5          1.5         0.2     setosa
#> 29           5.2         3.4          1.4         0.2     setosa
#> 30           4.7         3.2          1.6         0.2     setosa
#> 31           4.8         3.1          1.6         0.2     setosa
#> 32           5.4         3.4          1.5         0.4     setosa
#> 33           5.2         4.1          1.5         0.1     setosa
#> 34           5.5         4.2          1.4         0.2     setosa
#> 35           4.9         3.1          1.5         0.2     setosa
#> 36           5.0         3.2          1.2         0.2     setosa
#> 37           5.5         3.5          1.3         0.2     setosa
#> 38           4.9         3.6          1.4         0.1     setosa
#> 39           4.4         3.0          1.3         0.2     setosa
#> 40           5.1         3.4          1.5         0.2     setosa
#> 41           5.0         3.5          1.3         0.3     setosa
#> 42           4.5         2.3          1.3         0.3     setosa
#> 43           4.4         3.2          1.3         0.2     setosa
#> 44           5.0         3.5          1.6         0.6     setosa
#> 45           5.1         3.8          1.9         0.4     setosa
#> 46           4.8         3.0          1.4         0.3     setosa
#> 47           5.1         3.8          1.6         0.2     setosa
#> 48           4.6         3.2          1.4         0.2     setosa
#> 49           5.3         3.7          1.5         0.2     setosa
#> 50           5.0         3.3          1.4         0.2     setosa
#> 51           7.0         3.2          4.7         1.4 versicolor
#> 52           6.4         3.2          4.5         1.5 versicolor
#> 53           6.9         3.1          4.9         1.5 versicolor
#> 54           5.5         2.3          4.0         1.3 versicolor
#> 55           6.5         2.8          4.6         1.5 versicolor
#> 56           5.7         2.8          4.5         1.3 versicolor
#> 57           6.3         3.3          4.7         1.6 versicolor
#> 58           4.9         2.4          3.3         1.0 versicolor
#> 59           6.6         2.9          4.6         1.3 versicolor
#> 60           5.2         2.7          3.9         1.4 versicolor
#> 61           5.0         2.0          3.5         1.0 versicolor
#> 62           5.9         3.0          4.2         1.5 versicolor
#> 63           6.0         2.2          4.0         1.0 versicolor
#> 64           6.1         2.9          4.7         1.4 versicolor
#> 65           5.6         2.9          3.6         1.3 versicolor
#> 66           6.7         3.1          4.4         1.4 versicolor
#> 67           5.6         3.0          4.5         1.5 versicolor
#> 68           5.8         2.7          4.1         1.0 versicolor
#> 69           6.2         2.2          4.5         1.5 versicolor
#> 70           5.6         2.5          3.9         1.1 versicolor
#> 71           5.9         3.2          4.8         1.8 versicolor
#> 72           6.1         2.8          4.0         1.3 versicolor
#> 73           6.3         2.5          4.9         1.5 versicolor
#> 74           6.1         2.8          4.7         1.2 versicolor
#> 75           6.4         2.9          4.3         1.3 versicolor
#> 76           6.6         3.0          4.4         1.4 versicolor
#> 77           6.8         2.8          4.8         1.4 versicolor
#> 78           6.7         3.0          5.0         1.7 versicolor
#> 79           6.0         2.9          4.5         1.5 versicolor
#> 80           5.7         2.6          3.5         1.0 versicolor
#> 81           5.5         2.4          3.8         1.1 versicolor
#> 82           5.5         2.4          3.7         1.0 versicolor
#> 83           5.8         2.7          3.9         1.2 versicolor
#> 84           6.0         2.7          5.1         1.6 versicolor
#> 85           5.4         3.0          4.5         1.5 versicolor
#> 86           6.0         3.4          4.5         1.6 versicolor
#> 87           6.7         3.1          4.7         1.5 versicolor
#> 88           6.3         2.3          4.4         1.3 versicolor
#> 89           5.6         3.0          4.1         1.3 versicolor
#> 90           5.5         2.5          4.0         1.3 versicolor
#> 91           5.5         2.6          4.4         1.2 versicolor
#> 92           6.1         3.0          4.6         1.4 versicolor
#> 93           5.8         2.6          4.0         1.2 versicolor
#> 94           5.0         2.3          3.3         1.0 versicolor
#> 95           5.6         2.7          4.2         1.3 versicolor
#> 96           5.7         3.0          4.2         1.2 versicolor
#> 97           5.7         2.9          4.2         1.3 versicolor
#> 98           6.2         2.9          4.3         1.3 versicolor
#> 99           5.1         2.5          3.0         1.1 versicolor
#> 100          5.7         2.8          4.1         1.3 versicolor
#> 101          6.3         3.3          6.0         2.5  virginica
#> 102          5.8         2.7          5.1         1.9  virginica
#> 103          7.1         3.0          5.9         2.1  virginica
#> 104          6.3         2.9          5.6         1.8  virginica
#> 105          6.5         3.0          5.8         2.2  virginica
#> 106          7.6         3.0          6.6         2.1  virginica
#> 107          4.9         2.5          4.5         1.7  virginica
#> 108          7.3         2.9          6.3         1.8  virginica
#> 109          6.7         2.5          5.8         1.8  virginica
#> 110          7.2         3.6          6.1         2.5  virginica
#> 111          6.5         3.2          5.1         2.0  virginica
#> 112          6.4         2.7          5.3         1.9  virginica
#> 113          6.8         3.0          5.5         2.1  virginica
#> 114          5.7         2.5          5.0         2.0  virginica
#> 115          5.8         2.8          5.1         2.4  virginica
#> 116          6.4         3.2          5.3         2.3  virginica
#> 117          6.5         3.0          5.5         1.8  virginica
#> 118          7.7         3.8          6.7         2.2  virginica
#> 119          7.7         2.6          6.9         2.3  virginica
#> 120          6.0         2.2          5.0         1.5  virginica
#> 121          6.9         3.2          5.7         2.3  virginica
#> 122          5.6         2.8          4.9         2.0  virginica
#> 123          7.7         2.8          6.7         2.0  virginica
#> 124          6.3         2.7          4.9         1.8  virginica
#> 125          6.7         3.3          5.7         2.1  virginica
#> 126          7.2         3.2          6.0         1.8  virginica
#> 127          6.2         2.8          4.8         1.8  virginica
#> 128          6.1         3.0          4.9         1.8  virginica
#> 129          6.4         2.8          5.6         2.1  virginica
#> 130          7.2         3.0          5.8         1.6  virginica
#> 131          7.4         2.8          6.1         1.9  virginica
#> 132          7.9         3.8          6.4         2.0  virginica
#> 133          6.4         2.8          5.6         2.2  virginica
#> 134          6.3         2.8          5.1         1.5  virginica
#> 135          6.1         2.6          5.6         1.4  virginica
#> 136          7.7         3.0          6.1         2.3  virginica
#> 137          6.3         3.4          5.6         2.4  virginica
#> 138          6.4         3.1          5.5         1.8  virginica
#> 139          6.0         3.0          4.8         1.8  virginica
#> 140          6.9         3.1          5.4         2.1  virginica
#> 141          6.7         3.1          5.6         2.4  virginica
#> 142          6.9         3.1          5.1         2.3  virginica
#> 143          5.8         2.7          5.1         1.9  virginica
#> 144          6.8         3.2          5.9         2.3  virginica
#> 145          6.7         3.3          5.7         2.5  virginica
#> 146          6.7         3.0          5.2         2.3  virginica
#> 147          6.3         2.5          5.0         1.9  virginica
#> 148          6.5         3.0          5.2         2.0  virginica
#> 149          6.2         3.4          5.4         2.3  virginica
#> 150          5.9         3.0          5.1         1.8  virginica

Created on 2024-03-19 with reprex v2.1.0

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.1 (2023-06-16)
#>  os       macOS Ventura 13.6.3
#>  system   x86_64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/Toronto
#>  date     2024-03-19
#>  pandoc   3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.2   2023-12-11 [1] CRAN (R 4.3.0)
#>  colorspace    2.1-0   2023-01-23 [1] CRAN (R 4.3.0)
#>  digest        0.6.34  2024-01-11 [1] RSPM (R 4.3.0)
#>  dplyr       * 1.1.4   2023-11-17 [1] CRAN (R 4.3.0)
#>  evaluate      0.23    2023-11-01 [1] CRAN (R 4.3.0)
#>  fansi         1.0.6   2023-12-08 [1] CRAN (R 4.3.0)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.3.0)
#>  forcats     * 1.0.0   2023-01-29 [1] CRAN (R 4.3.0)
#>  fs            1.6.3   2023-07-20 [1] CRAN (R 4.3.0)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.3.0)
#>  ggplot2     * 3.5.0   2024-02-23 [1] RSPM (R 4.3.0)
#>  glue          1.7.0   2024-01-09 [1] RSPM (R 4.3.0)
#>  gtable        0.3.4   2023-08-21 [1] CRAN (R 4.3.0)
#>  hms           1.1.3   2023-03-21 [1] CRAN (R 4.3.0)
#>  htmltools     0.5.7   2023-11-03 [1] CRAN (R 4.3.0)
#>  knitr         1.45    2023-10-30 [1] CRAN (R 4.3.0)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.3.0)
#>  lubridate   * 1.9.3   2023-09-27 [1] CRAN (R 4.3.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.3.0)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.3.0)
#>  pillar        1.9.0   2023-03-22 [1] CRAN (R 4.3.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.3.0)
#>  purrr       * 1.0.2   2023-08-10 [1] CRAN (R 4.3.0)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.3.0)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.3.0)
#>  R.oo          1.26.0  2024-01-24 [1] RSPM (R 4.3.0)
#>  R.utils       2.12.3  2023-11-18 [1] CRAN (R 4.3.0)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.3.0)
#>  readr       * 2.1.5   2024-01-10 [1] RSPM (R 4.3.0)
#>  reprex        2.1.0   2024-01-11 [1] RSPM (R 4.3.0)
#>  rlang         1.1.3   2024-01-10 [1] RSPM (R 4.3.0)
#>  rmarkdown     2.25    2023-09-18 [1] CRAN (R 4.3.0)
#>  rstudioapi    0.15.0  2023-07-07 [1] CRAN (R 4.3.0)
#>  scales        1.3.0   2023-11-28 [1] CRAN (R 4.3.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.3.0)
#>  stringi       1.8.3   2023-12-11 [1] CRAN (R 4.3.0)
#>  stringr     * 1.5.1   2023-11-14 [1] CRAN (R 4.3.0)
#>  styler        1.10.2  2023-08-29 [1] CRAN (R 4.3.0)
#>  tibble      * 3.2.1   2023-03-20 [1] CRAN (R 4.3.0)
#>  tidyr       * 1.3.1   2024-01-24 [1] RSPM (R 4.3.0)
#>  tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.3.0)
#>  tidyverse   * 2.0.0   2023-02-22 [1] CRAN (R 4.3.0)
#>  timechange    0.3.0   2024-01-18 [1] RSPM (R 4.3.0)
#>  tzdb          0.4.0   2023-05-12 [1] CRAN (R 4.3.0)
#>  utf8          1.2.4   2023-10-22 [1] CRAN (R 4.3.0)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.3.0)
#>  withr         3.0.0   2024-01-16 [1] RSPM (R 4.3.0)
#>  xfun          0.42    2024-02-08 [1] RSPM (R 4.3.0)
#>  yaml          2.3.8   2023-12-11 [1] CRAN (R 4.3.0)
#> 
#>  [1] /Users/timothychisamore/Library/R/x86_64/4.3/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

All the best,

Tim

Thank you Tim.

I hope you can also guide me on the graph on number of facilities per level and per province. I don’t know what’s wrong with my syntax. There are three levels (1,2,3) and coded as 0 (No) and 1 (Yes). I converted the levels into numeric and yet I’m not getting the right results.

#Number of facilities per level and per province
ggplot(AFHF_long, aes(x = Level, y = No, fill = Province)) +
geom_bar(aes(x = Level, y = No, fill = Province), stat = “identity”, position = “dodge”) +
facet_wrap(~Province, scales = “free_y”, nrow = 4) +
labs(title = “Number and Level Facilities Visited, Samar and Southern Leyte”,
x = “Level”,
y = “Number of Facilities”) +
scale_y_continuous() +
theme_bw()

Hi,

Can you provide a reproducible example? Please this post for guidance on creating one: How to post an R code question

All the best,

Tim

Hello Tim,

Here it is:

#Number of AFHF facilities per Level and Province?Wrong output
AFHF_long <-  AFHF_clean  %>% 
  adorn_totals("col", select = c(Level1,Level2,Level3)) %>% 
  pivot_longer(   
    cols = Level1:Level3,
    names_to = "Level",   
    values_to = "No") %>% 
  mutate(Level=ifelse(Level== "Level1", "1", Level)) %>% 
  mutate(Level=ifelse(Level== "Level2", "2", Level)) %>% 
  mutate(Level=ifelse(Level== "Level3", "3", Level)) %>% 
  filter(No=="1")   #No Level 2 and 3 as of now

demo_data <- AFHF_long %>%
  head(5) %>%                      
  select(Province, Facility, Level)

dpasta(demo_data)

tibble::tribble(
                                                                     ~Province,                     ~Facility, ~Level,
                                                                       "Samar",   "Samar Provincial Hospital",    "1",
                                                                       "Samar", "Catbalogan City Teen Center",    "1",
                                                              "Southern Leyte",   "Maasin City Health Unit I",    "1",
                                                              "Southern Leyte",  "Maasin City Health Unit II",    "1",
                                                              "Southern Leyte", "Maasin City Health Unit III",    "1"
                                                              )
#> # A tibble: 5 × 3
#>   Province       Facility                    Level
#>   <chr>          <chr>                       <chr>
#> 1 Samar          Samar Provincial Hospital   1    
#> 2 Samar          Catbalogan City Teen Center 1    
#> 3 Southern Leyte Maasin City Health Unit I   1    
#> 4 Southern Leyte Maasin City Health Unit II  1    
#> 5 Southern Leyte Maasin City Health Unit III 1
ggplot(AFHF_long, aes(x = Level, y = No, fill = Province)) +
  geom_bar(aes(x = Level, y = No, fill = Province), stat = "identity", position = "dodge") +
  facet_wrap(~Province, scales = "free_y", nrow = 4) +
  labs(title = "Number and Level Facilities Visited, Samar and Southern Leyte",
       x = "Level",
       y = "Number of Facilities") +
  scale_y_continuous() +
  theme_bw()

Thank you.

Echo

Hi,

This example isn’t reproducible since you are using AFHF_long to plot your data and you do not provide this data, i.e., we do not have access to the No variable that you are trying to use in your plot.

All the best,

Tim

Oh sorry, here is the complete set:

AFHF_raw <- import(here("data",  "Encoding sheet for JPARAP Monitoring.xlsx")) %>% 
  clean_names() %>% 
  filter(x1!="Province") %>% 
  select (-x4)

AFHF_raw[is.na(AFHF_raw)] <- 0


AFHF_clean <- AFHF_raw %>% 
  mutate_at(c("H_Q4(2023)_1014-M",
              "H_Q1(2024)_1014-M",
              "H_Q4(2023)_1014-F",
              "H_Q1(2024)_1014-F",
              "H_Q4(2023)-Q1(2024)_1014",
              "H_Q4(2023)-1519_M",
              "H_Q1(2024)-1519_M",
              "H_Q4(2023)-1519_F",
              "H_Q1(2024)-1519_F",
              "H_Q4(2023)-Q1(2024)_1519",
              "HM_Q4(2023)_1014-M",
              "HM_Q1(2024)_1014-M",
              "HM_Q4(2023)_1014-F",
              "HM_Q1(2024)_1014-F",
              "HM_Q4(2023)-Q1(2024)_1014",
              "HM_Q4(2023)-1519_M",
              "HM_Q1(2024)-1519_M",
              "HM_Q4(2023)-1519_F",
              "HM_Q1(2024)-1519_F",
              "HM_Q4(2023)-Q1(2024)_1519",
              "Trained_ARH",
              "Foundational course",
              "AJA",
              "ADEPT",
              "HYO",
              "AFHF Certified",
              "Level1",
              "Level2",
              "Level3"
              ), 
            as.numeric) 


#Number of AFHF facilities per Level and Province?
AFHF_long <-  AFHF_clean  %>% 
  adorn_totals("col", select = c(Level1,Level2,Level3)) %>% 
  pivot_longer(   
    cols = Level1:Level3,
    names_to = "Level",   
    values_to = "No") %>% 
  mutate(Level=ifelse(Level== "Level1", "1", Level)) %>% 
  mutate(Level=ifelse(Level== "Level2", "2", Level)) %>% 
  mutate(Level=ifelse(Level== "Level3", "3", Level)) %>% 
  filter(No=="1")   #there's no Level 2 and 3 as of now but may have once additional data comes in

demo_data <- AFHF_long %>%
  head(5) %>%                      
  select(Province, Facility, Level)

dpasta(demo_data)

tibble::tribble(
                                                                     ~Province,                     ~Facility, ~Level,
                                                                       "Samar",   "Samar Provincial Hospital",    "1",
                                                                       "Samar", "Catbalogan City Teen Center",    "1",
                                                              "Southern Leyte",   "Maasin City Health Unit I",    "1",
                                                              "Southern Leyte",  "Maasin City Health Unit II",    "1",
                                                              "Southern Leyte", "Maasin City Health Unit III",    "1"
                                                              )
#> # A tibble: 5 × 3
#>   Province       Facility                    Level
#>   <chr>          <chr>                       <chr>
#> 1 Samar          Samar Provincial Hospital   1    
#> 2 Samar          Catbalogan City Teen Center 1    
#> 3 Southern Leyte Maasin City Health Unit I   1    
#> 4 Southern Leyte Maasin City Health Unit II  1    
#> 5 Southern Leyte Maasin City Health Unit III 1
ggplot(AFHF_long, aes(x = Level, y = No, fill = Province)) +
  geom_bar(aes(x = Level, y = No, fill = Province), stat = "identity", position = "dodge") +
  facet_wrap(~Province, scales = "free_y", nrow = 4) +
  labs(title = "Number and Level Facilities Visited, Samar and Southern Leyte",
       x = "Level",
       y = "Number of Facilities") +
  scale_y_continuous() +
  theme_bw()

Hi,

We do not have access to the file you are using to define AFHF_raw, to make this example reproducible you need to create fake data that mimics this dataset. Alternatively, if you provide fake data for AFHF_long where you have not removed the No variable, this could potentially also work.

All the best,

Tim

Oh sorry got a bit confused with that.
Here’s the fake data for AFHF_raw

library(tidyverse)

generate_fake_names <- function(n) {
  replicate(n, paste0(sample(LETTERS, 5), collapse = ""))
}


set.seed(123)

# Fake data
AFHF_raw <- tibble(
  Province = sample(c("Province A", "Province B", "Province C"), size = 100, replace = TRUE),
  Facility = generate_fake_names(100),
  Operations = sample(c("Operation A", "Operation B", "Operation C"), size = 100, replace = TRUE),
  Type_Facility = sample(c("Type A", "Type B", "Type C"), size = 100, replace = TRUE),
  Others_Facility = sample(c("Other A", "Other B", "Other C"), size = 100, replace = TRUE),
  Hosp_Function = sample(c("Function A", "Function B", "Function C"), size = 100, replace = TRUE),
  Trained_ARH = sample(0:1, size = 100, replace = TRUE),
  Foundational_course = sample(0:1, size = 100, replace = TRUE),
  AJA = sample(0:1, size = 100, replace = TRUE),
  ADEPT = sample(0:1, size = 100, replace = TRUE),
  HYO = sample(0:1, size = 100, replace = TRUE),
  Others = sample(0:1, size = 100, replace = TRUE),
  `AFHF Certified` = sample(0:1, size = 100, replace = TRUE),
  Level1 = sample(0:1, size = 100, replace = TRUE),
  Level2 = sample(0:1, size = 100, replace = TRUE),
  Level3 = sample(0:1, size = 100, replace = TRUE),
  `Regular_DemandGen` = sample(0:1, size = 100, replace = TRUE),
  `DemandGen Activities` = sample(0:1, size = 100, replace = TRUE),
  `H_Q4(2023)_1014-M` = rnorm(100, mean = 50, sd = 10),
  `H_Q1(2024)_1014-M` = rnorm(100, mean = 50, sd = 10),
  `H_Q4(2023)_1014-F` = rnorm(100, mean = 50, sd = 10),
  `H_Q1(2024)_1014-F` = rnorm(100, mean = 50, sd = 10),
  `H_Q4(2023)-Q1(2024)_1014` = rnorm(100, mean = 50, sd = 10),
  `H_Q4(2023)-1519_M` = rnorm(100, mean = 50, sd = 10),
  `H_Q1(2024)-1519_M` = rnorm(100, mean = 50, sd = 10),
  `H_Q4(2023)-1519_F` = rnorm(100, mean = 50, sd = 10),
  `H_Q1(2024)-1519_F` = rnorm(100, mean = 50, sd = 10),
  `H_Q4(2023)-Q1(2024)_1519` = rnorm(100, mean = 50, sd = 10),
  `HM_Q4(2023)_1014-M` = rnorm(100, mean = 50, sd = 10),
  `HM_Q1(2024)_1014-M` = rnorm(100, mean = 50, sd = 10),
  `HM_Q4(2023)_1014-F` = rnorm(100, mean = 50, sd = 10),
  `HM_Q1(2024)_1014-F` = rnorm(100, mean = 50, sd = 10),
  `HM_Q4(2023)-Q1(2024)_1014` = rnorm(100, mean = 50, sd = 10),
  `HM_Q4(2023)-1519_M` = rnorm(100, mean = 50, sd = 10),
  `HM_Q1(2024)-1519_M` = rnorm(100, mean = 50, sd = 10),
  `HM_Q4(2023)-1519_F` = rnorm(100, mean = 50, sd = 10),
  `HM_Q1(2024)-1519_F` = rnorm(100, mean = 50, sd = 10),
  `HM_Q4(2023)-Q1(2024)_1519` = rnorm(100, mean = 50, sd = 10),
  `W_Recordingtool` = sample(0:1, size = 100, replace = TRUE),
  `Recordingtool` = sample(0:1, size = 100, replace = TRUE),
  `PAS_1st` = sample(0:1, size = 100, replace = TRUE),
  `PAS_2ndinitiatives` = sample(0:1, size = 100, replace = TRUE),
  `PAS_Techguidanceleaders` = sample(0:1, size = 100, replace = TRUE),
  `PAS_policies` = sample(0:1, size = 100, replace = TRUE),
  `PAS_perfaudit` = sample(0:1, size = 100, replace = TRUE),
  `Good_points` = sample(0:1, size = 100, replace = TRUE),
  `Implementation_inc` = sample(0:1, size = 100, replace = TRUE),
  `Pointstoimprove` = sample(0:1, size = 100, replace = TRUE)
)

# Convert specific vars to numeric
AFHF_clean <- AFHF_raw %>% 
  mutate_at(vars(starts_with("H_Q"), ends_with("_F")), as.numeric)

# Wide to long format
AFHF_long <-  AFHF_clean  %>% 
  adorn_totals("col", select = c(Level1,Level2,Level3)) %>% 
  pivot_longer(   
    cols = Level1:Level3,
    names_to = "Level",   
    values_to = "No") %>% 
  mutate(Level=ifelse(Level== "Level1", "1", Level)) %>% 
  mutate(Level=ifelse(Level== "Level2", "2", Level)) %>% 
  mutate(Level=ifelse(Level== "Level3", "3", Level)) %>% 
  filter(No=="1")   #No Level 2 and 3 as of now
1 Like

Hello,

I have modified your code to plot the number of facilities by province and level, please see below and let me know if I am misinterpreting your question:

# loading packages
library(tidyverse)
library(janitor)
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
#>     chisq.test, fisher.test

# creating fake data
generate_fake_names <- function(n) {
    replicate(n, paste0(sample(LETTERS, 5), collapse = ""))
}

AFHF_raw <- tibble(
    Province = sample(c("Province A", "Province B", "Province C"), size = 100, replace = TRUE),
    Facility = generate_fake_names(100),
    Operations = sample(c("Operation A", "Operation B", "Operation C"), size = 100, replace = TRUE),
    Type_Facility = sample(c("Type A", "Type B", "Type C"), size = 100, replace = TRUE),
    Others_Facility = sample(c("Other A", "Other B", "Other C"), size = 100, replace = TRUE),
    Hosp_Function = sample(c("Function A", "Function B", "Function C"), size = 100, replace = TRUE),
    Trained_ARH = sample(0:1, size = 100, replace = TRUE),
    Foundational_course = sample(0:1, size = 100, replace = TRUE),
    AJA = sample(0:1, size = 100, replace = TRUE),
    ADEPT = sample(0:1, size = 100, replace = TRUE),
    HYO = sample(0:1, size = 100, replace = TRUE),
    Others = sample(0:1, size = 100, replace = TRUE),
    `AFHF Certified` = sample(0:1, size = 100, replace = TRUE),
    Level1 = sample(0:1, size = 100, replace = TRUE),
    Level2 = sample(0:1, size = 100, replace = TRUE),
    Level3 = sample(0:1, size = 100, replace = TRUE),
    `Regular_DemandGen` = sample(0:1, size = 100, replace = TRUE),
    `DemandGen Activities` = sample(0:1, size = 100, replace = TRUE),
    `H_Q4(2023)_1014-M` = rnorm(100, mean = 50, sd = 10),
    `H_Q1(2024)_1014-M` = rnorm(100, mean = 50, sd = 10),
    `H_Q4(2023)_1014-F` = rnorm(100, mean = 50, sd = 10),
    `H_Q1(2024)_1014-F` = rnorm(100, mean = 50, sd = 10),
    `H_Q4(2023)-Q1(2024)_1014` = rnorm(100, mean = 50, sd = 10),
    `H_Q4(2023)-1519_M` = rnorm(100, mean = 50, sd = 10),
    `H_Q1(2024)-1519_M` = rnorm(100, mean = 50, sd = 10),
    `H_Q4(2023)-1519_F` = rnorm(100, mean = 50, sd = 10),
    `H_Q1(2024)-1519_F` = rnorm(100, mean = 50, sd = 10),
    `H_Q4(2023)-Q1(2024)_1519` = rnorm(100, mean = 50, sd = 10),
    `HM_Q4(2023)_1014-M` = rnorm(100, mean = 50, sd = 10),
    `HM_Q1(2024)_1014-M` = rnorm(100, mean = 50, sd = 10),
    `HM_Q4(2023)_1014-F` = rnorm(100, mean = 50, sd = 10),
    `HM_Q1(2024)_1014-F` = rnorm(100, mean = 50, sd = 10),
    `HM_Q4(2023)-Q1(2024)_1014` = rnorm(100, mean = 50, sd = 10),
    `HM_Q4(2023)-1519_M` = rnorm(100, mean = 50, sd = 10),
    `HM_Q1(2024)-1519_M` = rnorm(100, mean = 50, sd = 10),
    `HM_Q4(2023)-1519_F` = rnorm(100, mean = 50, sd = 10),
    `HM_Q1(2024)-1519_F` = rnorm(100, mean = 50, sd = 10),
    `HM_Q4(2023)-Q1(2024)_1519` = rnorm(100, mean = 50, sd = 10),
    `W_Recordingtool` = sample(0:1, size = 100, replace = TRUE),
    `Recordingtool` = sample(0:1, size = 100, replace = TRUE),
    `PAS_1st` = sample(0:1, size = 100, replace = TRUE),
    `PAS_2ndinitiatives` = sample(0:1, size = 100, replace = TRUE),
    `PAS_Techguidanceleaders` = sample(0:1, size = 100, replace = TRUE),
    `PAS_policies` = sample(0:1, size = 100, replace = TRUE),
    `PAS_perfaudit` = sample(0:1, size = 100, replace = TRUE),
    `Good_points` = sample(0:1, size = 100, replace = TRUE),
    `Implementation_inc` = sample(0:1, size = 100, replace = TRUE),
    `Pointstoimprove` = sample(0:1, size = 100, replace = TRUE)
)

# cleaning fake data
AFHF_clean <- AFHF_raw |>
    mutate(across(c(starts_with("H_Q"), ends_with("_F")), as.numeric))

# aggregating count of level by province and pivoting into a long format
AFHF_long <- AFHF_clean |>
    group_by(Province) |>
    summarize(Level1 = sum(Level1),
                        Level2 = sum(Level2),
                        Level3 = sum(Level3), .groups = "drop") |>
    pivot_longer(cols = Level1:Level3,
                             names_to = "Level",
                             values_to = "No") |>
    mutate(
        Level = case_when(
            Level == "Level1" ~ "1",
            Level == "Level2" ~ "2",
            Level == "Level3" ~ "3",
            .default = "Other"
        )
    )

# plotting the data
ggplot(AFHF_long, aes(x = Level, y = No, fill = Province)) +
    geom_bar(aes(x = Level, y = No, fill = Province), stat = "identity", position = "dodge") +
    facet_wrap(~Province, scales = "free_y", nrow = 4) +
    labs(title = "Number and Level Facilities Visited, Samar and Southern Leyte",
             x = "Level",
             y = "Number of Facilities") +
    scale_y_continuous() +
    theme_bw()

Created on 2024-03-23 with reprex v2.1.0

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.1 (2023-06-16)
#>  os       macOS Ventura 13.6.3
#>  system   x86_64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/Toronto
#>  date     2024-03-23
#>  pandoc   3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.2   2023-12-11 [1] CRAN (R 4.3.0)
#>  colorspace    2.1-0   2023-01-23 [1] CRAN (R 4.3.0)
#>  curl          5.2.0   2023-12-08 [1] CRAN (R 4.3.0)
#>  digest        0.6.34  2024-01-11 [1] RSPM (R 4.3.0)
#>  dplyr       * 1.1.4   2023-11-17 [1] CRAN (R 4.3.0)
#>  evaluate      0.23    2023-11-01 [1] CRAN (R 4.3.0)
#>  fansi         1.0.6   2023-12-08 [1] CRAN (R 4.3.0)
#>  farver        2.1.1   2022-07-06 [1] CRAN (R 4.3.0)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.3.0)
#>  forcats     * 1.0.0   2023-01-29 [1] CRAN (R 4.3.0)
#>  fs            1.6.3   2023-07-20 [1] CRAN (R 4.3.0)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.3.0)
#>  ggplot2     * 3.5.0   2024-02-23 [1] RSPM (R 4.3.0)
#>  glue          1.7.0   2024-01-09 [1] RSPM (R 4.3.0)
#>  gtable        0.3.4   2023-08-21 [1] CRAN (R 4.3.0)
#>  highr         0.10    2022-12-22 [1] CRAN (R 4.3.0)
#>  hms           1.1.3   2023-03-21 [1] CRAN (R 4.3.0)
#>  htmltools     0.5.7   2023-11-03 [1] CRAN (R 4.3.0)
#>  janitor     * 2.2.0   2023-02-02 [1] CRAN (R 4.3.0)
#>  knitr         1.45    2023-10-30 [1] CRAN (R 4.3.0)
#>  labeling      0.4.3   2023-08-29 [1] CRAN (R 4.3.0)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.3.0)
#>  lubridate   * 1.9.3   2023-09-27 [1] CRAN (R 4.3.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.3.0)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.3.0)
#>  pillar        1.9.0   2023-03-22 [1] CRAN (R 4.3.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.3.0)
#>  purrr       * 1.0.2   2023-08-10 [1] CRAN (R 4.3.0)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.3.0)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.3.0)
#>  R.oo          1.26.0  2024-01-24 [1] RSPM (R 4.3.0)
#>  R.utils       2.12.3  2023-11-18 [1] CRAN (R 4.3.0)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.3.0)
#>  readr       * 2.1.5   2024-01-10 [1] RSPM (R 4.3.0)
#>  reprex        2.1.0   2024-01-11 [1] RSPM (R 4.3.0)
#>  rlang         1.1.3   2024-01-10 [1] RSPM (R 4.3.0)
#>  rmarkdown     2.25    2023-09-18 [1] CRAN (R 4.3.0)
#>  rstudioapi    0.15.0  2023-07-07 [1] CRAN (R 4.3.0)
#>  scales        1.3.0   2023-11-28 [1] CRAN (R 4.3.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.3.0)
#>  snakecase     0.11.1  2023-08-27 [1] CRAN (R 4.3.0)
#>  stringi       1.8.3   2023-12-11 [1] CRAN (R 4.3.0)
#>  stringr     * 1.5.1   2023-11-14 [1] CRAN (R 4.3.0)
#>  styler        1.10.2  2023-08-29 [1] CRAN (R 4.3.0)
#>  tibble      * 3.2.1   2023-03-20 [1] CRAN (R 4.3.0)
#>  tidyr       * 1.3.1   2024-01-24 [1] RSPM (R 4.3.0)
#>  tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.3.0)
#>  tidyverse   * 2.0.0   2023-02-22 [1] CRAN (R 4.3.0)
#>  timechange    0.3.0   2024-01-18 [1] RSPM (R 4.3.0)
#>  tzdb          0.4.0   2023-05-12 [1] CRAN (R 4.3.0)
#>  utf8          1.2.4   2023-10-22 [1] CRAN (R 4.3.0)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.3.0)
#>  withr         3.0.0   2024-01-16 [1] RSPM (R 4.3.0)
#>  xfun          0.42    2024-02-08 [1] RSPM (R 4.3.0)
#>  xml2          1.3.6   2023-12-04 [1] CRAN (R 4.3.0)
#>  yaml          2.3.8   2023-12-11 [1] CRAN (R 4.3.0)
#> 
#>  [1] /Users/timothychisamore/Library/R/x86_64/4.3/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

All the best,

Tim

Oh that was perfect, you used case_when instead of ifelse, and you aggregated the count of level per province before pivoting to long format.

Thank you so much, esp. for the patience.

Regards,
Echo

1 Like