r - 在 ggplot2 中创建具有不同数据集的图例
问题描述
我正在尝试在 ggplot 中创建一个图例。如果我使用同一个文件中的不同变量,我添加colour = "xx"
aes 并且它可以工作。但是如果它是相同的变量但不同的数据集呢?
在下面的示例中,我从两个不同的数据集中绘制 Value ~ Year。如何创建一个用红线表示 df1 和用蓝线表示 df2 的图例?
A <- c(2001, 2002, 2003, 2004, 2005)
B <- c(3, 5, 2, 7, 5)
C <- c(2, 7, 4, 3, 5)
df1 <- data.frame(A, B)
df2 <- data.frame(A, C)
colnames(df1) <- c("Year","Value")
colnames(df2) <- c("Year","Value")
(test <- ggplot(df1, aes(Value, Year)) + geom_path(size = 1, colour='red') +
geom_path(data=df2, colour='blue') + ylab("Year")+ scale_x_continuous(position = "top") + scale_y_reverse(expand = c(0, 0)))
解决方案
推荐阅读
- algorithm - 使用最少给定的矩形覆盖一条折线,同时保持她的连续性
- botframework - LUIS 意图中无法识别 Communication.ContactName 实体
- spring - 比较 JDBCTemplate 和 Hibernate 使用 Spring 进行数据访问
- unity3d - 为什么我在尝试初始化 Unit IAP 时无法解析“实例”调用?
- c# - 排序点不正确
- mysql - 通过 MySQL 命令更改并显示“客户端”的 max_allowed_packet
- c# - C# WPF DataGrid with user input and update the variables accordingly
- vuejs2 - Vuejs-在生产模式下重定向期间出错
- azure - Powershell - 检查 Azure AD 来宾是否已存在
- android - webview android studio - 输入文件不可点击