我正在創建一個如下所示的圖表。問題是我希望灰色的“總”欄始終位于最右側。

當前代碼如下,任何人都可以修改/提供任何其他代碼來創建這種效果嗎?
#plot with reorder
PrevalencePlot <- ggplot(ICSTable4, aes(x = reorder(value, Area), y = value, fill = Statistical_Significance))
geom_col()
scale_fill_manual(values = colours)
geom_errorbar(aes(ymin=errorbarlowerplot, ymax=errorbarhigherplot),
width=.2, # Width of the error bars
position=position_dodge(.9))
theme_bw()
geom_text(aes(label = valuelabel), vjust = 2.5, colour = "black")
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))
如果有人能夠提供幫助,那么下面的資料框可以用來生成我認為的原則嗎?謝謝!
df <- data.frame(Area = c("Area1", "Area2", "Area3", "Area4", "Total"),
Value = c(1, 3, 7, 5, 4)
)
uj5u.com熱心網友回復:
在最小示例資料的基礎上,我們可以制作一個簡明版本的繪圖,以解決排序值的問題,并將選定的列放在最后。
df <- data.frame(Area = c("Area1", "Area2", "Area3", "Area4", "Total"),
value = c(1, 3, 7, 5, 4),
Statistical_Significance = c("higher", "lower", "lower", "higher", NA))
在繪圖之前創建列的順序更容易,因為我們需要根據順序創建因子value,然后重新定位目標列(“Total”)。
df <- df %>%
dplyr::arrange(desc(value)) %>% #arrange by value
dplyr::mutate(Area = forcats::as_factor(Area)) %>% # factor that defines order on x-axis
dplyr::mutate(Area = forcats::fct_relevel(Area, "Total", after = Inf)) # reposition "Total" column
ggplot(df, aes(x = Area, y = value, fill = Statistical_Significance))
geom_col()
theme_bw()

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/440263.html
上一篇:用日期在r中繪制時間序列圖的問題
