【问题标题】:Why is my ggplot2 bar graph not displaying?为什么我的 ggplot2 条形图不显示?
【发布时间】:2021-01-10 22:25:59
【问题描述】:

我正在尝试在 ggplot2 中绘制条形图并遇到问题。

从变量开始

PalList <- c(9, 9009, 906609, 99000099)
PalList1 <- as_tibble(PalList)
Index <- c(1,2,3,4)
PalPlotList <- cbind(Index, PalList)
PPL <- as_tibble(PalPlotList)

并加载 tidyverse library(tidyverse),我尝试像这样绘制:

PPL %>%
  ggplot(aes(x=PalList)) +
  geom_bar()

无论我访问的是 PPL 还是 PalList 都没有关系,我仍然会得到这个结果(轴和标签可能会改变,但图表区域不会改变):

即使这样仍然给出了一个空白的情节,只是现在在经典样式中:

ggplot(PalList1, aes(value)) +
  geom_bar() +
  theme_classic()

如果我尝试barplot(PalList),我会得到预期的结果。但我想要控制ggplot。有关如何解决此问题的任何建议?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    一个选项是在aes 中指定xy,创建geom_barstat 作为“身份”,并更改x 轴刻度标签

    library(ggplot2)   
    ggplot(PPL, aes(x = Index, y = PalList)) + 
        geom_bar(stat = 'identity') + 
         scale_x_continuous(breaks = Index, labels = PalList)
    

    【讨论】:

    • 谢谢!知道为什么我不能让它在 PalList1 上工作吗?
    • @DavidMVermillion barplot 可以采用命名向量或公式,甚至是没有任何名称的向量。在 ggplot 中,应提供 x 和 y
    猜你喜欢
    • 2017-08-10
    • 2015-12-13
    • 1970-01-01
    • 2022-08-12
    • 1970-01-01
    • 2016-01-14
    • 2022-11-11
    • 1970-01-01
    • 2021-05-26
    相关资源
    最近更新 更多