2015-09-10 62 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