why my code is wrong summarise () and mean()

Your bar chart reveals the locations that produce the highest rated chocolate bars. To get a better idea of the specific rating for each location, you’d like to highlight each bar.

Assume that you are working with the code chunk:

ggplot(data = best_trimmed_flavors_df) +

geom_bar(mapping = aes(x = Company.Location))

Add a code chunk to the second line of code to map the aesthetic color to the variable Rating**.**

NOTE: the three dots (...) indicate where to add the code chunk.

my code :-geom_bar(mapping = aes(x = Company.Location, color = Company.Location))


According to your bar chart, which two company locations produce the highest rated chocolate bars?

Scotland and France

Amsterdam and U.S.A.

Canada and France

Canada and U.S.A.

                                                                                                              *

Umm.. Are you sure these are the instructions? I find it weird that your professor asked you to map color, rather than fill. and I would suspect that the y axis should be mapped to the mean rating for each location, not the color..

Does this make sense to you?

best_trimmed_flavors_df <- tibble(
  Company.Location = sample(state.abb[1:5],20,TRUE),
  rating = sample(1:5,20,TRUE)
)

best_trimmed_flavors_df %>% 
  summarise(rating = mean(rating), .by = Company.Location) %>% 
  
ggplot()+geom_bar(aes(x = fct_reorder(Company.Location,-rating),
                      y = rating,
                      fill = Company.Location),
                  stat = "identity")

I changed a little bit the code your professor asked.

The fct_reorder() simply makes sure that the X axis categories are sorted by their value in y, you can remove it and see what happens.
stat = 'identity' is instead of using geom_col which i actualy prefer.
.by = in summarise is a new thing R guys are experimenting and I think its great. Usualy you would have to have a group_by before, and then you'd have grouped data. this is explicit and simple

1 Like

Thank you so much for the detailed explanation.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.