簡體   English   中英

ggplot2和ggiraph之間的堆疊條形圖順序不同

[英]Order of stacked bar charts differ between ggplot2 and 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"), 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"))

幾個附加的位:

#Value colours
Labels_Colours <- c("Satisfied" = "#59A14F", "Neutral" = "#76B7B2", "Dissatisfied" = "#F28E2B", "DK/NA" = "#BAB0AC")

#Tooltip feature
tooltip_css <- "background-color:white;font-style:italic;padding:10px;border-radius:10px 20px 10px 20px;"

ggplot2的圖表如下所示。 該圖表正是我希望我的ggiraph圖表顯示的樣子:

library(ggplot2)
library(dplyr)
mydf %>% filter(prop > 0 | is.na(prop)) %>%
ggplot(aes(x = Label, y = ifelse(is.na(prop), 0, prop), fill = Rating)) +
geom_col() +
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進行最終產品的嘗試。 此處代碼的唯一區別是,我使用的是geom_bar_interactive而不是geom_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")

在此處輸入圖片說明

“ DK / NA”欄以某種方式與“不滿意”欄切換了位置,但是我的百分比標簽和圖例仍保留在正確的位置。 是什么原因造成的? 我認為這與Rating系數中等級的順序有關,但是它們似乎順序正確。 我很迷惑

這是關於分組。 美學tooltipdata_idgeom_bar_interactive (或在geom_bar ,效果相同)使從分組的分組不同geom_text

[ http://ggplot2.tidyverse.org/reference/aes_group_order.html]默認情況下,該組設置為繪圖中所有離散變量的相互作用。 這通常可以正確地對數據進行分區,但是當不對數據進行分區時,或者在圖中未使用任何離散變量時,您需要通過將組映射到每個組具有不同值的變量來顯式定義分組結構。

定義組美學解決了這個問題(在主要aesgeom_bar中 ):

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

在此處輸入圖片說明

正如@DavidGohel指出的,這是一個錯誤,我發現以下行為可以解決:

library(forcats)
mydf <- arrange(mydf, Rating)
mydf$tooltip <- fct_inorder(mydf$tooltip)

現在,再次運行ggiraph代碼:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM