我对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)
})
如果任何人可以帮助和建议什么代码应该我使用或我应该改变它将是真的很棒!我被卡住了。非常感谢
1条答案
按热度按时间oyt4ldly1#
样本数据:
破碎图:
修复:
我喜欢在这里使用
dplyr::bind_rows(...)
,因为在我的示例数据中,没有固有的“年份”区分列。同样,data.table::rbindlist(list(...), idcol="year")
也做同样的事情。它们还(可能带有选项)支持不同的列集或列顺序。如果每个
year#
框架都已经有了必要的"year"
列,并且它们都有相同的列和列顺序,那么就可以使用years <- rbind(year1, year2)
。