如何为散点图定义固定的宽高比 [英] How to define fixed aspect-ratio for scatter-plot

查看:114
本文介绍了如何为散点图定义固定的宽高比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制从两个种群中每个个体测得的两个同位素的相关系数(值= 0.0:1.0).我希望散点图具有固定的长宽比,以便无论图形设备如何,x轴和y轴的大小都完全相同.有建议吗?

这是我在R中的第一个图,对我的代码改进的任何评论都值得赞赏吗?最后,值得学习基本的绘图技术还是我应该直接跳到ggplot2还是点阵?

我的剧情脚本:

## Create dataset
WW_corr <-
structure(list(South_N15 = c(0.7976495, 0.1796725, 0.5338347,
0.4103769, 0.7447027, 0.5080296, 0.7566544, 0.7432026, 0.8927161
), South_C13 = c(0.76706752, 0.02320767, 0.88429902, 0.36648357,
0.73840937, 0.0523504, 0.52145159, 0.50707858, 0.51874445), North_N15 = c(0.7483608,
0.4294148, 0.9283554, 0.8831571, 0.5056481, 0.1945943, 0.8492716,
0.5759033, 0.7483608), North_C13 = c(0.08114805, 0.47268136,
0.94975596, 0.06023815, 0.33652839, 0.53055943, 0.30228833, 0.8864435,
0.08114805)), .Names = c("South_N15", "South_C13", "North_N15",
"North_C13"), row.names = c(NA, -9L), class = "data.frame")

opar <- par()

## Plot results
par(oma = c(1, 0, 0, 0), mar = c(4, 5, 2, 2))           
plot(1,1,xlim=c(0:1.0), ylim=c(0:1.0), type="n", las=1, bty="n", main = NULL,
     ylab=expression(paste("Correlation Coefficient (r) for ", delta ^{15},"N ",
                     "\u0028","\u2030","\u0029")),
     xlab=expression(paste("Correlation Coefficient (r) for ", delta ^{13},"C ",
                     "\u0028","\u2030","\u0029")))

points(WW_corr$South_N15, WW_corr$South_C13, pch = 23, cex = 1.25, 
       bg ="antiquewhite4", col = "antiquewhite4")
points(WW_corr$North_N15, WW_corr$North_C13, pch = 15, cex = 1.25,
       bg ="black")
axis(1, at = seq(0, 1.0, by = 0.1), labels = F, tick = TRUE, tck = -0.01)
axis(2, at = seq(0, 1.0, by = 0.1), labels = F, tick = TRUE, tck = -0.01)
abline(h=.86, v=.86, col = "gray60", lty = 2)
legend("topleft", c("North", "South"), pch = c(15, 23), 
       col = c("black", "antiquewhite4"), pt.bg = c("black", "antiquewhite4"),
       horiz=TRUE, bty = "n")

par(opar)

解决方案

使用asp=1作为绘图参数将由低级plot.window调用解释,并应为您提供统一的宽高比.使用ylim和xlim的调用有可能会与宽高比具体化发生冲突,并且asp应该占优势".到目前为止,这是一个非常令人印象深刻的第一张R图.以及极好的问题构造.高分.

一个令人讨厌的记录是您对结构xlim=c(0:1.0)的使用.由于xlim需要两个元素向量,因此我希望xlim = c(0,1).如果更改为其他限制,则击键次数更少,将来也不会出错,因为如果您尝试使用"0:2.5"进行操作,则:"运算符会给您带来意想不到的结果.

I am plotting correlation coefficients (values = 0.0:1.0) for two isotopes measured in each individual from two populations. I would like to have a fixed aspect-ratio for my scatter-plot so that the x- and y-axis are exactly the same size no matter the graphics device. Suggestions?

This is my first plot in R, any comments on refinements to my code is appreciated? Finally, is it worth investing in learning the basic plotting techniques or should I jump right to ggplot2 or lattice?

My plot script:

## Create dataset
WW_corr <-
structure(list(South_N15 = c(0.7976495, 0.1796725, 0.5338347,
0.4103769, 0.7447027, 0.5080296, 0.7566544, 0.7432026, 0.8927161
), South_C13 = c(0.76706752, 0.02320767, 0.88429902, 0.36648357,
0.73840937, 0.0523504, 0.52145159, 0.50707858, 0.51874445), North_N15 = c(0.7483608,
0.4294148, 0.9283554, 0.8831571, 0.5056481, 0.1945943, 0.8492716,
0.5759033, 0.7483608), North_C13 = c(0.08114805, 0.47268136,
0.94975596, 0.06023815, 0.33652839, 0.53055943, 0.30228833, 0.8864435,
0.08114805)), .Names = c("South_N15", "South_C13", "North_N15",
"North_C13"), row.names = c(NA, -9L), class = "data.frame")

opar <- par()

## Plot results
par(oma = c(1, 0, 0, 0), mar = c(4, 5, 2, 2))           
plot(1,1,xlim=c(0:1.0), ylim=c(0:1.0), type="n", las=1, bty="n", main = NULL,
     ylab=expression(paste("Correlation Coefficient (r) for ", delta ^{15},"N ",
                     "\u0028","\u2030","\u0029")),
     xlab=expression(paste("Correlation Coefficient (r) for ", delta ^{13},"C ",
                     "\u0028","\u2030","\u0029")))

points(WW_corr$South_N15, WW_corr$South_C13, pch = 23, cex = 1.25, 
       bg ="antiquewhite4", col = "antiquewhite4")
points(WW_corr$North_N15, WW_corr$North_C13, pch = 15, cex = 1.25,
       bg ="black")
axis(1, at = seq(0, 1.0, by = 0.1), labels = F, tick = TRUE, tck = -0.01)
axis(2, at = seq(0, 1.0, by = 0.1), labels = F, tick = TRUE, tck = -0.01)
abline(h=.86, v=.86, col = "gray60", lty = 2)
legend("topleft", c("North", "South"), pch = c(15, 23), 
       col = c("black", "antiquewhite4"), pt.bg = c("black", "antiquewhite4"),
       horiz=TRUE, bty = "n")

par(opar)

解决方案

Using asp=1 as a parameter to plot will get interpreted by the low-level plot.window call and should give you a unitary aspect ratio. There is the potential that a call using ylim and xlim could conflict with an aspect ratio scpecification and the asp should "prevail". That's a very impressive first R graph, by the away. And an excellent question construction. High marks.

The one jarring note was your use of the construction xlim=c(0:1.0). Since xlim expects a two element vector, I would have expected xlim=c(0,1). Fewer keystrokes and less subject to error in the future if you changed to a different set of limits, since the ":" operator would give you unexpected results if you tried that with "0:2.5".

这篇关于如何为散点图定义固定的宽高比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆