繁体   English   中英

重新排序堆叠的geom_bar

[英]reordering stacked geom_bar

我第一次在这里问一个问题,所以请放轻松! 正如您从我的示例代码中看到的那样,我也是R的新手(3个月),因此显示它有些尴尬! 我有一个特定的要求,但是可能会有更好的可视化方式。 我们所在地区的人已经回答了几次问题,我们想比较最初和最近的回答。 答案基本上是1-5级,但我把它留作罗y的答案(从“完全不足”到“完全足够”)。 我想将“最差”答案显示为否定,将“好”答案显示为肯定,然后将中间答案(“相当不足”)一分为二,以使中途点有点集中在图上(我知道吗? !下面有链接的示例图。 我也想按区域分组,这是第一个响应还是最后一个响应。 如果我绘制两个单独的数据框,则图表看起来不错,但我无法订购图例。 如果我合并数据框,则图例看起来不错,但是图表出错了! 请帮忙!

# Input load
`dataset` = readr::read_csv("FirstLast,AnswerCount,Answer,Region
                 First,10,Completely Insufficient,North
                 First,3,Completely Insufficient,South
                 Last,5,Completely Insufficient,North
                 Last,1,Completely Insufficient,South
                 First,8,Mostly Insufficient,North
                 First,2,Mostly Insufficient,South
                 Last,9,Mostly Insufficient,North
                 Last,2,Mostly Insufficient,South
                 First,14,Quite Insufficient,North
                 First,3,Quite Insufficient,South
                 Last,19,Quite Insufficient,North
                 Last,7,Quite Insufficient,South
                 First,26,Mostly Sufficient,North
                 First,9,Mostly Sufficient,South
                 Last,44,Mostly Sufficient,North
                 Last,17,Mostly Sufficient,South
                 First,8,Completely Sufficient,North
                 First,3,Completely Sufficient,South
                 Last,16,Completely Sufficient,North
                 Last,3,Completely Sufficient,South")
require("dplyr")
library(dplyr)
require("ggplot2")
library(ggplot2)
require("tidyr")
library(tidyr)
require("stringr")
library(stringr)
require("formattable")
library(formattable)

# split mid answer for First reviews
Reviews.First.four <- filter(Reviews.Sums, FirstLast == "First", Answer=="Quite Insufficient") %>% mutate(AnswerCount=as.numeric(AnswerCount/2))
Reviews.First.rest <- filter(Reviews.Sums, FirstLast == "First", Answer != "Quite Insufficient")
Reviews.First <- full_join(Reviews.First.four, Reviews.First.rest) %>% arrange(Answer)
Reviews.First <- mutate(Reviews.First, RegRev = paste(Region, FirstLast))

# split mid answer for Last reviews
Reviews.Last.four <- filter(Reviews.Sums, FirstLast == "Last", Answer=="Quite Insufficient") %>% mutate(AnswerCount=as.numeric(AnswerCount/2))
Reviews.Last.rest <- filter(Reviews.Sums, FirstLast == "Last", Answer !="Quite Insufficient")
Reviews.Last <- full_join(Reviews.Last.four, Reviews.Last.rest) %>% arrange(Answer)
Reviews.Last <- mutate(Reviews.Last, RegRev = paste(Region,FirstLast))

# Split data into negative and positive scores
Reviews.First.Neg <- Reviews.First %>% 
filter (Answer == "Completely Insufficient" | Answer == "Mostly Insufficient" | Answer == "Quite Insufficient") %>% 
mutate(AnswerCount = AnswerCount *-1)
Reviews.First.Pos <- Reviews.First %>% 
filter (Answer == "Quite Insufficient" | Answer == "Mostly Sufficient" | Answer == "Completely Sufficient") 

Reviews.Last.Neg <- Reviews.Last %>% 
filter (Answer == "Completely Insufficient" | Answer == "Mostly Insufficient" | Answer == "Quite Insufficient") %>% 
mutate(AnswerCount = AnswerCount *-1)
Reviews.Last.Pos <-Reviews.Last %>% 
filter (Answer == "Quite Insufficient" | Answer == "Mostly Sufficient" | Answer == "Completely Sufficient") 

# Reorder factors (or try to anyway!)
Reviews.First.Neg$Answer <- factor(Reviews.First.Neg$Answer, levels=c("Completely Insufficient", "Mostly Insufficient", "Quite Insufficient"))
Reviews.First.Pos$Answer <- factor(Reviews.First.Pos$Answer, levels=rev(c("Quite Insufficient", "Mostly Sufficient", "Completely Sufficient")))
Reviews.Last.Neg$Answer <- factor(Reviews.Last.Neg$Answer, levels=c("Completely Insufficient", "Mostly Insufficient", "Quite Insufficient"))
Reviews.Last.Pos$Answer <- factor(Reviews.Last.Pos$Answer, levels=rev(c("Quite Insufficient", "Mostly Sufficient", "Completely Sufficient")))
# Other thing I tried was to order both factors same before union-ing them - plot Reviews.all instead of the separate First.Pos and First.Neg and still no joy - sad smiley 
#Reviews.First.Neg$Answer <- factor(Reviews.First.Neg$Answer, levels=c("Completely Insufficient", "Mostly Insufficient", "Quite Insufficient", "Mostly Sufficient", "Completely Sufficient"))
#Reviews.First.Pos$Answer <- factor(Reviews.First.Pos$Answer, levels=c("Completely Insufficient", "Mostly Insufficient", "Quite Insufficient", "Mostly Sufficient", "Completely Sufficient"))
#Reviews.all <- union(Reviews.First.Neg, Reviews.First.Pos)
#Reviews.all$Answer = factor(Reviews.all$Answer, levels=c("Completely Insufficient", "Mostly Insufficient", "Quite Insufficient", "Mostly Sufficient", "Completely Sufficient"))

# and plot!
ggplot() + 
# geom_bar(data=Reviews.all, aes(x=RegRev, y=AnswerCount, fill=Answer), stat="identity", position = "stack") +
geom_bar(data=Reviews.First.Neg, aes(x=RegRev, y=AnswerCount, fill=Answer), stat="identity", position = "stack") +
geom_bar(data=Reviews.First.Pos, aes(x=RegRev, y=AnswerCount, fill=Answer), stat="identity", position = "stack") +
geom_bar(data=Reviews.Last.Neg, aes(x=RegRev, y=AnswerCount, fill=Answer), stat="identity", position = "stack") +
geom_bar(data=Reviews.Last.Pos, aes(x=RegRev, y=AnswerCount, fill=Answer), stat="identity", position = "stack") +
coord_flip() + 
theme_minimal() + 
scale_fill_manual(values = c("#d7191c","#fdae61","#ffffbf","#abdda4","#2b83ba"))+
theme(
legend.position = "top"
) +
guides(fill = guide_legend(nrow = 2, byrow=TRUE))

TLDR-我在R方面感到很糟糕。非常感谢您的帮助。

如果我绘制两个单独的数据框,则图表看起来不错,但我无法订购图例。

如果我合并数据框(在本例中仅适用于First),则图例看起来不错,但是图表出错了! 啊!

让我知道您是否也想进行其他调整。

    Reviews.comb <- bind_rows(Reviews.Last.Pos, Reviews.Last.Neg, Reviews.First.Pos, Reviews.First.Neg)

    cols <- c("#d7191c","#fdae61","#ffffbf","#abdda4","#2b83ba")
    ord <- c("Completely Insufficient", "Mostly Insufficient", "Quite Insufficient", "Mostly Sufficient", "Completely Sufficient")

    ggplot() + geom_bar(data=Reviews.comb, aes(x=RegRev, y=AnswerCount, fill=Answer), stat="identity", position = "stack") +
      coord_flip() + 
      theme_minimal() + 
      scale_fill_manual(breaks = ord, values = cols) +
      theme(
        legend.position = "top") +
      guides(fill = guide_legend(nrow = 2, byrow=TRUE)) + 
      labs(x = "Answer Count", y = "Reg Rev") + expand_limits(y = c(-50, 50))

更新:我添加了expand_limits,以尝试将条形图的中心设为0。

我还比较了您的工会命令和等效的bind_rowsbind_rows(Reviews.First.Pos, Reviews.First.Neg) ); 只是顺序不同。 这可能是改变图形顺序的原因。 中断部分应为您重新排列图形。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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