【发布时间】:2018-10-25 07:39:45
【问题描述】:
我正在尝试创建以下数据框的条形图:
df <- structure(list(HCT_range = c("Low", "Low", "Low", "Low", "Low",
"Low"), HG1_range = c("High", "High", "Normal", "High", "High",
"Normal"), HG2_range = c("High", "Normal", "High", "High", "Normal",
"High"), n = c(17L, 54L, 66L, 15L, 61L, 60L), Type = c("LHH",
"LHN", "LNH", "LHH", "LHN", "LNH"), File = c("a", "a", "a", "b",
"b", "b"), Perc = c(0.0387, 0.123, 0.1503, 0.0342, 0.1389, 0.1366
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))
我正在尝试使用以下命令创建一个闪避的条形图:
ggplot(data = df, aes(x = Type, y = n)) +
geom_col(aes(fill = File), position = position_dodge(width = 0.9)) +
geom_text(aes(label = Perc), position = position_dodge(0.9))
但由于某种原因,测试并没有被躲避而是堆叠 - 尽管有特定的位置调用。
有什么想法吗?
【问题讨论】:
-
将
groupaes添加到geom_text或ggplot,如帮助文本的几个示例?geom_text中所述。在ggplot中将fill = File移动到aes也可以。 -
我添加了:ggplot(data = DF, aes(x = Type, y = n)) + geom_col(aes(fill = File), position = position_dodge(width = 0.9)) + geom_text (aes(label = Perc, group = 1), position = position_dodge(0.9)) - 它没有改变任何东西