R语言 如何在ggplot中创建虚线?它不工作

bbuxkriu  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(122)

我对1图例有问题。
图中的虚线在图例中不是虚线,尽管我使用命令定义了它们的样式:scale_linetype_manual我想用虚线显示第二年的输入。

gg <- ggplot()
    
    if ("Min" %in% selected_metrics) {
      gg <- gg + geom_line(data = monthly_temperatures_year1_A, aes(x = month, y = Min, color = "Min - Year 1"), size = 1, linetype = "solid", show.legend = TRUE)
      gg <- gg + geom_line(data = monthly_temperatures_year2_A, aes(x = month, y = Min, color = "Min - Year 2"), linetype = "dashed", size = 1)
      
    }
    if ("Mean" %in% selected_metrics) {
      gg <- gg + geom_line(data = monthly_temperatures_year1_A, aes(x = month, y = Mean, color = "Mean - Year 1"), size = 1, linetype = "solid")
      gg <- gg + geom_line(data = monthly_temperatures_year2_A, aes(x = month, y = Mean, color = "Mean - Year 2"), linetype = "dashed", size = 1)
      
    }
    if ("Max" %in% selected_metrics) {
      gg <- gg + geom_line(data = monthly_temperatures_year1_A, aes(x = month, y = Max, color = "Max - Year 1"), size = 1, linetype = "solid")
      gg <- gg + geom_line(data = monthly_temperatures_year2_A, aes(x = month, y = Max, color = "Max - Year 2"), linetype = "dashed", size = 1)
      
    }
    
    gg <- gg +
      labs(
        x = 'Month',
        y = 'Temperature',
        title = paste("Monthly", paste(selected_metrics, collapse = "/"), "Temperature Variation for", input$station)
      ) +
      scale_x_continuous(breaks = 1:12, labels = month_labels) +
      scale_color_manual(
        name = "Variables",
        labels = c(paste("Min -", input$year1_A), paste("Min -", input$year2_A), paste("Mean -", input$year1_A), paste("Mean -", input$year2_A), paste("Max -", input$year1_A), paste("Max -", input$year2_A)),
        values = c("orange", "orange", "green", "green", "red", "red")
      ) +
      scale_linetype_manual(
        name = "Variables",
        labels = c(paste("Min -", input$year1_A), paste("Min -", input$year2_A), paste("Mean -", input$year1_A), paste("Mean -", input$year2_A), paste("Max -", input$year1_A), paste("Max -", input$year2_A)),
        values = c("solid", "dashed", "solid", "dashed", "solid", "dashed")
      ) +
      theme(legend.position = "right")
    
    print(gg)
  })

如果任何人可以帮助和建议什么代码应该我使用或我应该改变它将是真的很棒!我被卡住了。非常感谢

oyt4ldly

oyt4ldly1#

样本数据:

set.seed(42)
year1 <- data.frame(x = factor(month.abb, levels=month.abb), y = runif(12), z = factor(sample(3, size=12, replace=TRUE)))
year2 <- data.frame(x = factor(month.abb, levels=month.abb), y = year1$y + 0.02, z = year1$z)

破碎图:

ggplot() + 
  geom_line(data = year1, mapping = aes(x, y, color = z, group = z), linetype = "solid") +
  geom_line(data = year2, mapping = aes(x, y, color = z, group = z), linetype = "dashed")

修复:

years <- dplyr::bind_rows(year1, year2, .id = "year")
ggplot() +
  geom_line(data = years, mapping = aes(x, y, color = z, group = interaction(z, year), linetype = factor(year))) +
  scale_linetype_manual(values = c("1"="solid", "2"="dashed"))

我喜欢在这里使用dplyr::bind_rows(...),因为在我的示例数据中,没有固有的“年份”区分列。同样,data.table::rbindlist(list(...), idcol="year")也做同样的事情。它们还(可能带有选项)支持不同的列集或列顺序。
如果每个year#框架都已经有了必要的"year"列,并且它们都有相同的列和列顺序,那么就可以使用years <- rbind(year1, year2)

相关问题