ggplot2 图例没有出现在图表中?

ggplot2 legend does not take place in graph?

这是我的代码:

modele <- ggplot(data = liste_ref, aes(x = BV, y = liste_ref[,p]))+
    geom_point(aes(text = paste("Code opération: ",OPE_ID), colour = "FRANCE"),size = 1)+
    geom_point(data = liste_ref_HER,aes(y = liste_ref_HER[,p],text = paste("Code operation: ",OPE_ID),colour = "HER"),size=1)+
    geom_point(data = select,aes(y = select[,p],text = paste("Code operation: ",OPE_ID), colour = "Selection"),size=1)+
    scale_y_log10() + scale_x_log10()+
    stat_smooth(aes(x = BV, y = liste_ref[,p]), method = "lm",formula = y ~ x, se = FALSE,size = 0.25,fullrange = TRUE,color = "#BABBBF")+
    stat_smooth(data = liste_ref_HER, aes(x = BV, y = liste_ref_HER[,p]), method = "lm",formula = y ~ x, se = FALSE,size = 0.25,fullrange = TRUE, color = "#2F61F5")+
    scale_color_manual(name = "Legend", values = c("FRANCE"="#BABBBF","HER"="#2F61F5","Selection"="red"))+
    ggtitle(paste("Relation entre la surface du bassin versant et ",pl," de  l'HER",HER[,1]))+
    xlab("Surface du bassin versant (en km²)") + ylab(yl)   

ggplotly(modele)

结果是:

传说不够space出现...请帮帮我!

感谢您的回答,抱歉您可以运行此代码:

liste_ref <- as.data.frame(cbind(c(3,4,13,16,17),c(19.62,391.21,9.57,3.16,57.53,142.50),c(6.62,13,4,3,9.2,12.27))) 
colnames(liste_ref)=c("OPE_ID","BV","Moy_Lm_Qb")
liste_ref_HER <- as.data.frame(cbind(c(56,58,87,94,97,98),c(51.13,155.9,8.2,36.9,9.49,770.54),c(4.8,3.2,17,5.6,3.4,6)))
colnames(liste_ref_HER)=c("OPE_ID","BV","Moy_Lm_Qb")                
select <- as.data.frame(cbind(54,51.13,8))                                             
colnames(select)=c("OPE_ID","BV","Moy_Lm_Qb")                    
ggplot(data = liste_ref, aes(x = BV, y = Moy_Lm_Qb))+
geom_point(aes(text = paste("Code opération: ",OPE_ID), colour = "FRANCE"),size = 1)+
geom_point(data = liste_ref_HER,aes(y = Moy_Lm_Qb,text = paste("Code operation: ",OPE_ID),colour = "HER"),size=1)+
geom_point(data = select,aes(y = Moy_Lm_Qb,text = paste("Code operation: ",OPE_ID), colour = "Selection"),size=1)+
scale_y_log10() + scale_x_log10()+
stat_smooth(aes(x = BV, y = Moy_Lm_Qb), method = "lm",formula = y ~ x, se = FALSE,size = 0.25,fullrange = TRUE,color = "#BABBBF")+
stat_smooth(data = liste_ref_HER, aes(x = BV, y = Moy_Lm_Qb), method = "lm",formula = y ~ x, se = FALSE,size = 0.25,fullrange = TRUE, color = "#2F61F5")+
scale_color_manual(name = "Legend", values = c("FRANCE"="#BABBBF","HER"="#2F61F5","Selection"="red"))+
ggtitle("Relation entre la surface du bassin versant et l'HER")
ggplotly()

你说得对,问题出在 plotly 包上!如果你只是 运行 ggplot 它工作完美,但使用 ggplotly() 传奇被删除......

这是一个替代方案。这对你有用吗?

#create a new dataset with a new column with 3 factor levels:
liste_ref$Test <- "liste_ref"
liste_ref_HER$Test <- "liste_ref_HER"
select$Test <- "select"
FinalData <- rbind(liste_ref, liste_ref_HER, select)


ggplot(data = FinalData, aes(x = BV, y = Moy_Lm_Qb, color=Test))+
geom_point(size = 1) + scale_y_log10() + scale_x_log10() + 
stat_smooth(data = FinalData, aes(x = BV, y = Moy_Lm_Qb, color=Test), method = "lm",formula = y ~ x, se = FALSE,size = 0.25,fullrange = TRUE) + 
scale_color_manual(name = "Legend", values = c("liste_ref"="#BABBBF","liste_ref_HER"="#2F61F5","select"="red")) +
ggtitle("Relation entre la surface du bassin versant et l'HER")

#adjust margin
m = list( l = 100, r = 100, b = 50, t = 50, pad = 0)
ggplotly() %>% layout(autosize = F, width = 1200, height = 600, margin = m)