错误:ggplot2不知道如何处理类gg / ggplot的数据

时间:2018-05-19 13:52:34

标签: 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 %>%
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" ...

您可以提供有关我为什么会看到此错误消息以及如何更正错误消息的任何帮助,我们将不胜感激。

1 个答案:

答案 0 :(得分:2)

因为我无法重现您的数据,所以我正在使用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()

您可以查看here以获取更多解释和示例。