重新排序堆叠 geom_bar
reordering stacked geom_bar
我第一次在这里提问,所以请放轻松!正如您将从我的示例代码中看到的那样,我也是 R 的新手(3 个月),所以展示它有点尴尬!
我有一个非常具体的要求,但可能有更好的可视化方法。我们所在地区的人已经多次回答问题,我们想比较第一次和最近的回答。答案基本上是 1-5 级,但我将其保留为冗长的答案(从完全不足到完全足够)。我想将 "worse" 答案显示为否定,好的答案显示为肯定,并将中间答案 ("Quite Insufficient") 分成两半,这样中点就可以在情节上居中(我是不是有道理吗?!下面有一个示例图链接)。我还想按地区分组,以及这是第一个还是最后一个响应。
如果我绘制 2 个单独的数据框,那么图表看起来不错,但我无法订购图例。
如果我合并数据框,图例看起来不错,但图表出错了!请帮忙!
# 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 方面很糟糕。非常感谢任何帮助。
If I plot the 2 separate dataframes then the chart looks good but I can't order the legend.
If I union the dataframes (just for First in this case) the legend looks good but the chart goes wrong! Argh!
如果您还想要其他调整或东西,请告诉我。
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 附近。
我还将您的 union 命令与 bind_rows
等效命令 (bind_rows(Reviews.First.Pos, Reviews.First.Neg)
) 进行了比较;只是顺序不同。这可能就是改变图表顺序的原因。休息部分应该为您重新排序图表。
我第一次在这里提问,所以请放轻松!正如您将从我的示例代码中看到的那样,我也是 R 的新手(3 个月),所以展示它有点尴尬! 我有一个非常具体的要求,但可能有更好的可视化方法。我们所在地区的人已经多次回答问题,我们想比较第一次和最近的回答。答案基本上是 1-5 级,但我将其保留为冗长的答案(从完全不足到完全足够)。我想将 "worse" 答案显示为否定,好的答案显示为肯定,并将中间答案 ("Quite Insufficient") 分成两半,这样中点就可以在情节上居中(我是不是有道理吗?!下面有一个示例图链接)。我还想按地区分组,以及这是第一个还是最后一个响应。 如果我绘制 2 个单独的数据框,那么图表看起来不错,但我无法订购图例。 如果我合并数据框,图例看起来不错,但图表出错了!请帮忙!
# 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 方面很糟糕。非常感谢任何帮助。
If I plot the 2 separate dataframes then the chart looks good but I can't order the legend.
If I union the dataframes (just for First in this case) the legend looks good but the chart goes wrong! Argh!
如果您还想要其他调整或东西,请告诉我。
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 附近。
我还将您的 union 命令与 bind_rows
等效命令 (bind_rows(Reviews.First.Pos, Reviews.First.Neg)
) 进行了比较;只是顺序不同。这可能就是改变图表顺序的原因。休息部分应该为您重新排序图表。