我正在try 将总数添加到堆叠条形图中,在该条形图中我描述了比例,其中x和y变量都是多个响应变量.尽管收到了警告消息,我还是设法对总数进行了计算,但当我试图将总数添加到比例条形图中时,它完全搞砸了.
这是我的DF hnvf3的摘录:
structure(list(HNV_grasslands = c(1L, 0L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), HNV_arable = c(1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L,
1L, 1L, 1L), HNV_permanent_crop = c(1L, 1L, 1L, 0L, 0L, 1L, 1L,
1L, 1L, 1L, 1L), HNV_mosaic = c(1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L,
1L, 0L, 1L), Animal_vegetal = c("", "Both", "Both", "Both", "",
"Vegetal species", "Vegetal species", "", "Both", "Animal species",
"Both")), row.names = 10:20, class = "data.frame")
这是我用来在Counts版本中添加总数的代码,尽管收到了警告:
在统计转换过程中删除了以下美学:fill当ggplot无法推断数据中正确的分组 struct 时,可能会发生这种情况.您是否忘记指定
group
美学或将数值变量转换为因子?
我把总数放在最上面.
hnvf4 <- hnvf3 %>%
pivot_longer(
cols = starts_with("HNV"),
names_to = c("HNV"),
names_prefix = "HNV_",
values_to = "value1") |>
filter(value1 == 1 & Animal_vegetal !="") |>
select(-value1) # pone esto para que hnvf2 no sea todo "1" en todas las filas, sino el valor de provisioning, etc
glimpse(hnvf4)
hnvf4 |>
ggplot(aes(x = HNV,fill = factor(Animal_vegetal, levels=c("Animal species", "Vegetal species", "Both")))) +
geom_bar()+
geom_text(stat ="count",aes(label=after_stat(count), group= HNV, vjust= -1))+
scale_fill_manual(values = c("#FDCDAC", "#B3E2CD","#CBD5E8"))+
theme(axis.text.x = element_text(angle=90, vjust = 0.5, hjust=1),
axis.ticks.x = element_blank())+
theme(axis.line.y = element_line(linewidth = 0.5, colour = "black"),
axis.ticks.length.y.left = unit(.25, "cm"))+
theme(panel.background = element_rect(fill=NA))+
scale_x_discrete(labels=c('arable land', 'grasslands', 'mosaic landscape',
'permanent croplands')) +
labs(x= "HNV farmland type", y="Number of studies", fill= "")+
#geom_text(data=stacked, aes(label=nsum, variable),size=2, vjust= 1)+
theme(axis.title.y = element_text(margin=margin(r= 15)))+
scale_y_continuous(limits= c(0,100), breaks=c(0,25,50,75, 100),expand = c(0,0)) +
theme(legend.position = c(.99, .95),
legend.justification = c("right", "top"))
这是对比例不起作用的编码:
hnvf4%>%
ggplot(aes(x = HNV,fill = factor(Animal_vegetal,levels=c("Animal species", "Vegetal species", "Both")))) +
geom_bar(position="fill")+
#geom_text(stat ="count",aes(label=after_stat(count), group= HNV, vjust= -1))+
scale_fill_manual(values = c("#FDCDAC", "#B3E2CD","#CBD5E8"))+
theme(axis.text.x = element_text(angle=90, vjust = 0.5, hjust=1),
axis.ticks.x = element_blank())+
theme(axis.line.y = element_line(linewidth = 0.5, colour = "black"),
axis.ticks.length.y.left = unit(.25, "cm"))+
theme(panel.background = element_rect(fill=NA))+
scale_x_discrete(labels=c('arable land', 'grasslands', 'mosaic landscape',
'permanent croplands')) +
labs(x= "HNV farmland type", y="Proportion of studies", fill= "")+
#geom_text(data=stacked, aes(label=nsum, variable),size=2, vjust= 1)+
theme(axis.title.y = element_text(margin=margin(r= 15)))+
# para que no haya espacio extra y que el eje y salga de 0
scale_y_continuous(expand = c(0,0))
我还判断了Stackflow中的以下条目:question1、question2、question3、text,但我发现很难为geom_text()
创建新的数据框,因为我没有显示变量计数的变量.我try 修改我的数据并 for each 变量创建一个带有计数的新df,但仍然不起作用.任何帮助都将不胜感激.