如何用2个不同的Y轴绘图?

如何解决如何用2个不同的Y轴绘图?

| 我想在R中叠加两个散点图,以便每个点集都有其自己的(不同的)y轴(即,在图的位置2和4上),但这些点看起来都叠加在同一图上。 可以用
plot
这样做吗? 编辑显示问题的示例代码
# example code for SO question
y1 <- rnorm(10,100,20)
y2 <- rnorm(10,1,1)
x <- 1:10
# in this plot y2 is plotted on what is clearly an inappropriate scale
plot(y1 ~ x,ylim = c(-1,150))
points(y2 ~ x,pch = 2)
    

解决方法

更新:R Wiki上已复制的资料,网址为http://rwiki.sciviews.org/doku.php?id=tips:graphics-base:2yaxes,现在链接断开:也可以从Wayback机器上获得 同一图上的两个不同的y轴 (某些资料最初由Daniel Rajdl撰写,2006/03/31 15:26) 请注意,在极少数情况下,在同一绘图上使用两个不同的比例是合适的。容易误导图形的观看者。请检查以下两个示例和有关此问题的评论(“垃圾图表”中的example1,example2),以及Stephen Few的这篇文章(结论是“我当然不能一劳永逸地得出结论:双刻度轴的图永远不会有用;只是我无法根据其他更好的解决方案来考虑这种情况。”)另请参阅本动画片中的第4点... 如果确定的话,基本方法是创建第一个绘图,设置
par(new=TRUE)
以防止R清除图形设备,使用
axes=FALSE
创建第二个绘图(并将
xlab
ylab
设置为空白–
ann=FALSE
应该也可以),然后使用
axis(side=4)
在右侧添加新轴,
mtext(...,side=4)
在右侧添加轴标签。这是一个使用一些虚构数据的示例:
set.seed(101)
x <- 1:10
y <- rnorm(10)
## second data set on a very different scale
z <- runif(10,min=1000,max=10000) 
par(mar = c(5,4,4) + 0.3)  # Leave space for z axis
plot(x,y) # first plot
par(new = TRUE)
plot(x,z,type = \"l\",axes = FALSE,bty = \"n\",xlab = \"\",ylab = \"\")
axis(side=4,at = pretty(range(z)))
mtext(\"z\",side=4,line=3)
plotrix
包中的
twoord.plot()
latticeExtra
包中的
doubleYScale()
会自动执行此过程。 另一个示例(改编自Robert W. Baer的R邮件列表帖子):
## set up some fake test data
time <- seq(0,72,12)
betagal.abs <- c(0.05,0.18,0.25,0.31,0.32,0.34,0.35)
cell.density <- c(0,1000,2000,3000,4000,5000,6000)

## add extra space to right margin of plot within frame
par(mar=c(5,6) + 0.1)

## Plot first set of data and draw its axis
plot(time,betagal.abs,pch=16,axes=FALSE,ylim=c(0,1),xlab=\"\",ylab=\"\",type=\"b\",col=\"black\",main=\"Mike\'s test data\")
axis(2,las=1)  ## las=1 makes horizontal labels
mtext(\"Beta Gal Absorbance\",side=2,line=2.5)
box()

## Allow a second plot on the same graph
par(new=TRUE)

## Plot the second plot and put axis scale on right
plot(time,cell.density,pch=15,7000),col=\"red\")
## a little farther out (line=4) to make room for labels
mtext(\"Cell Density\",col=\"red\",line=4) 
axis(4,col.axis=\"red\",las=1)

## Draw the time axis
axis(1,pretty(range(time),10))
mtext(\"Time (Hours)\",side=1,line=2.5)  

## Add Legend
legend(\"topleft\",legend=c(\"Beta Gal\",\"Cell Density\"),text.col=c(\"black\",\"red\"),pch=c(16,15),col=c(\"black\",\"red\"))
相似的配方可用于叠加不同类型的图-条形图,直方图等。     ,顾名思义,plotrix包中的“ 10”具有两个纵坐标轴。
library(plotrix)
example(twoord.plot)
    ,一种选择是并排绘制两个图。
ggplot2
facet_wrap()
为此提供了一个不错的选择:
dat <- data.frame(x = c(rnorm(100),rnorm(100,10,2)),y = c(rnorm(100),rlnorm(100,9,index = rep(1:2,each = 100)
  )

require(ggplot2)
ggplot(dat,aes(x,y)) + 
geom_point() + 
facet_wrap(~ index,scales = \"free_y\")
    ,如果可以放弃刻度/轴标签,则可以将数据重新缩放为(0,1)间隔。例如,当您通常对轨道之间的局部相关性感兴趣并且它们具有不同的比例(覆盖率成千上万,Fst 0-1)时,这适用于染色体上的不同“摆动” traks。
# rescale numeric vector into (0,1) interval
# clip everything outside the range 
rescale <- function(vec,lims=range(vec),clip=c(0,1)) {
  # find the coeficients of transforming linear equation
  # that maps the lims range to (0,1)
  slope <- (1 - 0) / (lims[2] - lims[1])
  intercept <- - slope * lims[1]

  xformed <- slope * vec + intercept

  # do the clipping
  xformed[xformed < 0] <- clip[1]
  xformed[xformed > 1] <- clip[2]

  xformed
}
然后,使用具有
chrom
position
coverage
fst
列的数据帧,您可以执行以下操作:
ggplot(d,aes(position)) + 
  geom_line(aes(y = rescale(fst))) + 
  geom_line(aes(y = rescale(coverage))) +
  facet_wrap(~chrom)
这样做的好处是您不仅限于两个traccs。     ,我也建议,
plotrix
包中的plot26ѭ具有更多的两个纵坐标轴。
data<-read.table(text=
\"e0AL fxAL e0CO fxCO e0BR fxBR anos
 51.8  5.9 50.6  6.8 51.0  6.2 1955
 54.7  5.9 55.2  6.8 53.5  6.2 1960
 57.1  6.0 57.9  6.8 55.9  6.2 1965
 59.1  5.6 60.1  6.2 57.9  5.4 1970
 61.2  5.1 61.8  5.0 59.8  4.7 1975
 63.4  4.5 64.0  4.3 61.8  4.3 1980
 65.4  3.9 66.9  3.7 63.5  3.8 1985
 67.3  3.4 68.0  3.2 65.5  3.1 1990
 69.1  3.0 68.7  3.0 67.5  2.6 1995
 70.9  2.8 70.3  2.8 69.5  2.5 2000
 72.4  2.5 71.7  2.6 71.1  2.3 2005
 73.3  2.3 72.9  2.5 72.1  1.9 2010
 74.3  2.2 73.8  2.4 73.2  1.8 2015
 75.2  2.0 74.6  2.3 74.2  1.7 2020
 76.0  2.0 75.4  2.2 75.2  1.6 2025
 76.8  1.9 76.2  2.1 76.1  1.6 2030
 77.6  1.9 76.9  2.1 77.1  1.6 2035
 78.4  1.9 77.6  2.0 77.9  1.7 2040
 79.1  1.8 78.3  1.9 78.7  1.7 2045
 79.8  1.8 79.0  1.9 79.5  1.7 2050
 80.5  1.8 79.7  1.9 80.3  1.7 2055
 81.1  1.8 80.3  1.8 80.9  1.8 2060
 81.7  1.8 80.9  1.8 81.6  1.8 2065
 82.3  1.8 81.4  1.8 82.2  1.8 2070
 82.8  1.8 82.0  1.7 82.8  1.8 2075
 83.3  1.8 82.5  1.7 83.4  1.9 2080
 83.8  1.8 83.0  1.7 83.9  1.9 2085
 84.3  1.9 83.5  1.8 84.4  1.9 2090
 84.7  1.9 83.9  1.8 84.9  1.9 2095
 85.1  1.9 84.3  1.8 85.4  1.9 2100\",header=T)

require(plotrix)
twoord.stackplot(lx=data$anos,rx=data$anos,ldata=cbind(data$e0AL,data$e0BR,data$e0CO),rdata=cbind(data$fxAL,data$fxBR,data$fxCO),lcol=c(\"black\",\"red\",\"blue\"),rcol=c(\"black\",ltype=c(\"l\",\"o\",\"b\"),rtype=c(\"l\",lylab=\"Años de Vida\",rylab=\"Hijos x Mujer\",xlab=\"Tiempo\",main=\"Mortalidad/Fecundidad:1950–2100\",border=\"grey80\")
legend(\"bottomright\",c(paste(\"Proy:\",c(\"A. Latina\",\"Brasil\",\"Colombia\"))),cex=1,lwd=2,bty=\"n\",lty=c(1,1,2),pch=c(NA,1) )
    ,类似于@BenBolker接受的答案的另一种替代方法是在添加第二组点时重新定义现有图的坐标。 这是一个最小的例子。 数据:
x  <- 1:10
y1 <- rnorm(10,100,20)
y2 <- rnorm(10,1)
情节:
par(mar=c(5,5,5)+0.1,las=1)

plot.new()
plot.window(xlim=range(x),ylim=range(y1))
points(x,y1,pch=19)
axis(1)
axis(2,col.axis=\"red\")
box()

plot.window(xlim=range(x),ylim=range(y2))
points(x,y2,col=\"limegreen\",pch=19)
axis(4,col.axis=\"limegreen\")
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-