我对R很陌生,我正在编写一些输出散点图矩阵的代码。数据框架采用以下格式:
A B C D
2 3 0 5
8 9 5 4
0 0 5 3
7 0 0 0我的数据集可以运行到100-1000 s行和10-10 0 0 s的列,具有一个很大的值(因此日志转换我的数据)。
这段代码使我在增强基本绘图方面取得了一定的成功(请参阅嵌入式图像):
panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1), xlog = FALSE, ylog = FALSE)
r <- abs(cor(x, y))
txt <- format(c(r, 0.123456789), digits = digits)[1]
txt <- paste(prefix, txt)
if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
text(0.5, 0.5, txt, cex = cex.cor * r)
}
# Add regression line to plots.
my_line <- function(x,y,...){
points(x,y,...)
LR <- lm(log(x) ~ log(y), data = SP)
abline(LR, col = "red", untf = TRUE)
}
# Plot scatter plot matrices.
pairs(mydataframe, pch = 20, main = "test",
cex = 0.125, cex.labels = 1,
xlim = c(100, 1e9),
ylim = c(100, 1e9),
upper.panel = panel.cor,
lower.panel = my_line,
log = "xy")'示例
问题1-不是在上面的面板中得到R^2值,而是得到NAs。我该怎么纠正呢?
问题2-我想删除调整R^2值的文本大小与相关性成比例的函数。我知道这是在panel.cor,但不确定哪一部分将需要删除或调整。
事先非常感谢
编辑:2016年06月8日
我发现了一项也简化了代码的工作:
panel.cor <- function(x, y, digits = 2, cex.cor, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
# correlation coefficient
r <- cor(x, y)
txt <- format(c(r, 0.123456789), digits = digits)[1]
txt <- paste("r= ", txt, sep = "")
text(0.5, 0.6, txt)
}
# add regression line to plots.
my_line <- function(x,y,...)
{
points(x,y,...)
LR <- lm(x ~ y, data = SP)
abline(LR, col = "red", untf = TRUE)
}
# Plot scatterplot matrices.
pairs(SP, pch = 20, main = "test",
cex = 0.125, cex.labels = 1,
upper.panel = panel.cor,
lower.panel = my_line)例2
问题似乎是缺少值,即0。我最初将这些值更改为NA值,以便我可以使用日志刻度。这与日志转换相结合,导致上面板中缺少R^2值。
理想情况下,我想要一个原木秤。有什么办法可以做到这一点,而不引入死板的问题?
澄清-我希望在散点图(下面板)和x轴在直方图(对角线面板)的日志(xy)标度。我今天一直在玩它,但我不能完全得到我想要的。也许我对两人的要求太高了。任何帮助都将不胜感激。
编辑:2016年6月10日
成功!....well大约99%快乐。
我对对角线面板进行了修改-添加了柱状图,对上面板添加了p-值(用于添加直方图的基本代码,因为x轴上使用了日志缩放,因此需要调整直方图)。如果我的描述不准确或不正确,请随时更正:
library(lattice)
DF <- read.csv("File location", header = TRUE)
DF.1 <- DF+1 # Added small epsilon to data frame otherwise plot errors arise due to missing values.
# Function to calculate R^2 & p-value for upper panels in pairs() - scatterplot matrices.
panel.cor <- function(x, y, digits = 3, cex.cor, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1), xlog = FALSE, ylog = FALSE) # xlog/ylog: ensures that R^2 and p-values display in upper panel.
# Calculate correlation coefficient and add to diagonal plot.
r <- cor(x, y)
txt <- format(c(r, 0.123456789), digits = digits)[1]
txt <- paste("r= ", txt, sep = "")
text(0.5, 0.7, txt, cex = 1.25) # First 2 arguments determine postion of R^2-value in upper panel cells.
# Calculate P-value and add to diagonal plot.
p <- cor.test(x, y)$p.value
txt2 <- format(c(p, 0.123456789), digits = digits)[1]
txt2 <- paste("p= ", txt2, sep = "")
if(p<0.01) txt2 <- paste("p= ", "<0.01", sep = "")
text(0.5, 0.3, txt2, cex = 1.25) # First 2 arguments determine postion of p-value in upper panel cells.
}
# Function to calculate frequency distribution and plot histogram in diagonal plot.
panel.hist <- function(x, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0.5, 1.5, 0, 1.75), xlog = TRUE, ylog = FALSE) # xlog argument allows log x-axis when called in pairs.
h <- hist(log(x), plot = FALSE, breaks = 20)
breaks <- h$breaks; nB <- length(breaks)
y <- h$counts; y <- y/max(y)
rect(breaks[-nB], 0, breaks[-1], y, col = "cyan")
}
# add regression line to plots.
my_line <- function(x,y, ...)
{
points(x,y,...)
LR <- lm(log(x) ~ log(y), data = DF.1)
abline(LR, col = "red", untf = TRUE)
}
# Plot scatterplot matrices.
pairs(DF.1, pch = 20, main = "Chart Title",
cex = 0.75, cex.labels = 1.5, label.pos = 0.0001,
upper.panel = panel.cor,
lower.panel = my_line,
diag.panel = panel.hist,
log = ("xy"),
xlim = c(5, 1e9),
ylim = c(5, 1e9))药膏中的苍蝇:
1-对角线面板中的文本标签仅部分显示。我在"pairs()“中使用了"label.pos”参数的递减值,该参数将标签向下移动,直到它们出现。然而,不管我怎么降低这个值,他们都不会再动了。我试着从直方图函数中强迫这个位置,但这行不通。我希望有人能看到我错过了什么。谢谢.我还没有收到任何答复:
PS:我试着把第三张图片和我的成功情节联系起来,但是我因为缺乏reputation...groan而失败了。
编辑日期:2016年6月13日
解决了!我觉得有点傻。定位主标题在对角线面板上的修复是非常简单的,我花了很长时间尝试更复杂的方法来做到这一点。成对的"label.pos“参数应该是否定的!我使用了一个小值-0.0675,它将其放置在包含直方图的单元格顶部。
我希望其他人发现这是有用的。我将标记为已解决,但我希望对我的代码注释的任何评论,或如果有人看到使代码更有效率的方法。谢谢亚历克斯
发布于 2016-08-09 21:45:15
有时候我觉得自己很笨。回答我自己的question...who会有thought...slaps的头。请在我的文章中看到我找到的补丁。
https://stackoverflow.com/questions/37684237
复制相似问题