creating a line graph to display total pounds of each type in a specific year

You get that error your x-variable is a category and you can’t join them into a line. I guess you would need a bar plot (I flip the plot so that the types can be read, you can remove coord_flip() if you don’t need that) :

fluid_milk_sales %>%
filter(year == 2017) %>%
ggplot(aes(x = reorder(milk_type,pounds), y = pounds)) +
geom_col() + xlab("milk_type") + coord_flip() 

enter image description here

Or if you want like a lollipop plot, it goes like:

fluid_milk_sales %>%
filter(year == 2017) %>%
ggplot(aes(x = reorder(milk_type,pounds), y = pounds)) +
geom_point() +
geom_segment(aes(xend = milk_type, yend = 0)) + 
coord_flip() + xlab("milk_type")

enter image description here

If you really want to force a line, which I think doesn’t make sense (note I reorder with the negative to start with the highest):

fluid_milk_sales %>%
filter(year == 2017) %>%
ggplot(aes(x = reorder(milk_type,-pounds), y = pounds,group=1)) +
geom_line() + xlab("milk_type")

enter image description here

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top