首页 > 解决方案 > 为什么我的 ggplot2 条形图不显示?

问题描述

我正在尝试在 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。对于如何解决这个问题,有任何的建议吗?

标签: rggplot2

解决方案


一个选项是指定x, yin aes, 创建geom_barwithstat为 'identity', 并更改 x 轴刻度标签

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

在此处输入图像描述


推荐阅读