Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Visualize your data 1
Visualize your data 2
Work with your chart
9. Rotate your stacked bar chart
Check yourself


Stacked bar charts compare different areas by the height of their representative segments. If you rotate your chart so that the bars are on the horizontal axis, you will usually get more space for them. Each bar will be longer, and the smaller segments will be much easier to read.

To rotate a chart in ggplot2, add coord_flip() to your chart. It looks like this:

plot + coord_flip()

To make more space for the bars, change the position of the legend:

+ theme(legend.position = "top")

The legend will be shown at the top of the chart. To ensure the colors in the legend are shown in the same order as they are on the bars, use this command:

+ guides(fill = guide_legend(reverse = T))


Rotate your chart by adding the coord_flip() command to the stacked object. Change legend position by adding theme(legend.position = "top"). Change the order of colors in the legend by adding guides(fill = guide_legend(reverse = T)).

When you're done, press Run and Check Code to check your code.

Stuck? Here's a hint!

You should write:

  coord_flip() +
  theme(legend.position = "top") + 
  guides(fill = guide_legend(reverse = T))