2017-04-19 117 views
0

我有兩個不同的變量數據集圖標符號 - 城鎮標籤影響ggplot

當我繪製出來,並與stat_smooth添加兩個迴歸線,傳說符號不正確顯示(它們會作爲兩個「一的):

ggplot(metrics, aes(x=popDensity, y= TPB, color = factor(type))) + geom_point() +theme_minimal() + stat_smooth(method = "lm", se = FALSE) + 
    geom_label_repel(aes(label= rownames(metrics)), size=3) + 
    theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=12)) + 
    labs(x = expression(paste("density ", km^{2})), y = expression(paste("rating[![enter image description here][1]][1]")))+ 
    theme(legend.position="top", legend.direction="horizontal") 

這是結果:enter image description here

然而,當我刪除geom_label_repel功能,我得到我需要的圖標符號 - 但當然,標籤不出現。

ggplot(metrics, aes(x=popDensity, y= TPB, color = factor(type))) + geom_point() +theme_minimal() + stat_smooth(method = "lm", se = FALSE) + 
    #geom_label_repel(aes(label= rownames(metrics)), size=3) + 
    theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=12)) + 
    labs(x = expression(paste("density ", km^{2})), y = expression(paste("rating")))+ 
    theme(legend.position="top", legend.direction="horizontal") 

enter image description here

爲什麼會出現這種情況,是有針對此問題的已知的解決方法?另外,有沒有辦法手動更改圖例標題的標題?我曾嘗試使用+ theme(legend.title = "title"),但得到的錯誤:

Error in (function (el, elname) : 
    Element legend.title must be a element_text object. 

樣本數據:

> dput(metrics) 
structure(list(popDensity = c(4308, 27812, 4447, 5334, 4662, 
2890, 24623, 5847, 1689, 481, 4100), TPB = c(1, 0.5, 1, 1.3, 
0.8, 4, 0.2, 0.7, 5, 4, 2), type = c("City", "City", "City", 
"City", "City", "City", "Town", "Town", "Town", "Town", "Town" 
)), .Names = c("popDensity", "TPB", "type"), row.names = c("City1", 
"City2", "City3", "City4", "City5", "City6", "Town1", "Town2", 
"Town3", "Town4", "Town5"), class = "data.frame") 

回答

1

您的通話geom_label_repel內添加show.legend = FALSEa's似乎是標籤的標準圖例,它會覆蓋使用相同顏色的geom_point

+0

謝謝 - 一個人應該如何將圖例的標題從'factor(type)'改爲別的東西? –

+0

嘗試'+ scale_colour_manual(name =「別的東西」)' –

+0

然後我得到'錯誤在f(...):參數「值」缺失,沒有默認' –