2016-10-26 37 views
1

我已閱讀了一些關於此的帖子,但沒有找到適合我的問題的正確解決方案。 因爲來自geom_vline的虛線是垂直的,所以它也在圖例中顯示。最重要的是,短劃線也可以得到一個真正沒有必要的垂直合作伙伴。 是否有可能爲顏色和類型只獲取水平線?這會讓傳說更加清晰。由於我的代碼基於大型數據集,因此我在此處提供了一個簡單的短變體。如何從ggplot的圖例中刪除線條?

# data (example) 

meetdagen1 <- as.Date(c("2016-06-01", "2016-06-28", "2016-07-17", "2016-08-03", "2016-08-30", "2016-09-10")) 
maxtemp <- c(20, 22, 28, 24, 23, 22) 
meantemp <- maxtemp - 2 
mintemp <- meantemp - 2 

meetdagen2 <- c(meetdagen1, as.Date(c("2016-09-29", "2016-10-12", "2016-11-01"))) 
maxtemp2 <- c(maxtemp, 20, 17, 19) 
meantemp2 <- maxtemp2 - 2 
mintemp2 <- meantemp2 - 2 

# dataframes for ggplot 

df <- data.frame(meetdagen1, meantemp, mintemp, maxtemp) 
df2 <- data.frame(meetdagen2, meantemp2, mintemp2, maxtemp2) 

# plot 

ggplot() + 
    xlab("Time (months)") + 
    ylab("Daily temperature (°C)") + 
    scale_x_date(date_labels = "%b %d", date_breaks = "1 month") + 
    geom_line(data = df, aes(x = meetdagen1, y = maxtemp, colour = "max.temp"), size = 0.75) + 
    geom_line(data = df, aes(x = meetdagen1, y = meantemp, colour = "gem.temp"), size = 0.75) + 
    geom_line(data = df, aes(x = meetdagen1, y = mintemp, colour = "min.temp"), size = 0.75) + 
    geom_line(data = df2, aes(x = meetdagen2, y = maxtemp2, colour = "max.temp", lty = "prediction"), size = 0.75) + 
    geom_line(data = df2, aes(x = meetdagen2, y = meantemp2, colour = "gem.temp", lty = "prediction"), size = 0.75) + 
    geom_line(data = df2, aes(x = meetdagen2, y = mintemp2, colour = "min.temp", lty = "prediction"), size = 0.75) + 
    geom_vline(aes(xintercept = as.numeric(Sys.Date()), lty = "today"), size = 0.75) + 
    scale_colour_manual("", values = c(max.temp = "firebrick2", gem.temp = "grey20", min.temp = "royalblue3")) + 
    scale_linetype_manual("", values = c(prediction = 3, today = 5)) #+ 
    #guides(colour = guide_legend(override.aes = list(linetype = 0))) + 
    #guides(lintype = guide_legend(override.aes = list(colour = NA))) 

底部兩行是我在類似問題中找到的解決方案。但他們隱藏所有線型或所有顏色,這是沒有用的。

任何想法?

(我想插入圖片,但我不知道在哪個網站,我可以把他們...)

回答

2

如果添加show.legend = FALSE號召,geom_vline這將不包括垂直線,這應該給你想要什麼。

+0

哇,那麼容易...謝謝你的快速反應! – Tingolfin