【问题标题】:geom_col() with values close to each other in ggplotgeom_col() 在 ggplot 中的值彼此接近
【发布时间】:2020-04-06 10:24:16
【问题描述】:

我在函数geom_col() 中遇到了问题。如果我的 x 值相距足够大,它似乎工作正常,但如果它们“接近”,我的情节不会按预期显示。请参见下面的示例。

在类似的现有issue中,建议手动设置width,但这并不能解决问题。

此外,人们应该期望第一个和第二个情节以相同的方式工作。但是在第二个图中,默认设置似乎改变了轴

这是一个错误吗?还是我错过了一个选项?

# library(ggplot2)

# plot that does work
df <- data.frame(x = c(1,2,3,5,6), y = c(45,45,45,45,45))
ggplot(df, aes(x = x, y = y)) + geom_col()

# plot that does not work (only different numbers)
df <- data.frame(x = c(0.9,0.91,0.92,1.0,1.01), y = c(45,45,45,45,45))
ggplot(df, aes(x = x, y = y)) + geom_col()

# plot that still does not work, with manual setting of width (you can try different options)
ggplot(df, aes(x = x, y = y)) + geom_col(width = 0.05)

# plot showing that there are at least 5 seperate bars, only I expect y to be the height of the bar and x to be the groups
ggplot(df, aes(x = x, y = y)) + geom_col(position = 'dodge2', colour = "black")

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    所以您缺少的选项是orientation,它有助于指定要运行的轴。 geom_col 猜测正确的方向,而在您的第二个 df 中,它猜测沿 y 轴运行,因此我们需要指定改为沿 x 轴运行。

    df <- data.frame(x = c(0.9,0.91,0.92,1.0,1.01), y = c(45,45,45,45,45))
    ggplot(df, aes(x = x, y = y)) + geom_col(orientation = 'x')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-05
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2016-10-07
      相关资源
      最近更新 更多