2015-09-10 64 views
1

我一直在试图传说添加到我的ggplot但悲惨地失败了。我没有去通过其他询问其均与添加传说手动如12 但不能适用answerson我ggplot。我尝试了功能 scale_colour_manual,但图例不显示。 任何帮助将不胜感激。添加传说GGPLOT2

p <- ggplot() + 

    # corine plot 
geom_point(data=t, aes(x=FPR, y=TPR),colour="black", size =3,pch=1) + 
geom_line(data=t, aes(x=FPR, y=TPR), 
     colour="lightblue", size=1) + 
    #globecover plot 
geom_point(data=tgl, aes(x=FPR, y=TPR),colour="black",size=3,pch=1) + 
geom_line(data=tgl, aes(x=FPR, y=TPR), 
     colour="red", size=1)+ 
#grump plot 
geom_point(data=tgr, aes(x=FPR, y=TPR),colour="black",size=3, pch=1) + 
geom_line(data=tgr, aes(x=FPR, y=TPR), 
     colour="pink", size=1) 

p <- p+geom_abline(intercept=0, slope=1) 

p<- p+ labs(list(title = "FPR vs TPR", x = "False Positive Rate", y = "True Positive Rate")) 

p <-p+theme_bw() + 
theme(axis.title.x = element_text(size = 15, vjust=-.2)) + 
theme(axis.title.y = element_text(size = 15, vjust=0.3)) 

p+ scale_colour_manual(name="legend", value =c("corine"= "lightblue", "globcover"="red", "grump"="pink")) 

是的,我的DATAS T,TGR,TGL是这样的:

ID Countries FPR  TPR 
1  Bristol 0.08716076 0.6894999  
2  Brussel 0.18621056 0.8065292  
3  Budapest 0.07085285 0.8234692  
4  Edinburgh 0.05507682 0.6944172  
5  Gozo 0.11037915 0.6360882  

等等!

+0

您可以显示一些示例数据?先制作一个大的数据框,然后制作一个情节,让你避免重复一些事情,可能会更容易。 – Heroka

+0

@Heroka是的,我更新了我的问题。我会记住这一点。 – tropicalbath

+0

也许这线程可以帮助[链接](http://stackoverflow.com/a/10349375/709777) – pacomet

回答

1

我写了一个解决方案与第一组合数据,因为它的效率高得多。如果它们全都相同,您也不需要为每个几何设置aes

结合数据:

#add group variable (called data because t is a funtio) 
tt$group <- "corine" 
#make up the other dataframes 
set.seed(1) 
tgl <- data.frame(ID=1:5, Countries=LETTERS[1:5],FPR=runif(5,0,0.12),TPR=runif(5,0.5,0.8)) 
tgl$group <- "globcover" 
tgr <- data.frame(ID=1:5, Countries=LETTERS[1:5],FPR=runif(5,0,0.12),TPR=runif(5,0.5,0.8)) 
tgr$group <- "grump" 

#combine 
all_data <- rbind(tt,tgl,tgr) 

的再绘制组合数据

使情节

p2 <- ggplot(all_data, aes(x=FPR, y=TPR, group=group)) + 
    geom_point(color="black") + #no need for x and y, as unchanged 
    geom_line(aes(color=group)) + 
    scale_colour_manual(values =c("corine"= "lightblue", "globcover"="red", "grump"="pink")) 
p2 

enter image description here

+0

Hi @Heroka,非常感谢! – tropicalbath