[英]Logarithmic scale plot in R
I want to plot the clustering coefficient and the average shortest- path as a function of the parameter p of the Watts-Strogatz model as following: 我想根据Watts-Strogatz模型的参数p绘制聚类系数和平均最短路径,如下所示:
And this is my code: 这是我的代码:
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)
How should I proceed to make this x-axis? 我应该如何制作这个X轴?
You can generate the values of p
using code like the following: 您可以使用如下代码生成
p
的值:
p <- 10^(seq(-4,0,0.2))
You want your x values to be evenly spaced on a log10 scale. 您希望x值以log10刻度均匀分布。 This means you need to take evenly spaced values as the exponent for the base 10, because the log10 scale takes the log10 of your x values, which is the exact opposite operation.
这意味着您需要将均匀间隔的值作为以10为底的指数,因为log10刻度取x值的log10,这与操作正好相反。
With this, you are already pretty far. 有了这个,您已经很遥远了。 You don't need
par(new=TRUE)
, you can simply use the function plot
followed by the function points
. 您不需要
par(new=TRUE)
,只需使用函数plot
和函数points
。 The latter does not redraw the whole plot. 后者不会重绘整个情节。 Use the argument
log = 'x'
to tell R you need a logarithmic x axis. 使用参数
log = 'x'
告诉R您需要一个对数的x轴。 This only needs to be set in the plot
function, the points
function and all other low-level plot functions (those who do not replace but add to the plot) respect this setting: 仅需要在
plot
功能, points
功能和所有其他低级绘图功能(那些不替换但添加到绘图中的功能)中进行设置即可,请遵守以下设置:
plot(p,trans, ylim = c(0,1), ylab='coeff', log='x')
points(p,path, ylim = c(0,1), ylab='coeff',pch=15)
EDIT: If you want to replicate the log-axis look of the above plot, you have to calculate them yourselves. 编辑:如果要复制上图的对数轴外观,则必须自己计算。 Search the internet for 'R log10 minor ticks' or similar.
在互联网上搜索“ R log10次刻度”或类似内容。 Below is a simple function which can calcluate the appropriate position for log axis major and 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'")
))
}
After you have defined this function, by using the above code, you can call the function inside the axis(...)
function, which draws axes. 定义此函数后,使用上面的代码,可以在
axis(...)
函数内部调用该函数,该函数绘制轴。 As a suggestion: save the function away in its own R script and import that script at the top of your calculation using the function source
. 建议:将函数保存在自己的R脚本中,然后使用函数
source
在计算的顶部导入该脚本。 By this means, you can reuse the function in future projects. 这样,您可以在以后的项目中重用该功能。 Prior to drawing the axes, you have to prevent
plot
from drawing default axes, so add the parameter axes = FALSE
to your plot
call: 在绘制坐标轴之前,必须防止
plot
绘制默认坐标轴,因此将参数axes = FALSE
添加到plot
调用中:
plot(p,trans, ylim = c(0,1), ylab='coeff', log='x', axes=F)
Then you may generate the axes, using the tick positions generated by the new function: 然后,您可以使用新功能生成的刻度位置生成轴:
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()
As a third option, as you are importing ggplot2 in your original post: The same, without all of the above, with ggplot: 第三种选择是,在原始帖子中导入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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.