热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在ggplot2中添加比率线的最佳方法是什么?-Whatisthebestwaytoaddratiolinestoplotinggplot2?

Ihaveconstructedthefollowscatterplotinggplot2:我在ggplot2中构建了以下散点图:q<-ggplot(data,aes(x

I have constructed the follow scatter 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

However, what I want to know add are ratios expressed as lines on the graph. I have five lines to add to the graph and I am not sure the best way to add them. One way I have added them is with geom_abline as follows:

但是,我想知道的是在图表上用线表示的比率。我有五行要添加到图表中,我不确定添加它们的最佳方式。我添加它们的一种方法是使用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)

which produces the following graph: graph with ratio lines

它产生以下图表:具有比率线的图形

What I am confused with now is that I do not know how to add a legend detailing what each line represents.

我现在感到困惑的是,我不知道如何添加详细说明每行代表的图例。

Also, I cant seem to find a better way to attempt this graph besides abline. Would there be a better function to use in my situation?

此外,我似乎无法找到一个更好的方法来尝试此图表除了abline。在我的情况下会有更好的功能吗?

Thanks!

Here is my data:

这是我的数据:

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))

1 个解决方案

#1


0  

Here's a somewhat hacky solution. It works, but I had to change the point shape so that I could use the fill aesthetic to color the points and the color aesthetic for the lines - when I just used color for both of them, I needed to specify 6 color values, and ggplot put the lines and points in the same legend.

这是一个有点hacky的解决方案。它可以工作,但我必须改变点形状,以便我可以使用填充美学来为线条着色和颜色审美 - 当我只使用它们的颜色时,我需要指定6个颜色值,并且ggplot将线条和点放在同一个图例中。

First, store the names, intercepts, and slopes in a data frame:

首先,在数据框中存储名称,截距和斜率:

    ratios <- data.frame(intercept = rep(0, 4), 
                 slope = c(2.10559, 2.278689, 1.43309, 1.61066), 
                 Ratio = paste("ratio", 1:4))

Then, create all the lines with one call to geom_abline.

然后,通过一次调用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()

Here's the result: Here's the result I had to change the line types because some of the previous line types looked the same in the legend. I used override.aes = list(color = NA) because otherwise the legend for season has black lines going through it.

结果如下:我不得不更改线型,因为某些先前的线型在图例中看起来相同。我使用了override.aes = list(color = NA),因为否则季节的图例会有黑线穿过它。

To change the names of the lines in the legend, just replace paste("ratio", 1:4) with the actual values you'd like to use.

要更改图例中线条的名称,只需将粘贴(“比率”,1:4)替换为您要使用的实际值。

Edit: use stroke = NA in the call to geom_point to get rid of the black border.

编辑:在调用geom_point时使用stroke = NA来摆脱黑色边框。


推荐阅读
author-avatar
惰费旧使爽DDD_540
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有