我想在R中用ggplot制作一个情节,但它给了我一个值

时间:2017-04-02 04:28:30

标签: r ggplot2

我试图用ggplot2制作一个条形图。这是数据集(错误应该在x,而cantidad在y):

Error should be in the x, and cantidad as y

当我运行时:

grafico <- ggplot(tipoerror, aes(x = Error, y = Cantidad))

我在情节部分没有情节。我在环境中有了新的价值。

你能帮助我看看我做错了什么吗?

2 个答案:

答案 0 :(得分:1)

有几个小/初学者错误(其中大多数已经在其他评论或答案中提到)加起来:

  1. 在致电aes时,参数y = Cantidad使用大写C写入,而图片显示标题使用小写c
  2. 创建的ggplot对象缺少一个图层,即ggplot不知道要绘制的内容
  3. ggplot对象grafico已创建但未打印。这就是为什么新对象出现在RStudio的 Environment 窗格中,但该图未显示在 Plots 窗格中。
  4. 请尝试:

    library(ggplot2)
    grafico <- ggplot(tipoerror, aes(x = Error, y = cantidad)) + 
      geom_col()
    grafico
    

    生成此图表:

    enter image description here

    数据

    由于OP未能以可重现的格式提供数据,我不得不编制一些样本数据

    tipoerror <- data.frame(
      Error = c(NA, LETTERS[1:9]),
      cantidad = c(309, 149, 91, 80, 79, 42, 39, 22,  5,  3)
    )
    

    其中字母替换错误消息。

答案 1 :(得分:0)

如果您的数据已经汇总,则需要添加stat =&#34; identity&#34;。如评论中所述,您还需要图层。在这种情况下,geom_bar。

library(ggplot)
er <- data.frame(A = c("John", "Erik", "David"), Count = c(39, 49, 68))
er
ggplot(er, aes(x = A, y = Count)) + geom_bar(stat = "identity")
相关问题