[英]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.