ggplot: geom_polygon - Error in length of Aestetics

266 views Asked by At

I want to draw a scatterplot with three polygons in the background colouring specific areas. It should look like this. The code works fine with 4 variables on the y axis, but throws and error when I add a fifth. I can't figure out the reason.

The Error:

Error: Aesthetics must be either length 1 or the same as the data (5): x, y

The reproducable code:

library(dplyr)
library(ggplot2)

v1 <- c(1, 1, 1)
v2 <- c(1, 0, 0)
v3 <- c(1, 0, 1)
v4 <- c(0, 1, 1)
v5 <- c(1, 0, 1)

xG <- c(3, 3, 3, 3, 3)

input <- c(v1, v2, v3, v4, v5)
df <- data_frame(values = input, 
             module = c(rep("A", length(v1)), 
                        rep("B", length(v2)), 
                        rep("C", length(v3)), 
                        rep("D", length(v4)), 
                        rep("E", length(v5))))

perWorkField <- df %>%
  group_by(module) %>%
  summarise(sums = sum(values)) %>%
  mutate(percent = round((sums / xG) * 100, 2))

ggplot(data = perWorkField) + 
  geom_point(mapping = aes(x = percent, y = module)) + xlim(c(0, 100)) + 
  geom_polygon(aes(x = c(0, 30, 30, 0), y = c(0.5, 0.5, 5.5, 5.5)), 
           fill="#F5817A", 
           color = NA) +
  geom_polygon(aes(x = c(30, 75, 75, 30), y = c(0.5, 0.5, 5.5, 5.5)), 
           fill="#FFFF0044", 
           color = NA) +
  geom_polygon(aes(x = c(75, 100, 100, 75), y = c(0.5, 0.5, 5.5, 5.5)), 
           fill="#00FF0044", 
           color = NA) + 
  geom_point(mapping = aes(x = percent, y = module), shape = 20, size = 5) + theme_classic()

I appreciate any advice. Thanks in advance!

2

There are 2 answers

2
Patrik_P On BEST ANSWER

You can try this to make it work, I replaced the data argument in order to make it work properly

ggplot() + 
  geom_point(data = perWorkField, mapping = aes(x = percent, y = module)) + xlim(c(0, 100)) + 
  geom_polygon(aes(x = c(0, 30, 30, 0), y = c(0.5, 0.5, 5.5, 5.5)), 
               fill="#F5817A", 
               color = NA) +
  geom_polygon(aes(x = c(30, 75, 75, 30), y = c(0.5, 0.5, 5.5, 5.5)), 
               fill="#FFFF0044", 
               color = NA) +
  geom_polygon(aes(x = c(75, 100, 100, 75), y = c(0.5, 0.5, 5.5, 5.5)), 
               fill="#00FF0044", 
               color = NA) + 
  geom_point(data = perWorkField, mapping = aes(x = percent, y = module), shape = 20, size = 5) + theme_classic()
1
Z.Lin On

Since you're creating rectangular shaded regions, geom_rect() may be easier to read / maintain than geom_polygon().

In addition, annotate() can be used to avoid the problem with inherited aesthetics from the data frame:

ggplot(data = perWorkField) + 
  geom_point(aes(x = percent, y = module)) +
  annotate("rect", xmin =  0, xmax =  30, ymin = 0.5, ymax = 5.5, fill = "#F5817A", color = NA) +
  annotate("rect", xmin = 30, xmax =  75, ymin = 0.5, ymax = 5.5, fill = "#FFFF0044", color = NA) +
  annotate("rect", xmin = 75, xmax = 100, ymin = 0.5, ymax = 5.5, fill = "#00FF0044", color = NA) +
  geom_point(aes(x = percent, y = module), shape = 20, size = 5) +
  theme_classic()