r-ggplot中未识别对象



我正在尝试制作一个条形图,并将每个类别的百分比添加到其中。然而,当我尝试运行代码时,我会收到以下错误消息:

Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomLabel,  : 
object 'share' not found

这是我的代码:

base3 %>%
group_by(country.x) %>% 
summarise(total=sum(cost)) %>%
mutate(countrygroup=sapply(country.x,cathegorize), countries=sapply(country.x,reverse_cathegorize), share=total/sum(total)) %>%
ggplot() +
geom_bar(aes(x=reorder(countries,total),y=total,fill=countrygroup),colour = 'black', alpha = 0.5,stat="identity")+
geom_label(aes(x=reorder(countries,total),y=share),label = paste0(round((share), 3)*100, '%'))

几个潜在原因:

  1. 您的变量share没有在mutate()中正确创建;当您只运行第1-4行时(因此,在ggplot()之前(,您的数据集是什么样子的
  2. 当您调用round((share), 3)时,ggplot并不知道您在谈论一个变量,因为它在aes()之外;尝试round((.$share), 3)

最新更新