r - R 中的对数标度图

标签 r plot

我想绘制聚类系数和平均最短 -
路径作为 Watts-Strogatz 模型参数 p 的函数,如下所示:

enter image description here

这是我的代码:

library(igraph)
library(ggplot2)
library(reshape2)
library(pracma)
p <- #don't know how to generate this?
trans <- -1
path <- -1

for (i in p) {
  ws_graph <- watts.strogatz.game(1, 1000, 4, i)
  trans <-c(trans, transitivity(ws_graph, type = "undirected", vids = NULL,
               weights = NULL))
  path <- c(path,average.path.length(ws_graph))
}
#Remove auxiliar values
trans <- trans[-1]
path <- path[-1]
#Normalize them
trans <- trans/trans[1]
path <- path/path[1]

x = data.frame(v1 = p, v2 = path, v3 = trans)

plot(p,trans, ylim = c(0,1), ylab='coeff')
par(new=T)
plot(p,path, ylim = c(0,1), ylab='coeff',pch=15)

我应该如何继续制作这个 x 轴?

最佳答案

您可以生成 p 的值使用如下代码:

p <- 10^(seq(-4,0,0.2))

您希望 x 值在 log10 尺度上均匀分布。这意味着您需要采用均匀间隔的值作为以 10 为底的指数,因为 log10 比例采用 x 值的 log10,这是完全相反的操作。

有了这个,你已经很远了。你不需要par(new=TRUE) ,您可以简单地使用函数 plot后跟函数 points .后者不会重绘整个情节。使用参数 log = 'x'告诉 R 你需要一个对数 x 轴。这只需要在plot中设置函数,points function 和所有其他低级绘图函数(那些不替换而是添加到绘图中的函数)尊重此设置:
plot(p,trans, ylim = c(0,1), ylab='coeff', log='x')
points(p,path, ylim = c(0,1), ylab='coeff',pch=15)

plot

编辑:如果你想复制上图的对数轴外观,你必须自己计算它们。在互联网上搜索“R log10 minor ticks”或类似内容。下面是一个简单的函数,可以计算对数轴主要和次要刻度的适当位置
log10Tck <- function(side, type){
   lim <- switch(side, 
     x = par('usr')[1:2],
     y = par('usr')[3:4],
     stop("side argument must be 'x' or 'y'"))
   at <- floor(lim[1]) : ceil(lim[2])
   return(switch(type, 
     minor = outer(1:9, 10^(min(at):max(at))),
     major = 10^at,
     stop("type argument must be 'major' or 'minor'")
   ))
}

定义好这个函数后,通过上面的代码,就可以调用axis(...)里面的函数了。函数,绘制轴。建议:将函数保存在自己的 R 脚本中,然后使用函数 source 在计算顶部导入该脚本。 .通过这种方式,您可以在以后的项目中重复使用该功能。在绘制轴之前,您必须阻止 plot从绘制默认轴,所以添加参数 axes = FALSE给您的 plot称呼:
plot(p,trans, ylim = c(0,1), ylab='coeff', log='x', axes=F)

然后您可以使用由生成的刻度位置生成轴
新功能:
axis(1, at=log10Tck('x','major'), tcl= 0.2) # bottom
axis(3, at=log10Tck('x','major'), tcl= 0.2, labels=NA) # top
axis(1, at=log10Tck('x','minor'), tcl= 0.1, labels=NA) # bottom
axis(3, at=log10Tck('x','minor'), tcl= 0.1, labels=NA) # top
axis(2) # normal y axis
axis(4) # normal y axis on right side of plot
box()

plot with pretty axes

作为第三种选择,当您在原始帖子中导入 ggplot2 时:相同,没有以上所有内容,使用 ggplot:
# Your data needs to be in the so-called 'long format' or 'tidy format' 
# that ggplot can make sense of it. Google 'Wickham tidy data' or similar
# You may also use the function 'gather' of the package 'tidyr' for this
# task, which I find more simple to use.  
d2 <- reshape2::melt(x, id.vars = c('v1'), measure.vars = c('v2','v3'))
ggplot(d2) +
   aes(x = v1, y = value, color = variable) + 
   geom_point() + 
   scale_x_log10()

ggplot plot

关于r - R 中的对数标度图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47890742/

相关文章:

r - 操作图例 : tmap 中的 "missing"标签

r - R 中多边形的长度

r - 为现有列值创建新顺序而不重新排序数据框中的行 - R

matlab - 在matlab中绘制微分误差

python - 如何绘制特征的变化

r - 如何以交替顺序在 R 中使用矢量化函数?

r - 如何从 party:::ctree 模型中删除训练数据?

Python-如何在积分中绘制不是被积分值的参数

python - CDF累积分布函数误差

R 为什么 plot.xts 在调用线后创建额外的图形?