scales::percent() converts to character instead of numeric

  • scales::percent() converts to character instead of numeric

     David updated 1 month ago 2Members · 3 Posts
  • jordan.trachtenberg

    Member
    December 18, 2020 at 12:14 pm

    Hi David,

    In your scales package section, I noticed that the percent() function actually converts values to characters, so that when I run my code, the y-axis is completely out of order. Is there a way to avoid having to both format and convert as a number?

    library(scales)

    highlight_school <- change_over_time %>%

    filter(race_ethnicity == "Black/African American") %>%

    filter(district_name == "Southern Oregon ESD") %>%

    mutate(percent_students_display = percent(percent_students, accuracy = 1))

    change_over_time %>%

    filter(race_ethnicity == "Black/African American") %>%

    mutate(percent_students_display = percent(percent_students, accuracy = 1)) %>% # 1 - nearest whole number, 0.1 - 1 decimal place

    ggplot(aes(x = year,

    y = percent_students_display,

    group = district_id)) +

    geom_point(color = rru_gray) +

    geom_point(data = highlight_school,

    inherit.aes = TRUE, # Use the same aesthetic properties as ggplot

    color = rru_red) +

    geom_line(color = rru_gray,

    alpha = 0.5) + # Decreases opacity of lines so that you can see more

    geom_line(data = highlight_school,

    inherit.aes = TRUE,

    color = rru_red) +

    labs(title = "Change in % of Black/African American students",

    subtitle = "for Oregon school districts from 2017-18 to 2018-19",

    caption = "test caption: highlighted population sizes may be small") +

    theme_minimal() +

    theme(plot.title.position = "plot", # Left justifies to plot instead of axis edge

    plot.caption = element_text(color = "orange", size = 8),

    axis.title = element_blank(), # Can use this command instead of labs() function

    axis.text = element_text(family = "Georgia",

    size = 12),

    panel.grid.minor = element_blank())

    I have done this with

    scale_y_continuous(labels = percent_format())

    but didn’t know if there was another way using the scales package when you mutate the variable.

  • jordan.trachtenberg

    Member
    December 18, 2020 at 12:47 pm

    Nevermind. I got ahead mentally of the next two lessons!

  • David

    Organizer
    December 18, 2020 at 1:18 pm

    Glad you figured it out! For anyone else reading this, I’d suggest using a numeric variable for the y axis and using the percent_format() function from the scales package for the y axis text.

Viewing 1 - 3 of 3 posts

Log in to reply.

Original Post
0 of 0 posts June 2018
Now