热门标签 | 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来摆脱黑色边框。


推荐阅读
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
  • node.jsrequire和ES6导入导出的区别原 ... [详细]
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社区 版权所有