[英]log-log scale for y axis in R
I am trying to create a Weibull probability plot for censored data in R. To do so I need a log-log scale for the y axis and a log scale for the x axis. 我正在尝试为R中的审查数据创建一个Weibull概率图。为此,我需要对数坐标轴刻度为y,对数坐标轴为x。
The y axis is the probability (range 0 to 1) and the x axis is "time in days". y轴是概率(范围从0到1),x轴是“天数”。
I know that I can create logarithmic axis with log="xy" in the plot() function. 我知道我可以在plot()函数中使用log =“ xy”创建对数轴。 But I need a log-log scale for the y axis.
但是我需要对数轴刻度为y轴。
Is there a way to do that? 有没有办法做到这一点?
Thank you in advance! 先感谢您!
Data example: data$ 数据示例: data $
X cens survCount medianRank
136.5424 1 10 0.09090909
181.9756 1 9 0.18181818
192.4309 1 8 0.27272727
216.6145 1 7 0.36363636
224.3097 0 6 NA
254.4997 0 5 NA
285.1438 1 4 0.49090909
289.3991 1 3 0.61818182
295.9161 0 2 NA
309.9522 0 1 NA
X: times till failure X:直到失败的时间
cens: binary, 0 if censored cens:二进制,如果检查则为0
survCount: number of things alive before failure/censoring survCount:失败/检查之前存在的事物数
medianRanks: cumulated probability of failure 平均排名:失败的累积概率
Explanation: 说明:
So X is what I want on the log x axis and the medianRanks are what I want on the log-log y axis. 所以X是我在对数x轴上想要的,而位数Ranks是我想要在对数对数y轴上的东西。
The problem is that you can't calculate twice the logarithm from a number <1 becaue the first logarithm will give a negative number and you can't calculate a logarithm from a negative number. 问题是您不能从小于1的数字计算两倍的对数,因为第一个对数将给出负数,并且您不能从负数计算对数。 That is why I want to transform the axis and not the values.
这就是为什么我要变换轴而不是值的原因。
What I did so far: 到目前为止,我做了什么:
My workaround so far is to multiply my y values with a number (eg 1000) so that I don't have any values that are less than 1 and then calculate the log-log of these values. 到目前为止,我的解决方法是将y值与一个数字(例如1000)相乘,以使我没有小于1的任何值,然后计算这些值的对数-对数。 I then plot them, hide the axes and add new axes with the axis() function.
然后,我绘制它们,隐藏轴并使用axis()函数添加新轴。
data$medianRank <- data$medianRank*1000
loglogY <- log(log(data$medianRank))
logX <- log(data$X)
plot(logX, loglogY, yaxt="n", xaxt="n")
ylabels <- c(0.1, 0.2, 0.5, 0.7, 0.99)
yAt <- log(log(ylabels*1000))
axis(2, at=yAt, labels=ylabels)
xlabels <- c(100, 200, 300, 400)
xAt <- log(xlabels)
axis(1, at=xAt, labels=xlabels)
@mike1886 suggested to use the ggplot2 package. @ mike1886建议使用ggplot2软件包。 I had a look at it and what I found is quite promising.
我看了一下,发现的结果很有希望。 When one creates a ggplot one can add coord_trans() to transform the axes.
当创建ggplot时,可以添加coord_trans()来变换轴。 There are a few transformations available but I couldn't find a log-log one.
有一些可用的转换,但是我找不到对数。 Fortunately one can also write a custom transformation with the trans_new() function from the scales package.
幸运的是,还可以使用scales包中的trans_new()函数编写自定义转换。
My code so far for the new transformation: 到目前为止,我的代码已经进行了新的转换:
require(ggplot2)
require(scales)
loglog_trans <- function(){
trans <- function(x){ log(log(x)) }
inv <- function(x){ exp(exp(x)) }
trans_new("loglog", trans, inv)
}
wpp <- ggplot(data2, aes(ftime, medianRank)) + geom_point()
wpp
wpp + coord_trans("log10", "loglog")
But it is not working. 但这是行不通的。
Error in if (zero_range(range)) { : missing value where TRUE/FALSE needed In addition: Warning message: In log(log(x)) : NaNs produced
if(zero_range(range)){中的错误:需要TRUE / FALSE时缺少值另外:警告消息:在log(log(x))中:产生NaN
You can try using ggplot2 (this is a very nice and complete plotting package) in R. For example, consider looking at the page: http://www.cookbook-r.com/Graphs/Axes_(ggplot2)/#axis-transformations-log-sqrt-etc 您可以尝试在R中使用ggplot2(这是一个非常漂亮且完整的绘图程序包)。例如,考虑查看以下页面: http ://www.cookbook-r.com/Graphs/Axes_(ggplot2)/#axis- 转换日志SQRT等
This will allow you to do whatever you would like to the axes. 这将使您可以对轴进行任何操作。 For example,
例如,
m <- qplot(rating, log10(votes), data=subset(movies, votes > 1000), na.rm = TRUE)
m + scale_y_log10() + scale_x_log10()
I suspect that you are being expected to plot "complementary log -log" which probably means you are being asked to plot the log of the negative log. 我怀疑您期望绘制“互补对数-log”,这可能意味着您被要求绘制负对数的对数。 I admit that this is not exactly how such plots usually appear.
我承认,这并不总是这样的情节通常出现。 What I usually see in texts regarding survival analysis is a rising trend and one should see roughly parallel lines (with positive slope) for log(-log(survival)) plotted against time when the proptional hazards assumption is met.
在关于生存分析的课文中,我通常会看到一种上升的趋势,应该看到在满足危险性假设时,log(-log(survival))随时间绘制的大致平行线(具有正斜率)。
dat <- read.table(text=" X cens survCoun
136.5424 1 10 0.09090909
181.9756 1 9 0.18181818
192.4309 1 8 0.27272727
216.6145 1 7 0.36363636
224.3097 0 6 NA
254.4997 0 5 NA
285.1438 1 4 0.49090909
289.3991 1 3 0.61818182
295.9161 0 2 NA
309.9522 0 1 NA", header=TRUE)
with( dat, plot( log(X), log( - log(medianRank) ) ) )
So consider this where I am taking survCount/10 to be the proportion surviving at time= X: 因此,请考虑以下情况:我将survCount / 10用作时间= X时幸存的比例:
png(); with( dat, plot( log(X),
log( - log(survCount/max(survCount) ) )
) )
dev.off()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.