在 ggplot2 中添加比率线以绘制的最佳方法是什么?
What is the best way to add ratio lines to plot in ggplot2?
我在ggplot2中构建了如下散点图:
q<-ggplot(data,aes(x=prot..,y=carb..,group=factor(early.or.late),color=factor(early.or.late))+geom_point(),size=4)+scale_color_manual(values=c("#E69F00", "#56B4E9"),name="Early or late\nSeason",breaks=c("1","2"),labels=c("Early","Late"))
q2<-q+coord_fixed()+scale_x_continuous(expand=c(0,0))+scale_y_continuous((expand=c(0,0)))+xlim(0,.25)+ylim(0,.25)
graph 1
但是,我想知道的是图表上用线条表示的比率。我有五行要添加到图表中,但我不确定添加它们的最佳方式。我添加它们的一种方法是使用 geom_abline 如下:
q<-ggplot(data,aes(x=prot..,y=carb..,group=factor(early.or.late),color=factor(early.or.late))+geom_point(),size=4)+scale_color_manual(values=c("#E69F00", "#56B4E9"),name="Early or late\nSeason",breaks=c("1","2"),labels=c("Early","Late"))
q2<-q+geom_abline(aes(intercept=0,slope=2.10559),size=2,lty=1,colour="#E69F00",show.legend=TRUE)+
geom_abline(aes(intercept=0,slope=2.278689),size=2,lty=3,colour="#E69F00")+
geom_abline(aes(intercept=0,slope=1.43309),size=2,lty=5,colour="#56B4E9")+
geom_abline(aes(intercept=0,slope=1.610966),size=2,lty=1,colour="#56B4E9")
q3<-q2+coord_fixed()+scale_x_continuous(expand=c(0,0))+scale_y_continuous((expand=c(0,0)))+xlim(0,.25)+ylim(0,.25)
生成下图:
graph with ratio lines
我现在很困惑的是我不知道如何添加一个图例来详细说明每一行代表什么。
此外,除了 abline 之外,我似乎找不到更好的方法来尝试此图。有没有更好的功能适合我的情况?
谢谢!
这是我的数据:
data<-structure(list(ï..Vial = c(36L, 31L, 37L, 39L, 42L, 43L, 44L,
78L, 80L, 85L, 87L, 89L, 64L, 65L, 68L, 70L, 26L, 27L, 67L, 33L,
82L, 88L, 66L, 72L, 40L, 41L, 84L, 28L, 30L, 38L, 76L), Experiment = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A",
"B"), class = "factor"), Location = structure(c(3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Coonamble",
"Ivanhoe", "Trangie"), class = "factor"), Field = c(2L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L), Plot.Trans = c(2L,
1L, 2L, 4L, 5L, 4L, 5L, 2L, 4L, 5L, 3L, 1L, 5L, 1L, 4L, 2L, 3L,
1L, 4L, 2L, 1L, 4L, 1L, 2L, 4L, 5L, 2L, 3L, 2L, 3L, 3L), Transect = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), early.or.late = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L), Functional.Group = structure(c(5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("",
"fern", "forb", "frob", "grass", "Grass", "rushes", "sedge",
"Sedge", "shrub"), class = "factor"), Species = structure(c(7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 8L, 24L, 24L, 25L, 28L, 28L, 23L, 23L, 34L, 31L, 31L, 32L,
35L), .Label = c("Buffel cenchrus cillarnes", "Cannonball", "Chloris Truncata",
"Danthonia caespitesa", "Diplachne fusca", "Dissocarpus caradoucus",
"Enteropogon acirularis", "Eragrostis parviflora", "Gymnoschoenus sphaerocephalus",
"Juncus", "Marsilea drummondii", "Medicago polymorpha", "Paspalidium constrictum",
"Paspalidium jubiflorium", "Queana", "Sedge Spp.", "Sida Spp",
"Solarium esuriah", "Stipa Bigeniculata", "Stipa Scailura", "Stipa Scarpra",
"Stipa Scrbra", "Stipa spp", "Stipa Spp", "Stipa Spp.", "Stipa variable",
"Stipa Variable", "Stipa Variables", "Tribulus terrestris", "Tribulus Terrestris",
"Unknown C", "Unknown E", "Unknown Forb", "Unknown Grass", "Unknown Grass 3",
"Unknown Herb A", "Unknown Herb B", "Walhalleya Subxerophila"
), class = "factor"), carb.. = c(0.129333365, 0.202844822, 0.142275002,
0.236668235, 0.169526039, 0.115846616, 0.213292819, 0.113944998,
0.124599354, 0.143516228, 0.115867745, 0.146299899, 0.102153528,
0.158337108, 0.191291861, 0.215434765, 0.183662016, 0.095864692,
0.179608436, 0.19113643, 0.234070371, 0.18278671, 0.186428087,
0.194377919, 0.145811916, 0.17748817, 0.186150973, 0.144640378,
0.170878979, 0.153140185, 0.145762334), prot.. = c(0.251963957,
0.231460224, 0.205105442, 0.208611869, 0.260078976, 0.260470299,
0.206310414, 0.142409299, 0.135318216, 0.088785988, 0.123123662,
0.172596121, 0.109418993, 0.117703208, 0.132949015, 0.129416645,
0.231538345, 0.21764968, 0.116498089, 0.173706485, 0.06157231,
0.134473911, 0.095607729, 0.145505098, 0.1587147, 0.213428882,
0.082957063, 0.253450972, 0.295918143, 0.300092067, 0.14219478
)), .Names = c("ï..Vial", "Experiment", "Location", "Field",
"Plot.Trans", "Transect", "early.or.late", "Functional.Group",
"Species", "carb..", "prot.."), class = "data.frame", row.names = c(36L,
37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 53L, 54L,
55L, 56L, 62L, 63L, 64L, 93L, 94L, 97L, 101L, 102L, 103L, 104L,
106L, 107L, 108L, 109L, 111L))
这是一个有点老套的解决方案。它有效,但我必须更改点形状,以便我可以使用填充美学为点着色,并使用颜色美学为线条着色——当我只为它们都使用颜色时,我需要指定 6 个颜色值,并且ggplot 将线和点放在同一个图例中。
首先,将名称、截距和斜率存储在数据框中:
ratios <- data.frame(intercept = rep(0, 4),
slope = c(2.10559, 2.278689, 1.43309, 1.61066),
Ratio = paste("ratio", 1:4))
然后,通过调用 geom_abline
.
创建所有行
ggplot(data, aes(x = prot.., y = carb.., group = factor(early.or.late),
fill = factor(early.or.late))) +
geom_point(size = 4, pch = 21) +
scale_fill_manual(values = c("#E69F00", "#56B4E9"), name = "Season",
labels = c("Early","Late")) +
scale_color_manual(values = rep(c("#E69F00", "#56B4E9"), 2)) +
scale_linetype_manual(values = rep(c(1,3), each = 2)) +
geom_abline(aes(intercept = intercept, slope = slope, color = Ratio,
lty = Ratio), data = ratios,
show.legend = TRUE, size = 2) +
scale_x_continuous(expand = c(0,0), limits = c(0, 0.25)) +
scale_y_continuous(expand = c(0,0), limits = c(0, 0.25)) +
guides(fill = guide_legend(override.aes = list(color = NA))) +
coord_fixed()
结果如下:
我不得不更改线型,因为之前的一些线型在图例中看起来是一样的。我使用了 override.aes = list(color = NA)
因为否则季节的图例有黑线穿过它。
要更改图例中线条的名称,只需将 paste("ratio", 1:4)
替换为您要使用的实际值即可。
编辑: 在对 geom_point
的调用中使用 stroke = NA
去除黑色边框。
我在ggplot2中构建了如下散点图:
q<-ggplot(data,aes(x=prot..,y=carb..,group=factor(early.or.late),color=factor(early.or.late))+geom_point(),size=4)+scale_color_manual(values=c("#E69F00", "#56B4E9"),name="Early or late\nSeason",breaks=c("1","2"),labels=c("Early","Late"))
q2<-q+coord_fixed()+scale_x_continuous(expand=c(0,0))+scale_y_continuous((expand=c(0,0)))+xlim(0,.25)+ylim(0,.25)
graph 1
但是,我想知道的是图表上用线条表示的比率。我有五行要添加到图表中,但我不确定添加它们的最佳方式。我添加它们的一种方法是使用 geom_abline 如下:
q<-ggplot(data,aes(x=prot..,y=carb..,group=factor(early.or.late),color=factor(early.or.late))+geom_point(),size=4)+scale_color_manual(values=c("#E69F00", "#56B4E9"),name="Early or late\nSeason",breaks=c("1","2"),labels=c("Early","Late"))
q2<-q+geom_abline(aes(intercept=0,slope=2.10559),size=2,lty=1,colour="#E69F00",show.legend=TRUE)+
geom_abline(aes(intercept=0,slope=2.278689),size=2,lty=3,colour="#E69F00")+
geom_abline(aes(intercept=0,slope=1.43309),size=2,lty=5,colour="#56B4E9")+
geom_abline(aes(intercept=0,slope=1.610966),size=2,lty=1,colour="#56B4E9")
q3<-q2+coord_fixed()+scale_x_continuous(expand=c(0,0))+scale_y_continuous((expand=c(0,0)))+xlim(0,.25)+ylim(0,.25)
生成下图: graph with ratio lines
我现在很困惑的是我不知道如何添加一个图例来详细说明每一行代表什么。
此外,除了 abline 之外,我似乎找不到更好的方法来尝试此图。有没有更好的功能适合我的情况?
谢谢!
这是我的数据:
data<-structure(list(ï..Vial = c(36L, 31L, 37L, 39L, 42L, 43L, 44L,
78L, 80L, 85L, 87L, 89L, 64L, 65L, 68L, 70L, 26L, 27L, 67L, 33L,
82L, 88L, 66L, 72L, 40L, 41L, 84L, 28L, 30L, 38L, 76L), Experiment = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A",
"B"), class = "factor"), Location = structure(c(3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Coonamble",
"Ivanhoe", "Trangie"), class = "factor"), Field = c(2L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L), Plot.Trans = c(2L,
1L, 2L, 4L, 5L, 4L, 5L, 2L, 4L, 5L, 3L, 1L, 5L, 1L, 4L, 2L, 3L,
1L, 4L, 2L, 1L, 4L, 1L, 2L, 4L, 5L, 2L, 3L, 2L, 3L, 3L), Transect = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), early.or.late = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L), Functional.Group = structure(c(5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("",
"fern", "forb", "frob", "grass", "Grass", "rushes", "sedge",
"Sedge", "shrub"), class = "factor"), Species = structure(c(7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 8L, 24L, 24L, 25L, 28L, 28L, 23L, 23L, 34L, 31L, 31L, 32L,
35L), .Label = c("Buffel cenchrus cillarnes", "Cannonball", "Chloris Truncata",
"Danthonia caespitesa", "Diplachne fusca", "Dissocarpus caradoucus",
"Enteropogon acirularis", "Eragrostis parviflora", "Gymnoschoenus sphaerocephalus",
"Juncus", "Marsilea drummondii", "Medicago polymorpha", "Paspalidium constrictum",
"Paspalidium jubiflorium", "Queana", "Sedge Spp.", "Sida Spp",
"Solarium esuriah", "Stipa Bigeniculata", "Stipa Scailura", "Stipa Scarpra",
"Stipa Scrbra", "Stipa spp", "Stipa Spp", "Stipa Spp.", "Stipa variable",
"Stipa Variable", "Stipa Variables", "Tribulus terrestris", "Tribulus Terrestris",
"Unknown C", "Unknown E", "Unknown Forb", "Unknown Grass", "Unknown Grass 3",
"Unknown Herb A", "Unknown Herb B", "Walhalleya Subxerophila"
), class = "factor"), carb.. = c(0.129333365, 0.202844822, 0.142275002,
0.236668235, 0.169526039, 0.115846616, 0.213292819, 0.113944998,
0.124599354, 0.143516228, 0.115867745, 0.146299899, 0.102153528,
0.158337108, 0.191291861, 0.215434765, 0.183662016, 0.095864692,
0.179608436, 0.19113643, 0.234070371, 0.18278671, 0.186428087,
0.194377919, 0.145811916, 0.17748817, 0.186150973, 0.144640378,
0.170878979, 0.153140185, 0.145762334), prot.. = c(0.251963957,
0.231460224, 0.205105442, 0.208611869, 0.260078976, 0.260470299,
0.206310414, 0.142409299, 0.135318216, 0.088785988, 0.123123662,
0.172596121, 0.109418993, 0.117703208, 0.132949015, 0.129416645,
0.231538345, 0.21764968, 0.116498089, 0.173706485, 0.06157231,
0.134473911, 0.095607729, 0.145505098, 0.1587147, 0.213428882,
0.082957063, 0.253450972, 0.295918143, 0.300092067, 0.14219478
)), .Names = c("ï..Vial", "Experiment", "Location", "Field",
"Plot.Trans", "Transect", "early.or.late", "Functional.Group",
"Species", "carb..", "prot.."), class = "data.frame", row.names = c(36L,
37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 53L, 54L,
55L, 56L, 62L, 63L, 64L, 93L, 94L, 97L, 101L, 102L, 103L, 104L,
106L, 107L, 108L, 109L, 111L))
这是一个有点老套的解决方案。它有效,但我必须更改点形状,以便我可以使用填充美学为点着色,并使用颜色美学为线条着色——当我只为它们都使用颜色时,我需要指定 6 个颜色值,并且ggplot 将线和点放在同一个图例中。
首先,将名称、截距和斜率存储在数据框中:
ratios <- data.frame(intercept = rep(0, 4),
slope = c(2.10559, 2.278689, 1.43309, 1.61066),
Ratio = paste("ratio", 1:4))
然后,通过调用 geom_abline
.
ggplot(data, aes(x = prot.., y = carb.., group = factor(early.or.late),
fill = factor(early.or.late))) +
geom_point(size = 4, pch = 21) +
scale_fill_manual(values = c("#E69F00", "#56B4E9"), name = "Season",
labels = c("Early","Late")) +
scale_color_manual(values = rep(c("#E69F00", "#56B4E9"), 2)) +
scale_linetype_manual(values = rep(c(1,3), each = 2)) +
geom_abline(aes(intercept = intercept, slope = slope, color = Ratio,
lty = Ratio), data = ratios,
show.legend = TRUE, size = 2) +
scale_x_continuous(expand = c(0,0), limits = c(0, 0.25)) +
scale_y_continuous(expand = c(0,0), limits = c(0, 0.25)) +
guides(fill = guide_legend(override.aes = list(color = NA))) +
coord_fixed()
结果如下:
override.aes = list(color = NA)
因为否则季节的图例有黑线穿过它。
要更改图例中线条的名称,只需将 paste("ratio", 1:4)
替换为您要使用的实际值即可。
编辑: 在对 geom_point
的调用中使用 stroke = NA
去除黑色边框。