Reputation: 441
Hello I've been using R for around a year now but only in my spare time outside of work and I've encountered the following error whilst trying to create a graph on ggplot:
Error: ggplot2 doesn't know how to deal with data of class gg/ggplot
I see the error message when trying to create any of the 3 graphs below.
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))
The data I'm analysing is:
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" ...
Any help you can provide on why I'm seeing this error message and how I can correct it would be much appreciated.
Upvotes: 1
Views: 1417
Reputation: 4873
Because I cannot reproduce your data, I'm using mtcars
dataset.
Your main mistake was the use of pipe (%>%) instead of + .
In addition, I prefer to put the aes()
(x\y axis) under the ggplot()
and not under the other argument.
For example:
require(tidyverse)
df <- mtcars
df %>%
ggplot(aes(x = factor(vs))) +
geom_bar()
df %>%
ggplot(aes(x = mpg, y = disp)) +
geom_point()
You can look here for more explanations and examples.
Upvotes: 2