Rednaxel
Rednaxel

Reputation: 968

ggplot2 colors scale issue with Aesthetics

I'm trying to make a chart of the frequency of the items in a dataset but I'm having this error when ploting.

Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

Error: Aesthetics must be either length 1 or the same as the data (39123): x

This is the model of the data I'm using:

  product_id  order_id                  product_name
1   41899       330425    Oreo  Ice Cream Sandwiches
2  122580      1707573                     Mint Chip
3  146891       622568              Coffee Ice Cream
4  134292      1284843 Belgian Milk Chocolate Gelato
5  146530      2693694   Variety Pack Ice Cream Bars

The str output of the dataframe is (top 5 values):

'data.frame':   5 obs. of  3 variables:
 $ product_id  : int  41899 122580 146891 134292 146530
 $ order_id    : int  330425 1707573 622568 1284843 2693694
 $ product_name: Factor w/ 49688 levels "'Swingtop' Premium Lager",..: 28573 25871 10030 4236 47274

I've tried several changes to the plot code, but I got different errors.

This is the code I'm using.

orders_group <- group_by(orders_products,order_id)
orders_summ <- as.data.frame(summarise(orders_group, n_items = count(product_name)))

ggplot(orders_summ,aes(x=n_items))+
  geom_histogram(stat="count")+#geom_histogram(fill="indianred", bins = 100000) + 
  geom_rug()+
  coord_cartesian(xlim=c(0,80))+
  scale_fill_manual(values = getPalette(colourCount))

Upvotes: 1

Views: 115

Answers (1)

Marcus Campbell
Marcus Campbell

Reputation: 2796

I believe this stems from the fact that you are using count() incorrectly. count() produces a tibble, which will react strangely when inside the dataframe resulting from your call to summarise.

I have to run, but at first glance it looks like you created a column of dataframes (or something like that), which would explain your ggplot error. I believe what you are looking for is this:

orders_summ <- orders_products %>%
      group_by(order_id) %>% # normally this step would have produced your orders_group
      summarise(n_items = n())

Then try and run your ggplot code.

Upvotes: 1

Related Questions