R 错误:ggplot2不';我不知道如何处理gg/ggplot类的数据
您好,我已经使用R大约一年了,但只是在工作之外的业余时间,在尝试在ggplot上创建图形时遇到以下错误:R 错误:ggplot2不';我不知道如何处理gg/ggplot类的数据,r,ggplot2,graph,maps,R,Ggplot2,Graph,Maps,您好,我已经使用R大约一年了,但只是在工作之外的业余时间,在尝试在ggplot上创建图形时遇到以下错误: Error: ggplot2 doesn't know how to deal with data of class gg/ggplot 我在尝试创建下面3个图表中的任何一个时看到错误消息 BookingData %>% ggplot() %>% geom_bar(mapping = aes(x = Destination)) BookingData %>% ggplo
Error: ggplot2 doesn't know how to deal with data of class gg/ggplot
我在尝试创建下面3个图表中的任何一个时看到错误消息
BookingData %>%
ggplot() %>%
geom_bar(mapping = aes(x = Destination))
BookingData %>%
ggplot() %>%
geom_bar(mapping = aes(x = Cost))
BookingData %>%
ggplot() %>%
geom_point(mapping = aes(x = Destination, y = Cost))
我正在分析的数据是:
str(BookingData)
'data.frame': 8583 obs. of 7 variables:
$ Person.URN : int 4087 39748 294410 366031 692418 841419 1018069 46055
253036 484387 ...
$ Booking.URN : int 9298 79548 548230 697854 1314354 1594476 1930719 91905
472923 921033 ...
$ Destination : Factor w/ 15 levels "Australia","Denmark",..: 4 4 11 3 15 5
1 1 4 15 ...
$ Continent : Factor w/ 5 levels "Africa","Americas",..: 5 5 5 5 2 5 4 4 5
2 ...
$ Product : Factor w/ 3 levels "Accommodation Only",..: 3 3 3 3 2 3 3 2
3 3 ...
$ Cost : int 629 1047 454 798 676 1073 482 587 1217 532 ...
$ Booking.Date: Date, format: "2009-09-19" "2009-09-19" ...
如果您能提供有关我为什么看到此错误消息以及如何更正此错误消息的任何帮助,我们将不胜感激。由于我无法复制您的数据,因此我使用的是
mtcars
数据集。
您的主要错误是使用管道(%>%)而不是+。
此外,我更喜欢将aes()
(x\y轴)放在ggplot()
下,而不是放在另一个参数下
例如:
require(tidyverse)
df <- mtcars
df %>%
ggplot(aes(x = factor(vs))) +
geom_bar()
df %>%
ggplot(aes(x = mpg, y = disp)) +
geom_point()
require(tidyverse)
df%
ggplot(aes(x=因子(vs)))+
geom_bar()
df%>%
ggplot(aes(x=mpg,y=disp))+
几何点()
你可以寻找更多的解释和例子 ggplot不接受%>%。请尝试BookingData%>%ggplot()+geom_Bar欢迎使用SO。尽管@A.Suliman的建议很可能会解决问题,但一般来说,提供一些小样本数据以重现您的问题是一种良好的礼仪。如果您不想创建示例数据帧,那么我建议使用例如dput()而不是str()。噢,天哪,我知道这将是我自己的一个荒谬错误。非常感谢非常感谢通常是这样的;)。不客气,我很高兴能帮上忙。@FluffySheep1990我知道这不关我的事。当您提出问题时,我通常同意在ggplot()调用中创建映射。但是,如果要基于多个数据创建更复杂的绘图。。。在这种情况下,在每个plot-call中创建映射非常有用。。说这句话——这是唯一的办法……;)