一线传奇(错误:不知道如何在剧情中添加o)

时间:2016-06-17 16:59:31

标签: r plot ggplot2

我希望我的情节上的传奇是单行的。我尝试使用guide_legend(nrow=1)guides(fill=guide_legend(nrow=1,byrow=TRUE)),它们都产生相同的错误Error: Don't know how to add o to a plot

示例图片

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)

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:3)

您可以将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")