Home > Enterprise >  Circular barplot from r-graph-gallery with error: “Aesthetics must be either length 1...”
Circular barplot from r-graph-gallery with error: “Aesthetics must be either length 1...”

Time:05-29

I'm trying to reproduce a complex circular bar chart described in this tutorial (enter image description here

CodePudding user response:

The issue is that in the last geom_text which adds the group labels you pass a vector of length 4 to hjust. However, as you have 8 groups you have to pass a vector of length 8. Additionally I simplified you code a bit by using lapply to add your segment layers:

ggplot(data, aes(x = as.factor(id), y = value, fill = group))   # Note that id is a factor. If x is numeric, there is some space between the first bar
  geom_bar(aes(x = as.factor(id), y = value, fill = group), stat = "identity", alpha = 0.5)  
  # Add a val=100/75/50/25 lines. I do it at the beginning to make sur barplots are OVER it.
  lapply(seq(8), function(x) {
    geom_segment(data = grid_data, aes(x = end, y = 10 * x, xend = start, yend = 10 * x), colour = "grey", alpha = 1, size = 0.3, inherit.aes = FALSE)  
  })  
  # Add text showing the value of each 100/75/50/25 lines
  annotate("text", x = rep(max(data$id), 8), y = c(10, 20, 30, 40, 50, 60, 70, 80), label = c("1", "2", "3", "4", "5", "6", "7", "8"), color = "grey", size = 3, angle = 0, fontface = "bold", hjust = 1)  
  geom_bar(aes(x = as.factor(id), y = value, fill = group), stat = "identity", alpha = 0.5)  
  ylim(-100, 120)  
  theme_minimal()  
  theme(
    legend.position = "none",
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    plot.margin = unit(rep(-1, 4), "cm")
  )  
  coord_polar()  
  geom_text(data = label_data, aes(x = id, y = value   10, label = individual, hjust = hjust), color = "black", fontface = "bold", alpha = 0.6, size = 2.5, angle = label_data$angle, inherit.aes = FALSE)  
  # Add base line information
  geom_segment(data = base_data, aes(x = start, y = -5, xend = end, yend = -5), colour = "black", alpha = 0.8, size = 0.6, inherit.aes = FALSE)  
  geom_text(data = base_data, aes(x = title, y = -18, label = group), hjust = c(1, 1, 1, 0, 0, 0, 0, 0), 
            colour = "black", alpha = 0.8, size = 4, fontface = "bold", inherit.aes = FALSE)

enter image description here

  • Related