2016-04-27 119 views
1

这里我的代码:ggplot2图例不在图中发生?

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) 

而且它的结果是: enter image description here

的传说并没有足够多的空间出现......请大家帮帮我!

+1

有你ggplot代码几件事情需要改进,但首先,你似乎是创建具有'ggplotly'情节;那是什么包,你确定问题不在那里?我们如何能够帮助不能重现问题? – joran

+0

您能否提供样本数据,以便我们可以运行您的代码?这会让你更容易帮助你。例如,粘贴'dput(liste_ref [1:10,])'的输出以提供十行数据。它不需要是一个大样本,但它需要是与代码一起使用时再现问题的数据。 – eipi10

+0

要回显@joran,只需在命令行运行'modele'即可。 –

回答

1

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

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

enter image description here

+0

完美!谢谢MLavoie! –

+0

很好的回答。有一点需要注意,'autosize'也可以是'TRUE',并且你的保证金技巧仍然有效。 – timelyportfolio

0

谢谢您的回答,对不起,你可以运行此代码:

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()图例被删除...