使用此表:
Groups variable value
1 G1 VAR1 75.0
2 G1 VAR2 37.5
3 G2 VAR1 50.0
4 G2 VAR2 12.5
和這段代碼:
color_list <- c("VAR1"="blue","VAR2"="red")
tab %>%
mutate(Groups = fct_reorder(Groups, value)) %>%
ggplot( aes(x=value, y=Groups,fill=factor(variable, levels = c("VAR1","VAR2")),label=Groups))
scale_fill_manual(values = color_list[unique(tab$variable)], guide = guide_legend(order = 1)) theme_minimal()
我設法得到下圖:

但這是我的期望:

正如您所看到的,它是我需要的嵌套直方圖而不是累積直方圖表示。
有誰知道如何使用 ggplot2 做到這一點?
如果有幫助,這里是 dput 格式的表資料:
structure(list(Groups = c("G1", "G1", "G2", "G2"), variable = c("VAR1",
"VAR2", "VAR1", "VAR2"), value = c(75, 37.5, 50, 12.5)), class = "data.frame", row.names = c(NA,
-4L))
uj5u.com熱心網友回復:
你可以嘗試這樣的事情,混合你的代碼、dario 給出的提示和其他東西:
library(ggplot2)
library(dplyr)
library(forcats)
color_list <- c("VAR1"="blue","VAR2"="red")
tab %>%
mutate(Groups = fct_reorder(Groups, value)) %>%
ggplot( aes(x = Groups, y = value,
fill = factor(variable, levels = c("VAR1","VAR2")),
group = variable,
label = value))
# here you overlap your bars
geom_bar (stat="identity", position = position_dodge(width = 0))
# here custom colors
scale_fill_manual(values = color_list[unique(tab$variable)],
guide = guide_legend(order = 1))
# y (x) axis as you've pointed out, values and 30 by 30 labels, you can
# customize it of course
scale_y_continuous("value", labels =c(seq(from = 0, to = 90, by = 30),tab$value), breaks = c(seq(from = 0, to = 90, by = 30),tab$value))
# some frills
theme_minimal()
coord_flip()
labs(fill = "title")

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/315899.html
