9.18 Position adjustments

par(mar = c(4, 4, .1, .1))

ggplot(diamonds, aes(x = cut, color = cut)) + 
  geom_bar()

ggplot(diamonds, aes(x = cut, fill = cut)) + 
  geom_bar()

ggplot(diamonds, aes(x = cut, fill = clarity)) + 
  geom_bar()

  • The stacking is performed automatically using the position adjustment specified by the position argument. If you don’t want a stacked bar chart, you can use one of three other options: “identity”, “dodge” or “fill”.

  • N/B: position = “identity” will place each object exactly where it falls in the context of the graph. This is not very useful for bars, because it overlaps them. To see that overlapping we either need to make the bars slightly transparent by setting alpha to a small value, or completely transparent by setting fill = NA.

par(mar = c(4, 4, .1, .1))

ggplot(diamonds, aes(x = cut, fill = clarity))+ 
geom_bar(alpha = 1/5, position = "identity")

ggplot(diamonds, aes(x = cut, color = clarity))+ 
geom_bar(fill = NA, position = "identity")

  • Avoiding over-plotting
ggplot(mpg, aes(x = displ, y = hwy)) + 
  geom_point(position = "jitter")