我正在尝试在 ggplot2 中制作一个条形图,但是当我添加标签时,它们聚集在一个中间条上,而不是坐在值本身代表的条上。
我正在使用的代码:
synch<-data.frame(Species = c("AMCO","AMCO","AMCO","GADW","GADW","GADW","AGWT","AGWT","AGWT","RNDU","RNDU","RNDU"),
Value=c(93.3,61.1,97.4,0.8,34.9,0.2,0.0,4.0,0.0,0.5,0.0,0.4),
SurveyType=c("A","B","C","A","B","C","A","B","C","A","B","C"))
synch
library(dplyr)
library(ggplot2)
synchAMCO = synch %>% filter(Species == "AMCO")
synchGADW = synch %>% filter(Species == "GADW")
synchAGWT = synch %>% filter(Species == "AGWT")
synchRNDU = synch %>% filter(Species == "RNDU")
synchplot.again<- ggplot(data=synch) +
geom_bar(data = synchAMCO,
mapping = aes(x = Species, y = Value, fill = SurveyType),
stat='identity', position = position_dodge(width = 0.75), width=0.7) + theme_bw() +
scale_fill_manual(name = "Survey Type",
labels = c("UAV Pre-plane","Plane","UAV Post-plane"),
values = c("darkblue","red","lightblue"),
aesthetics = "fill") +
theme(legend.text = element_text(size = 10, family = "Arial", colour = "black")) +
new_scale_fill() +
geom_bar(data = synchGADW,
mapping = aes(x = Species, y = Value, fill = SurveyType),
stat='identity', position = position_dodge(width = 0.75), width=0.7,show.legend = FALSE) +
scale_fill_manual(name = "GADW",
labels = c("UAV Pre-plane","Plane","UAV Post-plane"),
values = c("darkblue","red","lightblue"),
aesthetics = "fill")+
new_scale_fill() +
geom_bar(data = synchAGWT,
mapping = aes(x = Species, y = Value, fill = SurveyType),
stat='identity', position = position_dodge(width = 0.75), width=0.7,show.legend = FALSE) +
scale_fill_manual(name = "AGWT",
labels = c("UAV Pre-plane","Plane","UAV Post-plane"),
values = c("darkblue","red","lightblue"),
aesthetics = "fill")+
new_scale_fill() +
geom_bar(data = synchRNDU,
mapping = aes(x = Species, y = Value, fill = SurveyType),
stat='identity', position = position_dodge(width = 0.75), width=0.7,show.legend = FALSE) +
scale_fill_manual(name = "RNDU",
labels = c("UAV Pre-plane","Plane","UAV Post-plane"),
values = c("darkblue","red","lightblue"),
aesthetics = "fill") +
geom_text(position = position_dodge(width = 0.75),
aes(x = Species, y = Value+2, label = Value))+
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
text = element_text(size = 16,
family = "Arial"))+
labs(y="Proportion of Count") +
ggtitle("Species Proportion of Total Survey Count by Survey Type -
Swan Creek Dewatering Unit, Late Season") +
theme(legend.title = element_text(size = 12, family = "Arial")) +
theme(plot.title = element_text(size = 15, family = "Arial"))
synchplot.again
这是结果图:
我只是希望标签位于各自的栏上。
我已经多次看到这个问题被问和回答,并且我已经尝试了每个不同答案中的代码。我有这三个问题链接如下:
这些都讨论了调整 geom_text 创建的标签位置的不同方法,但它们都不适合我。 有什么建议么? 预先感谢您。
您需要将
group = SurveyType
添加到 aes()
geom_text()
以实现文本标签的正确放置。
此外,您可以简化代码,因为您不需要引入 带有
ggnewscale::new_scale_fill()
的新音阶,您无需准备
synch
data.frame
的子集。
library(dplyr)
library(ggplot2)
ggplot(data = synch) +
geom_bar(
mapping = aes(x = Species, y = Value, fill = SurveyType),
stat = 'identity',
position = position_dodge(width = 0.75),
width = 0.7
) + theme_bw() +
scale_fill_manual(
name = "Survey Type",
labels = c("UAV Pre-plane", "Plane", "UAV Post-plane"),
values = c("darkblue", "red", "lightblue"),
aesthetics = "fill"
) +
geom_text(aes(
x = Species,
y = Value + 2,
label = Value,
group = SurveyType
),
position = position_dodge(width = 0.75)) +
theme(
legend.title = element_text(size = 12, family = "Arial"),
legend.text = element_text(
size = 10,
family = "Arial",
colour = "black"
),
plot.title = element_text(hjust = 0.5),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
text = element_text(size = 16,
family = "Arial")
) +
labs(y = "Proportion of Count") +
ggtitle(
"Species Proportion of Total Survey Count by Survey Type -
Swan Creek Dewatering Unit, Late Season"
)