简体   繁体   中英

ggplot2 legend does not take place in graph?

Here my code :

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)

And it result :在此处输入图片说明

The legend doesn't have enought space to appear... Please help me !

Here is an alternative. Would this work for you?

#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)

在此处输入图片说明

Thanks for your answer, sorry you can run this code :

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()

You're right the problem is going from plotly packages ! If you just run ggplot it works perfect, but with ggplotly() the legend is removed...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM