R 如何从2个不同长度的数据帧显示双y轴的图例?

R 如何从2个不同长度的数据帧显示双y轴的图例?,r,legend,yaxis,R,Legend,Yaxis,我需要帮助显示我在两个轴上绘制的数据的图例-两个数据帧,以公共x轴作为日期,我使用缩放将它们绘制在一起。但是,如果show.legend=true,则它们都不会显示在绘图上 如果您能帮助我,我将不胜感激!谢谢 示例数据集测试线索:(数据每周提供一次,为期2个月) 示例数据集测试_ortho:(2个月内每3天提供一次数据) 这是我的密码: test1 <- ggplot()+ geom_point(mapping = aes(x=test_lead$CollectDate, y=test_

我需要帮助显示我在两个轴上绘制的数据的图例-两个数据帧,以公共x轴作为日期,我使用缩放将它们绘制在一起。但是,如果show.legend=true,则它们都不会显示在绘图上

如果您能帮助我,我将不胜感激!谢谢

示例数据集测试线索:(数据每周提供一次,为期2个月)

示例数据集测试_ortho:(2个月内每3天提供一次数据)

这是我的密码:

test1 <- ggplot()+

geom_point(mapping = aes(x=test_lead$CollectDate, y=test_lead$ReportedResult), colour = "blue",  show.legend = TRUE) +

geom_line(mapping = aes(x=test_lead$CollectDate, y=test_lead$ReportedResult), colour = "blue", show.legend = TRUE) +

geom_point(mapping = aes(x=test_ph$CollectDate, y=test_ph$ReportedResult*27.2/1.920),colour = "hotpink", show.legend = TRUE) +

geom_line(mapping = aes(x=test_ph$CollectDate, y=test_ph$ReportedResult*27.2/1.920), colour = "hotpink", show.legend = TRUE) +
 scale_x_date(name = "", date_breaks = "2 week", date_labels = "%Y/%m/%d") +
theme(
    plot.title = element_text(size = 16, 
                              face = "bold",
                              family = "sans",
                              color = "black",
                              hjust = 0.5,
                              lineheight = 1.2 ), 
    axis.text.x = element_text(face="plain", size=10.5, angle=90), 
    axis.text.y = element_text(face="plain", size=10.5, angle=0), 
    legend.position="bottom", legend.box = "horizontal",
    panel.background = element_rect(fill='white', colour='black'), 
    panel.grid.major.y = element_line(colour = "grey")
  )

test1最简单的方法是将数据帧绑定到一个数据帧中,并为颜色打印调用Param。这样,就可以避免对两个数据帧运行相同的代码两次

如果不想绑定,可以在所有aes()中为颜色调用Param,并添加scale\u color\u手册

test1 <- ggplot()+

  geom_point(test_lead, mapping = aes(x=CollectDate, y=ReportedResult, color = Param), show.legend = TRUE) +

  geom_line(test_lead, mapping = aes(x=CollectDate, y=ReportedResult, color = Param), show.legend = TRUE) +

  geom_point(test_ph, mapping = aes(x=CollectDate, y=ReportedResult*27.2/1.920, color = Param), show.legend = TRUE) +

  geom_line(test_ph, mapping = aes(x=CollectDate, y=ReportedResult*27.2/1.920, color = Param), show.legend = TRUE) +
  scale_color_manual(values=c("blue", "hotpink")) +
  scale_x_date(name = "", date_breaks = "2 week", date_labels = "%Y/%m/%d") +
  theme(
    plot.title = element_text(size = 16, 
                              face = "bold",
                              family = "sans",
                              color = "black",
                              hjust = 0.5,
                              lineheight = 1.2 ), 
    axis.text.x = element_text(face="plain", size=10.5, angle=90), 
    axis.text.y = element_text(face="plain", size=10.5, angle=0), 
    legend.position="bottom", legend.box = "horizontal",
    panel.background = element_rect(fill='white', colour='black'), 
    panel.grid.major.y = element_line(colour = "grey")
  )

test1请共享一个示例数据集,以便我们可以复制您正在做的事情。我刚刚提供了我关心的这个绘图的列。希望他们讲得通。谢谢!!!我考虑过绑定它们,因为大约有10个参数和10个站点,所以我当前的代码效率很低。但我在使用bind df绘制它们时也遇到了问题,所以我决定坚持使用这个。
test1 <- ggplot()+

geom_point(mapping = aes(x=test_lead$CollectDate, y=test_lead$ReportedResult), colour = "blue",  show.legend = TRUE) +

geom_line(mapping = aes(x=test_lead$CollectDate, y=test_lead$ReportedResult), colour = "blue", show.legend = TRUE) +

geom_point(mapping = aes(x=test_ph$CollectDate, y=test_ph$ReportedResult*27.2/1.920),colour = "hotpink", show.legend = TRUE) +

geom_line(mapping = aes(x=test_ph$CollectDate, y=test_ph$ReportedResult*27.2/1.920), colour = "hotpink", show.legend = TRUE) +
 scale_x_date(name = "", date_breaks = "2 week", date_labels = "%Y/%m/%d") +
theme(
    plot.title = element_text(size = 16, 
                              face = "bold",
                              family = "sans",
                              color = "black",
                              hjust = 0.5,
                              lineheight = 1.2 ), 
    axis.text.x = element_text(face="plain", size=10.5, angle=90), 
    axis.text.y = element_text(face="plain", size=10.5, angle=0), 
    legend.position="bottom", legend.box = "horizontal",
    panel.background = element_rect(fill='white', colour='black'), 
    panel.grid.major.y = element_line(colour = "grey")
  )
test1 <- ggplot()+

  geom_point(test_lead, mapping = aes(x=CollectDate, y=ReportedResult, color = Param), show.legend = TRUE) +

  geom_line(test_lead, mapping = aes(x=CollectDate, y=ReportedResult, color = Param), show.legend = TRUE) +

  geom_point(test_ph, mapping = aes(x=CollectDate, y=ReportedResult*27.2/1.920, color = Param), show.legend = TRUE) +

  geom_line(test_ph, mapping = aes(x=CollectDate, y=ReportedResult*27.2/1.920, color = Param), show.legend = TRUE) +
  scale_color_manual(values=c("blue", "hotpink")) +
  scale_x_date(name = "", date_breaks = "2 week", date_labels = "%Y/%m/%d") +
  theme(
    plot.title = element_text(size = 16, 
                              face = "bold",
                              family = "sans",
                              color = "black",
                              hjust = 0.5,
                              lineheight = 1.2 ), 
    axis.text.x = element_text(face="plain", size=10.5, angle=90), 
    axis.text.y = element_text(face="plain", size=10.5, angle=0), 
    legend.position="bottom", legend.box = "horizontal",
    panel.background = element_rect(fill='white', colour='black'), 
    panel.grid.major.y = element_line(colour = "grey")
  )