我正在尝试使用 gpplot 在堆叠图中绘制正数和负数。根据我在此页面上找到的示例,这工作正常。
我的图表的限制是 -1 和 1,但我希望比例尺将标签显示为绝对百分比,即从左侧的 100% 到中心的 0% 到右侧的 100%。
下面的最小示例说明我可以获得百分比刻度标签 ( labels = percent) 或绝对刻度 ( labels = abs),但我不知道如何组合它们。
提前致谢。
library(tidyverse)
library(scales)
x <- tribble(
~response, ~count,
"a", -0.2,
"b", -0.1,
"c", 0.5,
"d", 0.2
)
p <- ggplot() +
geom_bar(data = x,
aes(x = "", y = count, fill = response),
position = "stack",
stat = "identity") +
coord_flip()
# Percent scale
p + scale_y_continuous(labels = percent, limits = c(-1, 1), expand = c(0.05, 0))
# Absolute scale
p + scale_y_continuous(labels = abs, limits = c(-1, 1), expand = c(0.05, 0))
Run Code Online (Sandbox Code Playgroud)
由reprex 包(v0.3.0)于 2019 年 11 月 14 日创建
Gra*_*ray -1
位置从堆叠变为闪避。这导致在显示不同计数值的同时将变量清晰地分离为零。
p <- ggplot() +
geom_bar(data = x %>% filter(count < 0),
aes(x = "", y = count, fill = response),
position = "dodge",
stat = "identity") +
geom_bar(data = x %>% filter(count >= 0),
aes(x = "", y = count, fill = response),
position = "dodge", stat = "identity") +
coord_flip()
# Percent scale
p + scale_y_continuous(labels = percent, limits = c(-1, 1), expand = c(0.05, 0))
# Absolute scale
p + scale_y_continuous(labels = abs, limits = c(-1, 1), expand = c(0.05, 0))
Run Code Online (Sandbox Code Playgroud)
输出显示在下面的某处。
很棒的绘图示例。谢谢,
| 归档时间: |
|
| 查看次数: |
588 次 |
| 最近记录: |