【问题标题】:Error: Data is a dataframe but R gives an error错误:数据是数据帧,但 R 给出错误
【发布时间】:2021-05-07 09:53:42
【问题描述】:

我尝试为特定变量制作 ggplot。当我使用此代码时,它不起作用,但是当我对不同的变量使用相同的函数时,它起作用了......:

waardevol <- ggplot(data=etadam, aes(x = waardevol)) + 
  geom_bar(aes(y = (..count..)/sum(..count..)), colour = "black", width = 0.6, fill = '#ffd308') + geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))), stat = "count", vjust = -0,25) + 
  scale_y_continuous(labels = percent) + 
  labs(y = 'percentage', title = 'Vonden jullie de training waardevol?', subtitle = 'N = 400', caption = 'Leren voor de Toekomst©') 


Data (etadam):
 ID                tijd                       gevoel waardevol        vak niveau vertrouwen
1 90 2021-04-25 15:16:11        Vrolijk;Gemotiveerd ;        Ja     Engels   HAVO         Ja
2 91 2021-04-25 15:17:08 Relaxt;Gemotiveerd ;Vrolijk;        Ja     Engels   HAVO         Ja
3 92 2021-04-25 15:49:16                    Neutraal;        Ja Wiskunde A   HAVO         Ja
4 93 2021-04-25 15:49:00              Trots;Gezegend;        Ja Wiskunde A   HAVO         Ja
5 94 2021-04-25 15:49:03                    Neutraal;        Ja Wiskunde A   HAVO        Nee
6 95 2021-04-25 15:49:23                    Neutraal;        Ja Wiskunde A   HAVO         Ja
uitleg                                       oefenexamen aanraden           vervolgopleiding
1 Zeer goed                                               6.1       Ja                        Nee
2 Zeer goed 4,8 maar ik heb slecht geslapen daar ligt het aan       Ja Bouwkunde HVA architectuur
3 Zeer goed                                               6,5       Ja             Cyber security
4   Normaal                                               5.2       Ja          Ja, verpleegkunde
5 Kan beter                                               5.7      Nee                        Nee
6      Goed                                                 -       Ja                        Nee

This Code acctually did work: 

uitleg <- ggplot(data=etadam, aes(x = uitleg)) + 
    geom_bar(aes(y = (..count..)/sum(..count..)), colour = "black", width = 0.6, fill = '#ffd308') +
    geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))), stat = "count", vjust = 5) + 
    scale_y_continuous(labels = percent) + 
    labs(x = NULL, y = 'percentage', title = 'Wat vonden jullie van de uitleg? - Alle trainingen', subtitle = 'N = 400', caption = 'Leren voor de Toekomst')


After I run the copied the last quote and run it a couple of times, the same error arises.

【问题讨论】:

    标签: r dataframe ggplot2 error-handling


    【解决方案1】:

    使用class()函数来确认它们是否是同一类型的变量。

    【讨论】:

    • 都是因素
    • 错误:data 必须是数据框,或其他可被fortify() 强制转换的对象,而不是数字向量
    • 嗨,这个问题看起来和你的问题很相似:stackoverflow.com/questions/54050320/…。由于我没有您的数据集,恐怕我无法在我的计算机中复制您的数据和代码。但我相信这应该不是什么大问题,也许再试一次data &lt;- as.data.frame(data)。顺便说一句,您是否通过 tidyverse 加载数据?
    • 您好,它仍然无法正常工作。奇怪的是,is.data.frame(etadam) 原来是 TRUE。所以 R 将我的数据识别为数据框,但是当我尝试使用 ggplot2 时,错误代码仍然存在....
    • 我解决了这个问题。问题在 vjust 中说明,其中“-”不是正确的符号!感谢您的帮助!
    猜你喜欢
    • 2018-04-18
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多