在 ggplot2 中使用 facet_wrap 时,通过两个轴中的相同值跟踪直线
Tracing linear lines through same values in both axis when using facet_wrap in ggplot2
我有这个数据框:
df <- structure(list(Real = c(5, 11, 11, 17, 6, 7, 11, 11, 10, 11,
13, 11, 7, 8, 8, 9, 10, 7, 7, 6, 5, 13, 8, 10, 7, 11, 4, 6, 5,
7, 9, 4, 8), Predicted = c(7.53104960942489, 8.48933218073805,
6.53527751338467, 15.5681067019339, 6.16286447784273, 9.60252366514763,
9.42571629457248, 10.2524454322055, 9.32847653546665, 9.77523370651072,
10.3625498760624, 14.275897279353, 7.69457109139684, 7.26906167591947,
8.07512249762137, 7.36859387534307, 10.357436237549, 7.87435765638371,
8.74311433109198, 7.29886361235044, 7.2713976024488, 7.55669872113011,
7.43176240945188, 13.9422570019077, 9.88336055293638, 9.63284506089816,
5.5285713215912, 10.4119762640024, 4.76896313240313, 8.45958527511939,
5.65034115485842, 4.12733609483675, 4.27125334429662), Tooth = c("Incisor",
"Incisor", "Incisor", "Incisor", "Incisor", "Canine", "Canine",
"Canine", "Canine", "Canine", "Canine", "Canine", "Premolar",
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar",
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar",
"Premolar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar",
"Molar")), .Names = c("Real", "Predicted", "Tooth"), row.names = c("167",
"174", "176", "315", "321", "105", "148", "241", "272", "305",
"314", "337", "115", "118", "131", "141", "144", "224", "227",
"249", "253", "258", "275", "306", "323", "338", "228", "295",
"299", "311", "312", "317", "326"), class = "data.frame")
我 运行 这个 ggplot:
int_breaks <- function(x, n = 5) {
l <- pretty(x, n)
l[abs(l %% 1) < .Machine$double.eps ^ 0.5]
}
library(ggplot2)
ggplot(data = df, aes(x = Real, y = Predicted)) +
geom_point(color = "red") +
facet_wrap(~factor(Tooth, levels = c("Incisor", "Canine", "Premolar", "Molar")), scales = "free") +
scale_y_continuous(breaks = int_breaks) +
scale_x_continuous(breaks = int_breaks)
我终于明白了这个情节:
我想在每个绘图中追踪一条经过 X 轴和 Y 轴相同值的线。也就是说,坐标 (10,10) 和 (15,15) 将定义直线。
预期的结果是这样的:
我试过 geom_abline()
但没有退出。
此外,我意识到在使用 coord_fixed()
时出现错误。关于如何标准化可视化有什么想法吗?
编辑 1
按照 Ian Campbell 的解决方案,复制粘贴他的代码,我得到了这个:
我没有收到关于该代码的任何警告。有什么想法吗?
geom_abline
这种方法对你不起作用吗?
library(ggplot2)
ggplot(data = df, aes(x = Real, y = Predicted)) +
geom_point(color = "red") +
geom_abline(slope = 1, intercept = 0) +
facet_wrap(~factor(Tooth, levels = c("Incisor", "Canine", "Premolar", "Molar")), scales = "free") +
scale_y_continuous(breaks = int_breaks) +
scale_x_continuous(breaks = int_breaks)
我有这个数据框:
df <- structure(list(Real = c(5, 11, 11, 17, 6, 7, 11, 11, 10, 11,
13, 11, 7, 8, 8, 9, 10, 7, 7, 6, 5, 13, 8, 10, 7, 11, 4, 6, 5,
7, 9, 4, 8), Predicted = c(7.53104960942489, 8.48933218073805,
6.53527751338467, 15.5681067019339, 6.16286447784273, 9.60252366514763,
9.42571629457248, 10.2524454322055, 9.32847653546665, 9.77523370651072,
10.3625498760624, 14.275897279353, 7.69457109139684, 7.26906167591947,
8.07512249762137, 7.36859387534307, 10.357436237549, 7.87435765638371,
8.74311433109198, 7.29886361235044, 7.2713976024488, 7.55669872113011,
7.43176240945188, 13.9422570019077, 9.88336055293638, 9.63284506089816,
5.5285713215912, 10.4119762640024, 4.76896313240313, 8.45958527511939,
5.65034115485842, 4.12733609483675, 4.27125334429662), Tooth = c("Incisor",
"Incisor", "Incisor", "Incisor", "Incisor", "Canine", "Canine",
"Canine", "Canine", "Canine", "Canine", "Canine", "Premolar",
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar",
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar",
"Premolar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar",
"Molar")), .Names = c("Real", "Predicted", "Tooth"), row.names = c("167",
"174", "176", "315", "321", "105", "148", "241", "272", "305",
"314", "337", "115", "118", "131", "141", "144", "224", "227",
"249", "253", "258", "275", "306", "323", "338", "228", "295",
"299", "311", "312", "317", "326"), class = "data.frame")
我 运行 这个 ggplot:
int_breaks <- function(x, n = 5) {
l <- pretty(x, n)
l[abs(l %% 1) < .Machine$double.eps ^ 0.5]
}
library(ggplot2)
ggplot(data = df, aes(x = Real, y = Predicted)) +
geom_point(color = "red") +
facet_wrap(~factor(Tooth, levels = c("Incisor", "Canine", "Premolar", "Molar")), scales = "free") +
scale_y_continuous(breaks = int_breaks) +
scale_x_continuous(breaks = int_breaks)
我终于明白了这个情节:
我想在每个绘图中追踪一条经过 X 轴和 Y 轴相同值的线。也就是说,坐标 (10,10) 和 (15,15) 将定义直线。
预期的结果是这样的:
我试过 geom_abline()
但没有退出。
此外,我意识到在使用 coord_fixed()
时出现错误。关于如何标准化可视化有什么想法吗?
编辑 1
按照 Ian Campbell 的解决方案,复制粘贴他的代码,我得到了这个:
我没有收到关于该代码的任何警告。有什么想法吗?
geom_abline
这种方法对你不起作用吗?
library(ggplot2)
ggplot(data = df, aes(x = Real, y = Predicted)) +
geom_point(color = "red") +
geom_abline(slope = 1, intercept = 0) +
facet_wrap(~factor(Tooth, levels = c("Incisor", "Canine", "Premolar", "Molar")), scales = "free") +
scale_y_continuous(breaks = int_breaks) +
scale_x_continuous(breaks = int_breaks)