【发布时间】:2020-03-09 22:54:16
【问题描述】:
我正在尝试使用 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))
由reprex package (v0.3.0) 于 2019 年 11 月 14 日创建
【问题讨论】:
-
用
labels = function(x) percent(abs(x))之类的东西替换labels = percent能实现您的目标吗? -
这实际上非常有效。非常感谢。比我想象的要容易得多。