繁体   English   中英

标签位于 R 条形图中的条形下方

[英]The labels are below the bars in the R barplot

我试图将标签放在情节上的所有条形上,但其中一些一直在条形下。 我应该在代码中编辑什么? 在此处输入图片说明

ggplot(data, aes(x = year,y = value)) + 
  geom_text(aes(label=value),  vjust=-3.5, size=3.5)+
  geom_bar(aes(fill = variable), stat = "identity",position = "dodge")+
  scale_x_continuous(breaks = unique(data$year))+
  ylab("Number of candidates")+
  theme(axis.title.x=element_blank())+
  scale_fill_discrete(name="",
                      labels=c("All", "Female"))

您需要将position_dodge添加到geom_text以跟随 geom_bar 的 position_dodge。

在这里,我以使用pivot_longer重塑的iris数据集pivot_longer

library(tidyverse)
ir_df <- iris %>% group_by(Species) %>% 
  summarise(Mean_Length = mean(Sepal.Length), Mean_Width = mean(Sepal.Width)) %>% 
  pivot_longer(., -Species, names_to = "Variables", values_to = "Value")

library(ggplot2)
ggplot(ir_df, aes(x = Species, y = Value, fill = Variables))+
  geom_bar(stat = "identity", position = position_dodge()) +
  geom_text(aes(label = Value), vjust = -3.5, position = position_dodge(width = 1))

在此处输入图片说明

如果您无法成功地将此代码改编为您的数据集,请考虑提供您的数据集的可重现示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM