r - ScatterPlot and ONLY one Histogram plot together -
i want visualize time series data 'scatter plot' , histogram on right side, haven't been able figure out how turn off histogram on upper side.
code example:
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) }
result:
expected result: same 1 have no histogram on upper side. i.e., histogram on right side log(tpm).
note: using psych package, scatter.hist function seemed easy , nice use, couldn't find how turn off 1 histogram.
where flexibility ends, hacking begins. if @ scatter.hist
function, see pretty basic usage of r base graphics. following modified code create plot want:
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) }
let's try first row:
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')
Comments
Post a Comment