【发布时间】:2026-02-15 00:15:01
【问题描述】:
我用ggplot 创建了这个散点图,正如你所见,我没有设法为该图创建相应的图例:
- 正方形应为深灰色,其文本应为“Méthode par transects”
- 三角形应为浅灰色,其文本应为“Methode par sous-transects”。
如果可能的话,我也希望将虚线“Seuil”的图例靠近这些点。
这是我的代码:
ggplot(tst_formule, aes(x=nom_graph, y = value, shape = BRI_type, col = factor(BRI_type))) +
geom_point(size = 4) +
scale_shape_manual("", values = c( 15, 17)) +
scale_colour_manual(values=c("grey20", "gray54"), # légende
name ="Légende",
breaks=c("BRI_adi_moy_guide", "BRI_adi_moy_Sandrine"),
labels=c("Méthode par transects", "Méthode par sous-transects")) +
geom_text(aes(label = value, vjust = -0.5, hjust= -0.1), show.legend = FALSE) + # etiquettes
geom_hline(aes(yintercept = 0.004, linetype = "Seuil"), colour= 'black') +
scale_linetype_manual("", values = c(2), guide = guide_legend(override.aes = list(color = c("black")))) +
scale_x_discrete("\nTronçons\n") +
scale_y_continuous("\nValeur du BRI*\n", limits = c(0,0.025)) +
theme(axis.text.x = element_text(size = 11),
axis.text.y = element_text(size = 11),
legend.text = element_text(size = 11),
plot.margin = unit(c(0.2,0.2,0.2,0.2), "cm"))
我阅读了几篇文章并尝试了不同的解决方案,但它不起作用,我还是 ggplot 的新手,我认为我的语法可能与图例不符......
这是我的数据的dput():
structure(list(Riviere = c("Durance", "Durance", "Roya", "Drac",
"Drac", "Durance", "Durance", "Roya", "Drac", "Drac"), Troncon = c("La Brillanne",
"Les Mées", "Basse vallée", "St Bonnet", "St Bonnet", "La Brillanne",
"Les Mées", "Basse vallée", "St Bonnet", "St Bonnet"), Annee = c(2017,
2017, 2018, 2011, 2018, 2017, 2017, 2018, 2011, 2018), nom_graph = c("La Brillane 2017 \nDurance",
"Les Mées 2017 \nDurance", "Roya 2018", "St Bonnet 2011 \nDrac",
"St Bonnet 2018 \nDrac", "La Brillane 2017 \nDurance", "Les Mées 2017 \nDurance",
"Roya 2018", "St Bonnet 2011 \nDrac", "St Bonnet 2018 \nDrac"
), BRI_type = c("BRI_adi_moy_Sandrine", "BRI_adi_moy_Sandrine",
"BRI_adi_moy_Sandrine", "BRI_adi_moy_Sandrine", "BRI_adi_moy_Sandrine",
"BRI_adi_moy_guide", "BRI_adi_moy_guide", "BRI_adi_moy_guide",
"BRI_adi_moy_guide", "BRI_adi_moy_guide"), value = c(0.0037,
0.0024, 0.0013, 0.0239, 0.0038, 0.0028, 0.0017, 0.0009, 0.02,
0.0031)), row.names = c(NA, -10L), class = "data.frame")
欢迎任何帮助!
【问题讨论】: