简体   繁体   中英

Uncluttering the axis labels of a ggplot in R

I have a small data where 8 people have picked one of five answer choices for 4 questions. But these five answer choices for each question is different.

Currently, if I facet_wrap() over the questions, the flipped x axis in each row shows 10 answer choices as the axis labels (see pic. below).

These labels look very messy. Is there a compact way (eg, maybe not using facet_wrap ) of visualizing this data where flipped x axis labels look not so messy (showing only the 5 answer choices for each question as labels for that question (ie, 5 unique axis labels for Representation , 5 unique axis labels for Solidification etc.))?

library(tidyverse)

data <- read_csv('https://raw.githubusercontent.com/rnorouzian/e/master/surv.csv')

names(data)[2:5] <- c("Representation", "Solidification", "Application", "Confidence")

data %>%
  pivot_longer(cols = -id) %>% 
  mutate(name = name, 
         value = str_wrap(value, 50)) %>%
  ggplot() + 
  geom_bar(aes(value, fill = name), show.legend = FALSE) + 
  facet_wrap(.~name) + 
  coord_flip() +
  theme(axis.text.y = element_text(size=8))

在此处输入图片说明

You can use facet_wrap(..., scales = "free_y") :

(I also changed the string wrapping length to 30)

data %>%
    pivot_longer(cols = -id) %>% 
    mutate(name = name, 
           value = str_wrap(value, 30)) %>%
    ggplot() + 
    geom_bar(aes(value, fill = name), show.legend = FALSE) + 
    facet_wrap(.~name, scales = "free_y") + 
    coord_flip() +
    theme(axis.text.y = element_text(size=8))

阴谋

EDIT previous wrong answer:

You need to clean up your value column to be orthogonal to the name column. Translate the answers into the same values for every question, eg

strongly agree -- agree -- neutral -- disagree -- strongly disagree

The you have shorter values on the y axis that are also the same for each facet.

Your data set could look like this:

 Representation "strongly agree" "The assignment highly reflected the class\\ninstructions" 4240222 Solidification "strongly agree" "The assignment highly helped me solidify the key\\nconcepts" 4240222 Application "strongly agree" "The assignment gave me a great opportunity to\\napply what I learned" 4240222 Confidence "strongly agree" "Strongly Agree" 4187679 Representation agree "The assignment reflected the class instructions" 4187679 Solidification agree "The assignment helped me solidify the key concepts" 4187679 Application "strongly agree" "The assignment gave me a great opportunity to\\napply what I learned" 4187679 Confidence neutral "Neutral" 4110077 Representation "strongly agree" "The assignment highly reflected the class\\ninstructions" 4110077 Solidification agree "The assignment helped me solidify the key concepts"

you can set up a translation table like this:

 The assignment highly reflected the class instructions strongly agree; The assignment highly helped me solidify the key concepts strongly agree; The assignment gave me a great opportunity to apply what I learned strongly agree; Strongly Agree agree; The assignment reflected the class instructions agree; The assignment helped me solidify the key concepts agree; Agree neutral; Neutral ....' ) ``` Then you can use [`left_join()`](https://dplyr.tidyverse.org/reference/join.html) to add the correct values of `translate$grade` to your data based on the common column `value`. "R for data science" has a [longer explanation](https://r4ds.had.co.nz/relational-data.html) of joins.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM