Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在ggplot2中的减淡几何图形栏中的类别集之间添加空间?_R_Ggplot2_Bar Chart_Geom Bar - Fatal编程技术网

R 如何在ggplot2中的减淡几何图形栏中的类别集之间添加空间?

R 如何在ggplot2中的减淡几何图形栏中的类别集之间添加空间?,r,ggplot2,bar-chart,geom-bar,R,Ggplot2,Bar Chart,Geom Bar,这是产品: 我的问题是——是否在每个问题的答案集之间留出一些空间?我希望相同颜色的列彼此相邻,但同时,我希望在不同颜色的列之间添加一些空间,以便在视觉上更清楚地显示这是5个不同的变量 此外,如果可能的话,我想为每个类别分别以降序排列列。 不幸的是,增加宽度的位置_道奇 DF %>% pivot_longer(cols = 2:6, values_to = "Answer", names_to = "Category") %>% coun

这是产品:

我的问题是——是否在每个问题的答案集之间留出一些空间?我希望相同颜色的列彼此相邻,但同时,我希望在不同颜色的列之间添加一些空间,以便在视觉上更清楚地显示这是5个不同的变量

此外,如果可能的话,我想为每个类别分别以降序排列列。 不幸的是,增加宽度的位置_道奇

DF %>% pivot_longer(cols = 2:6, values_to = "Answer", names_to = "Category") %>% 
  count(Category, Decision, Answer) %>% 
   group_by(Category, Answer) %>% mutate(percent = n / sum(n) * 100) %>% 
    filter(Decision == "Positive") %>% 
  ggplot(aes(Answer %>% fct_reorder2(percent, Category), percent, fill = Category)) +
  geom_bar(stat = "identity", position = position_dodge(), width = 0.95, color = "black", alpha = 0.5) + coord_flip() +
  scale_fill_uchicago() + labs(x = "", y = "", fill = "") + scale_y_continuous(breaks = seq(0, 100, 20), labels = str_c(seq(0, 100, 20), "%")) +
    theme_classic() + theme(legend.position = "top")
不起作用,我想这是有道理的


如果有任何帮助,我将不胜感激。提前谢谢你

可以这样实现:

  • 要在类别之间添加一些空间,您可以使用
    facet\u grid
    ,去掉条形文本并将面板间距设置为零。此外,我还使用了
    space=“free”
    ,这样您的条形图仍然具有相同的宽度

  • 要按降序重新排列条形图,可以使用
    tidytext::reorder\u in
    tidytext::scale\u x\u reorder()

  • 库(tidyverse)
    图书馆(ggsci)
    图书馆(tidytext)
    种子(42)
    DF%
    pivot_更长(cols=2:6,值_to=“Answer”,名称_to=“Category”)%>%
    计数(类别、决定、答案)%>%
    分组(类别、答案)%>%
    突变(百分比=n/和(n)*100)%>%
    过滤器(判定==“正”)%>%
    解组()%>%
    mutate(Answer=tidytext::在(Answer,by=percent,in=Category))范围内重新排序%>%
    ggplot(aes(答案、百分比、填充=类别))+
    几何图形条(stat=“identity”,position=position\u dodge(),width=0.9,color=“black”,alpha=0.5)+
    coord_flip()+
    比例尺填充比例尺+
    实验室(x=”,y=”,fill=)+
    刻度连续(断开=顺序(0,100,20),标签=连续(顺序(0,100,20),“%”)+
    tidytext::scale_x_reordered()+
    刻面网格(类别~,比例=“自由”,空间=“自由”)+
    主题(经典)+
    主题(legend.position=“top”,strip.text=element_blank(),panel.space.y=unit(0,“pt”))
    

    非常感谢!谢谢你的时间和回答,这正是我所需要的。
    DF %>% pivot_longer(cols = 2:6, values_to = "Answer", names_to = "Category") %>% 
      count(Category, Decision, Answer) %>% 
       group_by(Category, Answer) %>% mutate(percent = n / sum(n) * 100) %>% 
        filter(Decision == "Positive") %>% 
      ggplot(aes(Answer %>% fct_reorder2(percent, Category), percent, fill = Category)) +
      geom_bar(stat = "identity", position = position_dodge(), width = 0.95, color = "black", alpha = 0.5) + coord_flip() +
      scale_fill_uchicago() + labs(x = "", y = "", fill = "") + scale_y_continuous(breaks = seq(0, 100, 20), labels = str_c(seq(0, 100, 20), "%")) +
        theme_classic() + theme(legend.position = "top")
    
    position_dodge(0.5)