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


推荐阅读
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
  • 本文讨论了一个数列求和问题,该数列按照一定规律生成。通过观察数列的规律,我们可以得出求解该问题的算法。具体算法为计算前n项i*f[i]的和,其中f[i]表示数列中有i个数字。根据参考的思路,我们可以将算法的时间复杂度控制在O(n),即计算到5e5即可满足1e9的要求。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
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社区 版权所有