Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 散点图和仅一个直方图一起绘制_R - Fatal编程技术网

R 散点图和仅一个直方图一起绘制

R 散点图和仅一个直方图一起绘制,r,R,我想用“散点图”和右侧的直方图来可视化时间序列数据,但我还没有弄清楚如何关闭上方的直方图 代码示例: install.packages("psych") library(psych) data = matrix(rnorm(n=100000,mean=2,sd=1.5), nrow = 100, ncol=1000) fs = list() fs$p_Z = 1*(data>2) n_p = 1; for(i in floor(seq(1,dim(data)[2],length.out

我想用“散点图”和右侧的直方图来可视化时间序列数据,但我还没有弄清楚如何关闭上方的直方图

代码示例:

install.packages("psych")
library(psych)
data = matrix(rnorm(n=100000,mean=2,sd=1.5), nrow = 100, ncol=1000)
fs = list()
fs$p_Z = 1*(data>2)
n_p = 1;
  for(i in floor(seq(1,dim(data)[2],length.out=n_p)))
{
  scatter.hist(x = rep(1:length(data[,i])), y = data[,i],
               xlab = 'observations',
               ylab = 'log(TPM)',
               title = 'Mixture Plot',
               col = c("red","blue")[fs$p_Z[,i]+1], 
               correl = FALSE, ellipse = FALSE, smooth = FALSE)
}
结果:

预期结果: 和我的一样,但是上面没有柱状图。即,只有对数(TPM)右侧的直方图


注意:我正在使用一个看似简单易用的工具,但却找不到如何关闭一个直方图。

在灵活性结束的地方,黑客攻击就开始了。如果你看一下scatter.hist函数,你会发现它是R基本图形的基本用法。以下修改的代码将创建所需的绘图:

scat.hist <- function(x, y, xlab = NULL, ylab = NULL, title = "", ...) {

## Create layout
layout(matrix(c(1,2),1,2,byrow=TRUE), c(3,1), c(1,3))

## Plot scatter
par(mar=c(5,5,3,1))
plot(x= x, y = y, xlab = xlab, ylab = ylab, main = title, ...)

## Right histogram
yhist <- hist(y, plot = FALSE, breaks = 11)
par(mar=c(5,2,3,1))
mp <- barplot(yhist$density, space=0, horiz=TRUE, axes = FALSE)
## Density
d <- density(y, na.rm = TRUE, bw = "nrd", adjust = 1.2)
temp <- d$y
        d$y <- (mp[length(mp)] - mp[1] + 1) * (d$x - min(yhist$breaks))/(max(yhist$breaks) - min(yhist$breaks))
        d$x <- temp
lines(d)
}

回答得好,破解得好!
i = 1
scat.hist(x = seq_along(data[,i]), y = data[,i], col = c("red", "blue")[fs$p_Z[,i]+1], xlab = 'observations', ylab = 'log(TPM)', title = 'Mixture Plot')