堆叠条形图的顺序在ggplot2和ggiraph之间有所不同

堆叠条形图的顺序在ggplot2和ggiraph之间有所不同,r,ggplot2,ggiraph,R,Ggplot2,Ggiraph,我正在构建一个闪亮的应用程序,它准备了一个带有工具提示的堆叠条形图,显示了3个项目和2种类型的受访者的满意度水平。以下是构成图表的数据: mydf <- structure(list(Resp = structure(c(1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L), .Label = c("Resp_A", "Resp_B"), c

我正在构建一个闪亮的应用程序,它准备了一个带有工具提示的堆叠条形图,显示了3个项目和2种类型的受访者的满意度水平。以下是构成图表的数据:

mydf <- structure(list(Resp = structure(c(1L, 1L, 2L, 1L, 2L, 1L, 1L, 
2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L), .Label = c("Resp_A", 
"Resp_B"), class = "factor"), Rating = structure(c(4L, 4L, 4L, 
4L, 4L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
1L), .Label = c("DK/NA", "Dissatisfied", "Neutral", "Satisfied"
), class = "factor"), Question = structure(c(1L, 2L, 2L, 3L, 
3L, 1L, 2L, 2L, 3L, 3L, 1L, 2L, 2L, 3L, 3L, 1L, 2L, 2L, 3L, 3L
), .Label = c("q14", "q12", "q11"), class = "factor"), count = c(29, 
27, 11, 25, 12, 9, 12, 3, 9, 5, 3, 2, 11, 5, 8, 4, 4, 8, 6, 8
), prop = c(0.644444444444444, 0.6, 0.333333333333333, 0.555555555555556, 
0.363636363636364, 0.2, 0.266666666666667, 0.0909090909090909, 
0.2, 0.151515151515152, 0.0666666666666667, 0.0444444444444444, 
0.333333333333333, 0.111111111111111, 0.242424242424242, 0.0888888888888889, 
0.0888888888888889, 0.242424242424242, 0.133333333333333, 0.242424242424242
), Label = structure(c(1L, 2L, 2L, 3L, 3L, 1L, 2L, 2L, 3L, 3L, 
1L, 2L, 2L, 3L, 3L, 1L, 2L, 2L, 3L, 3L), .Label = c("Label_1", 
"Label_2", "Label_3"), class = "factor"), tooltip = c("<b>Satisfied</b>\n<b>Question:</b> Question_Text_1\n<b>n=</b> 29\n<b>Percentage:</b> 64%", 
"<b>Satisfied</b>\n<b>Question:</b> Question_Text_2\n<b>n=</b> 27\n<b>Percentage:</b> 60%", 
"<b>Satisfied</b>\n<b>Question:</b> Question_Text_2\n<b>n=</b> 11\n<b>Percentage:</b> 33%", 
"<b>Satisfied</b>\n<b>Question:</b> Question_Text_3\n<b>n=</b> 25\n<b>Percentage:</b> 56%", 
"<b>Satisfied</b>\n<b>Question:</b> Question_Text_3\n<b>n=</b> 12\n<b>Percentage:</b> 36%", 
"<b>Neutral</b>\n<b>Question:</b> Question_Text_1\n<b>n=</b> 9\n<b>Percentage:</b> 20%", 
"<b>Neutral</b>\n<b>Question:</b> Question_Text_2\n<b>n=</b> 12\n<b>Percentage:</b> 27%", 
"<b>Neutral</b>\n<b>Question:</b> Question_Text_2\n<b>n=</b> 3\n<b>Percentage:</b> 9%", 
"<b>Neutral</b>\n<b>Question:</b> Question_Text_3\n<b>n=</b> 9\n<b>Percentage:</b> 20%", 
"<b>Neutral</b>\n<b>Question:</b> Question_Text_3\n<b>n=</b> 5\n<b>Percentage:</b> 15%", 
"<b>Dissatisfied</b>\n<b>Question:</b> Question_Text_1\n<b>n=</b> 3\n<b>Percentage:</b> 7%", 
"<b>Dissatisfied</b>\n<b>Question:</b> Question_Text_2\n<b>n=</b> 2\n<b>Percentage:</b> 4%", 
"<b>Dissatisfied</b>\n<b>Question:</b> Question_Text_2\n<b>n=</b> 11\n<b>Percentage:</b> 33%", 
"<b>Dissatisfied</b>\n<b>Question:</b> Question_Text_3\n<b>n=</b> 5\n<b>Percentage:</b> 11%", 
"<b>Dissatisfied</b>\n<b>Question:</b> Question_Text_3\n<b>n=</b> 8\n<b>Percentage:</b> 24%", 
"<b>DK/NA</b>\n<b>Question:</b> Question_Text_1\n<b>n=</b> 4\n<b>Percentage:</b> 9%", 
"<b>DK/NA</b>\n<b>Question:</b> Question_Text_2\n<b>n=</b> 4\n<b>Percentage:</b> 9%", 
"<b>DK/NA</b>\n<b>Question:</b> Question_Text_2\n<b>n=</b> 8\n<b>Percentage:</b> 24%", 
"<b>DK/NA</b>\n<b>Question:</b> Question_Text_3\n<b>n=</b> 6\n<b>Percentage:</b> 13%", 
"<b>DK/NA</b>\n<b>Question:</b> Question_Text_3\n<b>n=</b> 8\n<b>Percentage:</b> 24%"
), Ord = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 
4, 4, 4), Question_Text = structure(c(3L, 1L, 1L, 2L, 2L, 3L, 
1L, 1L, 2L, 2L, 3L, 1L, 1L, 2L, 2L, 3L, 1L, 1L, 2L, 2L), .Label = c("Question_Text_2", 
"Question_Text_3", "Question_Text_1"), class = "factor")), row.names = c(NA, 
-20L), .Names = c("Resp", "Rating", "Question", "count", "prop", 
"Label", "tooltip", "Ord", "Question_Text"), class = c("tbl_df", 
"tbl", "data.frame"))

现在,我尝试使用
ggiraph
制作最终产品。这里代码的唯一区别是我使用的是
geom\u bar\u interactive
,而不是
geom\u col
,当然还有最后一行:

library(ggiraph)
mychart <- mydf %>% filter(prop > 0 | is.na(prop)) %>%
ggplot(aes(x = Label, y = ifelse(is.na(prop), 0, prop), fill = Rating)) +
geom_bar_interactive(aes(tooltip = tooltip, data_id = Rating), stat = "identity") +
geom_text(aes(label = ifelse(is.na(prop), prop, percent(round(prop, 2)))), position = position_stack(vjust = 0.5), size = 3) +
scale_fill_manual(values = Labels_Colours) +
coord_flip() +
facet_grid(~ Resp, drop = FALSE) +
labs(x = "", y = "") +
theme_minimal() +
theme(
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
)
ggiraph(code = print(mychart), width = 1, point = 4, width_svg = 8, height_svg = 2, tooltip_extra_css = tooltip_css, hover_css = "opacity:0.5")
库(ggiraph)
mychart%过滤器(属性>0 |为.na(属性))%>%
ggplot(aes(x=标签,y=ifelse(is.na(属性),0,属性),填充=评级))+
geom_bar_交互式(aes(工具提示=工具提示,数据id=评级),stat=“identity”)+
几何图形文本(aes(标签=ifelse(is.na(prop),prop,百分比(round(prop,2))),位置=位置堆栈(vjust=0.5),大小=3)+
比例\填充\手册(值=标签\颜色)+
coord_flip()+
面网格(~Resp,drop=FALSE)+
实验室(x=”,y=)+
主题_极小值()+
主题(
axis.text.x=元素_blank(),
axis.ticks.x=元素_blank(),
panel.grid.major=元素_blank(),
panel.grid.minor=元素_blank()
)
ggiraph(代码=打印(mychart),宽度=1,点=4,宽度=8,高度=2,工具提示\u额外\u css=工具提示\u css,悬停\u css=“不透明度:0.5”)


“DK/NA”条不知怎么地与“不满意”条交换了位置,但我的百分比标签和图例保留在正确的位置。这是什么原因造成的?我假设这与
评级
因子中的级别顺序有关,但它们似乎顺序正确。我很困惑。

正如@DavidGohel指出的,这是一个bug,我发现以下方法可以作为解决方法:

library(forcats)
mydf <- arrange(mydf, Rating)
mydf$tooltip <- fct_inorder(mydf$tooltip)
库(用于猫)

mydf是关于分组的。在
geom\u bar\u interactive
(或
geom\u bar
,效果相同)中的
tooltip
data\u id
使分组不同于
geom\u文本的分组

[默认情况下,该组设置为绘图中所有离散变量的交互作用。这通常会正确地划分数据,但如果不正确,或绘图中未使用离散变量,则需要通过将组映射到每个组具有不同值的变量来显式定义分组结构

定义组美学解决了这个问题(在主aes或geom_栏中):

mychart%过滤器(prop>0 | is.na(prop))%>%
ggplot(aes(x=标签,y=ifelse(is.na(prop),0,prop),fill=评级,group=评级))+
geom_bar_interactive(映射=aes(工具提示=工具提示,数据\u id=评级),stat=“identity”)+
几何图形文本(aes(标签=ifelse(is.na(prop),prop,百分比(round(prop,2))),位置=位置堆栈(vjust=0.5),大小=3)+
比例\填充\手册(值=标签\颜色)+
coord_flip()+
面网格(~Resp,drop=FALSE)+
主题_极小值()
ggiraph(代码=打印(mychart),宽度=1,点=4,宽度=8,高度=2,工具提示\u额外\u css=工具提示\u css,悬停\u css=“不透明度:0.5”)

ooops,看起来像一个严重的bug。谢谢()@DavidGohel我已经找到了一个解决方法,您可能会发现它在调试中很有用。我将把它作为一个答案发布。谢谢,现在我也很困惑。
library(ggiraph)
mychart <- mydf %>% filter(prop > 0 | is.na(prop)) %>%
ggplot(aes(x = Label, y = ifelse(is.na(prop), 0, prop), fill = Rating)) +
geom_bar_interactive(aes(tooltip = tooltip, data_id = Rating), stat = "identity") +
geom_text(aes(label = ifelse(is.na(prop), prop, percent(round(prop, 2)))), position = position_stack(vjust = 0.5), size = 3) +
scale_fill_manual(values = Labels_Colours) +
coord_flip() +
facet_grid(~ Resp, drop = FALSE) +
labs(x = "", y = "") +
theme_minimal() +
theme(
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
)
ggiraph(code = print(mychart), width = 1, point = 4, width_svg = 8, height_svg = 2, tooltip_extra_css = tooltip_css, hover_css = "opacity:0.5")
library(forcats)
mydf <- arrange(mydf, Rating)
mydf$tooltip <- fct_inorder(mydf$tooltip)
mychart <- mydf %>% filter(prop > 0 | is.na(prop)) %>%
  ggplot(aes(x = Label, y = ifelse(is.na(prop), 0, prop), fill = Rating, group = Rating)) +
  geom_bar_interactive(mapping = aes(tooltip = tooltip, data_id = Rating), stat = "identity") +
  geom_text(aes(label = ifelse(is.na(prop), prop, percent(round(prop, 2)))), position = position_stack(vjust = 0.5), size = 3) +
  scale_fill_manual(values = Labels_Colours) +
  coord_flip() +
  facet_grid(~ Resp, drop = FALSE) +
  theme_minimal() 
ggiraph(code = print(mychart), width = 1, point = 4, width_svg = 8, height_svg = 2, tooltip_extra_css = tooltip_css, hover_css = "opacity:0.5")