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


推荐阅读
  • 尽管使用TensorFlow和PyTorch等成熟框架可以显著降低实现递归神经网络(RNN)的门槛,但对于初学者来说,理解其底层原理至关重要。本文将引导您使用NumPy从头构建一个用于自然语言处理(NLP)的RNN模型。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 基因组浏览器中的Wig格式解析
    本文详细介绍了Wiggle(Wig)格式及其在基因组浏览器中的应用,涵盖variableStep和fixedStep两种主要格式的特点、适用场景及具体使用方法。同时,还提供了关于数据值和自定义参数的补充信息。 ... [详细]
  • 基于KVM的SRIOV直通配置及性能测试
    SRIOV介绍、VF直通配置,以及包转发率性能测试小慢哥的原创文章,欢迎转载目录?1.SRIOV介绍?2.环境说明?3.开启SRIOV?4.生成VF?5.VF ... [详细]
  • 本文详细介绍了Java中org.w3c.dom.Text类的splitText()方法,通过多个代码示例展示了其实际应用。该方法用于将文本节点在指定位置拆分为两个节点,并保持在文档树中。 ... [详细]
  • 本文详细介绍了 Apache Jena 库中的 Txn.executeWrite 方法,通过多个实际代码示例展示了其在不同场景下的应用,帮助开发者更好地理解和使用该方法。 ... [详细]
  • 深入理解Java泛型:JDK 5的新特性
    本文详细介绍了Java泛型的概念及其在JDK 5中的应用,通过具体代码示例解释了泛型的引入、作用和优势。同时,探讨了泛型类、泛型方法和泛型接口的实现,并深入讲解了通配符的使用。 ... [详细]
  • Linux设备驱动程序:异步时间操作与调度机制
    本文介绍了Linux内核中的几种异步延迟操作方法,包括内核定时器、tasklet机制和工作队列。这些机制允许在未来的某个时间点执行任务,而无需阻塞当前线程,从而提高系统的响应性和效率。 ... [详细]
  • 本文详细介绍了中央电视台电影频道的节目预告,并通过专业工具分析了其加载方式,确保用户能够获取最准确的电视节目信息。 ... [详细]
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社区 版权所有