我设法用geom_bar制作了一个漂亮的ggplot,但图例未显示。 基本上,我显示的是19个候选人中每个候选人2分之间的差异,这些候选人也是4个不同组的一部分(信息在我的df内部,称为“ GS4Gnew”)。 我手动选择了条形的颜色,但我希望它是该组的相对颜色。这就是我想在图例中显示的内容(每个组的每种颜色)。
我认为我的代码存在问题,因为我的审美是在geom_bar中而不是在ggplot中,我认为这就是为什么没有图例显示的原因...但是我不知道如何解决... 谢谢您的帮助。
ggplot(GS4Gnew) +
geom_bar(GS4Gnew, mapping=aes(x=Initiales, y=difference4G, colour=Groupe),
stat = "identity",
fill=c("lightblue","red","red","pink","pink","pink","pink","pink","pink",
"green","green","green","green","lightblue","lightblue","red","red","red","red"),
color="black") +
scale_y_continuous(breaks=c(-5,5,15),limits=c(-6,25), expand=c(0,0)) +
theme_minimal() +
labs(x="Participant",y="Différence entre les deux scores",
title = "Modification du score global entre les deux évaluations") +
theme(panel.grid.major.x=element_blank(),
panel.grid.major.y=element_line(colour="darkgrey"),
plot.title=element_text(size=rel(1.5), face="bold",vjust=1.5),
axis.title.y=element_text(face="bold",vjust=1.8),
axis.title.x=element_text(face="bold",vjust=-0.8))
答案 0 :(得分:0)
您必须将填充的aes
映射更改为fill=Groupe
,并在scale_fill_manual
内提供填充颜色值。下面的代码应该可以工作(没有数据示例就很难尝试)。如果不是,请尝试将fill=Groupe
更改为fill=factor(Groupe)
(以防万一)。
ggplot(GS4Gnew, aes(x = Initiales, y = difference4G, fill = Groupe)) +
geom_bar(stat = "identity", colour = "black") +
scale_y_continuous(breaks = c(-5,5,15), limits = c(-6,25), expand = c(0,0)) +
scale_fill_manual(values=c("lightblue", "red", "red", "pink", "pink", "pink",
"pink", "pink", "pink", "green", "green", "green",
"green", "lightblue", "lightblue", "red", "red",
"red", "red")) +
labs(x = "Participant", y = "Différence entre les deux scores",
title = "Modification du score global entre les deux évaluations") +
theme_minimal() +
theme(panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(colour="darkgrey"),
plot.title = element_text(size=rel(1.5), face="bold", vjust=1.5),
axis.title.y = element_text(face="bold", vjust=1.8),
axis.title.x = element_text(face="bold", vjust=-0.8))