在R中整理ggplot的轴标签 [英] Uncluttering the axis labels of a ggplot in R

查看:93
本文介绍了在R中整理ggplot的轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的data,其中8个人从4个问题的五个答案选项中选择了一个.但是,每个问题的这五个答案选择是不同的.

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.

当前,如果我对问题进行facet_wrap(),则每行中翻转的x轴会显示10个答案选项作为轴标签(请参见下图).

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).

这些标签看起来很乱.是否有一种紧凑的方式(例如,可能不使用facet_wrap)来可视化此数据,而翻转的x轴标签看起来并不那么混乱(每个问题仅显示5个答案选项作为该问题的标签(即5个唯一) Representation的轴标签,Solidification的5个唯一轴标签等))?

These labels look very messy. Is there a compact way (e.g., 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 (i.e., 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))

推荐答案

您可以使用facet_wrap(..., scales = "free_y"):

(我还将字符串包装长度更改为30)

(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))

编辑以前的错误答案:

您需要清理value列以使其与 name列.将答案转化为相同的值 问题,例如

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, e.g.

强烈同意-同意-中立-不同意-强烈不同意

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

您在y轴上具有较短的值,这些值也与 每个方面.

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.

这篇关于在R中整理ggplot的轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆