plot a trapezoid in ggplot

Viewed 282

I'd like to plot a trapezoid, like this (expertly drawn in Paint): enter image description here

When I define the corners and plot though

positions <- data.frame(
  id = rep("glass", each = 4),
  x = c(3.5, 6.5, 2, 8),
  y = c(1, 1, 8, 8),
  fill = rep("a")
)

library(ggplot2)
ggplot(positions, aes(x = x, y = y)) +
  geom_polygon(aes(fill = fill, group = id))

I get this. How do I define connections between the corners? enter image description here

1 Answers

The points are connected in the order of the rows. Simply change the order of the last two points:

library(ggplot2)
ggplot(positions[c(1,2,4,3),], aes(x = x, y = y)) +
  geom_polygon(aes(fill = fill, group = id))

enter image description here

Related