ggplot显示为“美学”的错误,长度必须为1或与数据(5)相同:

时间:2019-01-18 11:10:53

标签: r ggplot2

从“ https://www.r-graph-gallery.com/297-circular-barplot-with-groups/”绘制圆形条形图时,我们遇到以下错误:

  

错误:美学的长度必须为1或与数据(5)相同:正好

     

此外:警告消息:

     

1:删除了362个包含缺失值的行(position_stack)。

     

2:删除了362个包含缺失值的行(position_stack)。

大约有500条显示,数据列的最大值为1397

#Get the name and the y position of each label
label_data=data
number_of_bar=nrow(label_data)
angle= 90 - 360 * (label_data$id-0.5) /number_of_bar     
label_data$hjust<-ifelse( angle < -90, 1, 0)
label_data$angle<-ifelse(angle < -90, angle+180, angle)

# prepare a data frame for base lines
base_data=data %>% 
  group_by(project) %>% 
  summarize(start=min(id), end=max(id) - empty_bar) %>% 
  rowwise() %>% 
  mutate(title=mean(c(start, end)))

# prepare a data frame for grid (scales)
grid_data = base_data
grid_data$end = grid_data$end[ c( nrow(grid_data), 1:nrow(grid_data)-1)] + 1
grid_data$start = grid_data$start - 1
grid_data=grid_data[-1,]

#Get the Base Factor of the max
AverageFactor <- MaxOFAverage/5

# Make the plot
p = ggplot(data, aes(x=as.factor(id), y=AverageTimeForTestCase, fill=project)) +       

  geom_bar(aes(x=as.factor(id), y=AverageTimeForTestCase, fill=project), stat="identity", alpha=0.5) +


  geom_segment(data=grid_data, aes(x = end, y = AverageFactor*4, xend = start, yend = AverageFactor*4), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
  geom_segment(data=grid_data, aes(x = end, y = AverageFactor*3, xend = start, yend = AverageFactor*3), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
  geom_segment(data=grid_data, aes(x = end, y = AverageFactor*2, xend = start, yend = AverageFactor*2), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
  geom_segment(data=grid_data, aes(x = end, y = AverageFactor, xend = start, yend = AverageFactor), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +


  annotate("text", x = rep(max(data$id),4), y = c(AverageFactor, AverageFactor*2, AverageFactor*3, AverageFactor*4), label = c(AverageFactor, AverageFactor*2, AverageFactor*3, AverageFactor*4) , color="grey", size=3 , angle=0, fontface="bold", hjust=0) +

  geom_bar(aes(x=as.factor(id), y=AverageTimeForTestCase, fill=project), 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=AverageTimeForTestCase+10, label=testcase, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$angle, inherit.aes = FALSE ) +


  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=project), hjust=c(1,1,0,0), colour = "black", alpha=0.8, size=4, fontface="bold", inherit.aes = FALSE)

p

0 个答案:

没有答案