2016-06-17 27 views
3

我希望將我的情節頂部的圖例設置爲單線。我試圖用guide_legend(nrow=1)guides(fill=guide_legend(nrow=1,byrow=TRUE))這都產生了同樣的錯誤Error: Don't know how to add o to a plot單線圖例(錯誤:不知道如何將o添加到圖)

示例圖像

enter image description here

這是我的代碼和數據:

date = as.Date(c("2015-06-09","2015-06-09","2015-06-09","2015-06-09","2015-06-09","2015-06-09","2015-06-09, 2015-06-10","2015-06-10","2015-06-10","2015-06-10","2015-06-10","2015-06-10","2015-06-10","2015-06-10")) 
ph = c(8.1, 7.66, 6.71, 8.23, 8.23, 8.21, 7.9,8.1, 7.66, 6.71, 8.23, 8.23, 8.21, 7.9) 
step = c("Step1","Step2","Step3","Step4","Step5","Step6","Step7","Step1","Step2","Step3","Step4","Step5","Step6","Step7") 

df = data.frame(date,ph,step) 

df_plot <- ggplot(df,aes(x=date, y=ph, group=step,color=step, shape=step)) + 
    scale_shape_manual(values=c(1,2,3,4,5,6,7)) + geom_point(size=2) + geom_line() + 
    theme(legend.position = "top") + guide_legend(nrow=1) 

任何幫助將是非常不勝感激!

+0

我現在不能測試它,但你可以嘗試使用'theme(legend.position =「top」,legend.box =「horizo​​ntal」)'告訴我是否有用? – Mutador

+0

你有顏色和形狀的美學,所以這些就是你需要在'guides'中設置的內容:'guides(color = guide_legend(nrow = 1,byrow = TRUE),shape = guide_legend(nrow = 1,byrow = TRUE)) '(儘管我不認爲'byrow'在這裏改變了什麼)。 '指南(fill = guide_legend(nrow = 1,byrow = TRUE))'不應該產生錯誤,但不會以任何方式改變情節。 – eipi10

+0

@ eipi10感謝您的回覆。你的解決方案工作得很好! – Jonas

回答

2

您可以將guide_legend()放在scale_shape_manual()中。以下爲我工作:

ggplot(df,aes(x=date, y=ph, group=step,color=step, shape=step)) + 
    scale_shape_manual(values=c(1,2,3,4,5,6,7), guide = guide_legend(nrow=1)) + 
    geom_point(size=2) + geom_line() + 
    theme(legend.position = "top")