ggplot2:结合定位命令position_nudge和position_stack

弗洛

我想制作一个带有堆积条形图的图,并向右移动50%,以便刻度位于条形图之间。

library(tidyverse)

df <- tribble(~Station, ~Direction1, ~Direction2,
             "A", 432, 485,
             "V", 543, 345,
             "B", 435, 457,
             "Q", 0, 0)

df$Station <- factor(df$Station, levels=unique(df$Station))

df = gather(df, Direction1, Direction2, key = "Direction", value = "Passengers")

第一张图显示了所需的堆积条形图:

ggplot(data = df, aes(x = Station, y = Passengers, fill = Direction)) + 
  geom_bar(stat = "identity", position = position_stack())

第二个图将条形图移动到刻度线之间的中间位置,但条形图现在彼此之间绘制在一起:

ggplot(data = df, aes(x = Station, y = Passengers, fill = Direction)) + 
  geom_bar(stat = "identity", position = position_nudge(x = 0.5))

如何结合两个定位命令?

林正

以下应满足您的目的。您不必指定的位置,如果你想position_stack(),因为它是在默认位置参数geom_bar/ geom_col

(顺便说一下,您可能要考虑从切换geom_bargeom_col,因为stat = "identity"默认情况下后者使用。键入较少。)

ggplot(data = df, 
       aes(x = as.numeric(Station) + 0.5, y = Passengers, fill = Direction)) +
  geom_col() +
  scale_x_continuous(name = "Station",
                     limits = range(as.numeric(df$Station)),
                     breaks = unique(sort(as.numeric(df$Station))),
                     labels = levels(df$Station))

情节

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

是否可以在ggplot2的堆叠条形图中组合position_stack和nudge_x?

如何使用position_nudge将x轴标签与ggplot2中条的中心对齐

ggplot2 :: position_stack中的距离如何缩放?

ggplot2 position_stack(vjust) 使用绝对单位而不是比例

在ggplot2中结合position_dodge和position_fill

使用正值和负值控制 ggplot::geom_bar position_stack 在图形和图例中的填充颜色顺序

ggplot2 | geom_bar和position =“ identity”

堆叠条形图中的position_stack和百分比标签的反转

position_stack 不适用于 facet_wrap

具有position_stack的抖动文本/标签

结合 facet_grid (ggplot2) 和 denscomp (fitdistrplus)

ggplot2与rworldmap结合

ggplot2 position_dodge影响误差线的宽度

当使用position =“ dodge”时,ggplot2 barplot中断

将 nudge_y 添加到“抖动”R ggplot2 图的数据标签

如何在分面 ggplot2 中自动定位 R2 和 RMSE?

使用ggplot2结合facet_wrap和95%的密度图面积

ggplot2在geom_raster中结合了连续变量和离散变量

创建结合了主题和颜色的自定义ggplot2函数

将 ggplot2 与 R 中的基本图相结合:外观和语法

R // ggplot2:结合facet_wrap和for循环时的动态标题

使用ggraph / ggplot2在网络图中定位节点和边

ggplot2:带有 geom_bar(position = 'fill') 的堆叠条形图中的值标签

ggplot2:无法更改position ='right'的axis.title.y角度

使用facet_wrap()时无法控制ggplot2中的legend.position

jQuery定位与position()

子集和ggplot2

将 nudge_y 添加到“抖动”R ggplot2 图的数据标签 - 分类值

如何在bash中结合使用getopts和position参数?