r - ggplot条形图与条形翻转方向
问题描述
我正在尝试创建一个类似于这张图片中的图表。
您可以看到它们已经翻转了蓝色条的方向,即使它们具有正 x 值。现在,我能够重现该条形图,但条形图的方向相同。是否可以在 ggplot 中使用翻转条和正 x 值创建相同类型的图形?
解决方案
这是一个tidyverse
解决方案
图书馆
library(tidyverse)
数据
df <-
tibble(
y = letters[1:15],
p = runif(15,5,100),
g = as.factor(rep(0:1,c(5,10)))
)
代码
df %>%
#Create auxiliary variable, where for a determined group the percentage become negative
mutate(
p2 = if_else(g == 0, -p,p),
y = fct_reorder(y,p2)
) %>%
ggplot(aes(p2,y, fill = g))+
geom_col()+
scale_x_continuous(
breaks = seq(-100,100,10),
#Make the labels positive
labels = seq(-100,100,10) %>% abs()
)
输出
推荐阅读
- c# - 根据时间变换(Unity & C#)
- javascript - 使用纯 JavaScript 删除不包含在数组中的值的选项
- python - 重采样频率不会在短时间内丢失数据增量
- genexus - 有没有办法知道 For each 何时在genexus X v3 上的第一次和最后一次迭代?
- swift - Firebase 将经过身份验证的用户添加到数据库
- r - 有没有办法在多列上使用 group_by 将密度函数传递到数据框?我需要原始数据框中的 x 和 kde 概率
- kotlin - Webflux 保存到存储库而不使用 .block()
- lotus-domino - SaveOptions 字段没有被兑现..我的解决方案是否正确?
- sql-server - SQL 条件 where 变量
- acumatica - 将文本添加到网格的背面,在屏幕 AP302000 上