R for the Rest of Us Community
Public / Community
Public / Community
This is a place to ask questions and get help along the way on your R journey.
In addition to discussions of general questions, you’ll see threads for office hours. These are twice-monthly sessions to help you get unstuck. Ask questions and get live answers from me as well as guest experts. All code for office hours can be found here.
Not yet a member? If you have an account on the R for the Rest of Us website and are logged in, just click Join Group on the top right of this page. If you need to create an account, you can sign up here.
Ordering Likert scales in Cross Tabs
-
In this cross-tab below, how can we order the Likert scale (Excellent, Vgood, Good, Fair, Poor) instead of having it in ascending alphabetical order?
nhanes %>%
drop_na(gender, health_gen) %>%
tabyl(gender, health_gen) %>%
adorn_totals(where = c("row", "col")) %>%
adorn_percentages() %>%
adorn_pct_formatting(digits = 1) %>%
adorn_ns() -
Have you tried
fct_reorder
? See here some examples. In general, I tend to reorder factors (e.g., dichotomous, categorical vars) before pushing them into a table or plot to avoid cleaning variables in the table/plot.The most basic way of formatting factors is through the
factor()
function. E.g.,factor(health_gen, levels = c("Poor", "Fair", "Good", "Vgood", "Excellent"))
.-
Thanks, @diego-catalan-molina for your suggestions. I’m not sure I understand the concept of factors to apply these solutions correctly. When I try
nhanes %>%
drop_na(gender, health_gen) %>%
factor(health_gen, levels = c("Poor", "Fair", "Good", "Vgood", "Excellent")) %>%
tabyl(gender, health_gen) %>%
adorn_totals(where = c("row", "col")) %>%
adorn_percentages() %>%
adorn_pct_formatting(digits = 1) %>%
adorn_ns()
I get this error:
Error in factor(., health_gen, levels = c(“Poor”, “Fair”, “Good”, “Vgood”, : object ‘health_gen’ not found
When I try
nhanes %>%
drop_na(gender, health_gen) %>%
fct_reorder(health_gen, levels = c("Poor", "Fair", "Good", "Vgood", "Excellent")) %>%
tabyl(gender, health_gen) %>%
adorn_totals(where = c("row", "col")) %>%
adorn_percentages() %>%
adorn_pct_formatting(digits = 1) %>%
adorn_ns()I get this error:
Error:
f
must be a factor (or character vector).Is it because I’m starting with the nhanes dataframe?
-
Sorry @jordan-trachtenberg ! I should have been more clear. This is what you want:
nhanes %>%
mutate(health_gen_asfactor = #name of your new var
factor(health_gen, #original var
levels = c(“Poor”, “Fair”, “Good”, “Vgood”, “Excellent”)) #order you want to impose on categories
) %>% # add the rest of your pipe
-
This reply was modified 6 months ago by
diego.catalan molina.
-
This reply was modified 6 months ago by
-
Thank you, @diego-catalan-molina .
I got this to work for the cross-tab table:
nhanes %>%
mutate(health_gen_asfactor = factor(health_gen,
levels = c("Poor", "Fair", "Good", "Vgood", "Excellent"))
) %>%
drop_na(gender, health_gen) %>%
tabyl(gender, health_gen_asfactor) %>%
adorn_totals(where = c("row", "col")) %>%
adorn_percentages() %>%
adorn_pct_formatting(digits = 1) %>%
adorn_ns()
-
-
Thanks for jumping in, @diego-catalan-molina !
@jordan-trachtenberg just to clarify, are you trying to make a plot or reorder in a table (or table-like output)? I ask because the answer differs depending on what you want to do. Factors are for reordering in plots. The
arrange()
function works well when reordering in tables.-
For now, I’m trying to order the Likert scale in a table, but I could imagine that I would also need it for a plot in the future. I guess it’s good to know that those preferences require separate functions!
-
-
I’d love to go over this in the office hours this Friday at 10am Pacific. Any chance you can join so we can discuss there?
-
-
@dgkeyes I think I figured out the plot as well:
# Set up new data frame with desired Likert scale order
sleep_by_gender_andhealth <- nhanes %>%
mutate(ordered_healthgen = factor(health_gen, levels = c("Poor", "Fair", "Good", "Vgood", "Excellent"))) %>%
group_by(gender, ordered_healthgen) %>%
drop_na(gender, health_gen) %>%
summarize(mean_hrs_sleep = round(mean(sleep_hrs_night,
na.rm = TRUE),
1))
# Create visualization with new data frame
sleep_by_gender_andhealth %>%
ggplot(mapping = aes(x = ordered_healthgen,
y = mean_hrs_sleep,
fill = gender)) +
geom_bar(stat = "identity") +
coord_flip()
-
Log in to reply.