Manually defining the colours of a wireframe - r

I am plotting some surfaces in R using the lattice package. I can't find a way to choose the colours of the surface. Here is an example:
Here is an example of how i plot each:
theseCol=heat.colors(150)
mm=paste("WB numbers where present\n(",nstoch," sims)",sep="")
WBnumbers=wbPrev_series
rownames(WBnumbers)=KList
colnames(WBnumbers)=iMwbList
wireframe(WBnumbers, zlim=c(0,max(wbPrev_series,na.rm=TRUE)), colorkey=FALSE,
col.regions=theseCol, scales = list(arrows = FALSE), drape = TRUE,
main=mm, zlab="", xlab="K", ylab="iMwb")
I would like for the first surface to be as it is, but for the others to be coloured not by their z levels but by the 1st surface's z levels. I tried multiple things but wireframe always accepts the colours i give as the possible ranges for the current variable.
Anyway this could be done?
Thanks

Here is the answer Dave W. posted some years back on the R-help mailing list. You probably can google up the entire thread.
From: David Winsemius
Following the advice in help(wirefrane) you need to look at the
levelplot section for advice re: a proper specification to colorkey
and follow the appropriate links in the help pages. Whether your data
is a proper input to wireframe cannot be determined from the included
information, although I suppose your reported success suggests it is.
This is an untested (since there was nothing to test) wild-assed guess
after reading the material I pointed to:
wireframe(data.m,aspect = c(0.3), shade=TRUE, screen = list(z = 0, x =
-45),
light.source = c(0,0,10), distance =
0.2,zlab="Freq",xlab="base",ylab="Fragment",
col=level.colors(x, at = do.breaks(range(data.m), 30),
col.regions = colorRampPalette(c("red", "white",
"blue")(30))
)
EDIT:
Per Josh's request, I played around a bit. The following will apply color shading (drape):
wireframe(dmat,drape=TRUE,col='black',col.regions = colorRampPalette(c("red", "white", "blue"))(30) )
Which sets the "drape" colors but not the gridlines themselves.
It's a darn shame that wireframe doesn't respect par(new=TRUE), because if it did we could slice the data matrix into z-ranges and overplot one color at a time.
I will have to check my "archive" of old experiments w/ R graphics when I get home, but I think I ended up using the scatterplot3d package to get data-dependent grid colors.

Related

Layered ScatterD3 plots?

Is it possible to overlay multiple layers of ScatterD3 plots on top of one another? I haven't been able to find this anywhere in either the vignettes or searching StackExchange/Google.
I'm curious, since folks have been able to make PCA Vector Loading plots using ScatterD3. If one could overlay this on top of another plot with the points (akin to what's possible with ggplot2 or ggvis layers), you could have a gorgeous and interactive PCA plot. Additionally, you might be able to outline points (since point stroke currently isn't an option).
Does anyone have any insight or workarounds?
It is possible, but more difficult. I would recommend using plotly package. You'll be able to use the View tab in the RStudio and more easily examine your 3D scatter by rotating. The color scheme is also easier to add This post attempts to tackle a similar, though not identical. A good (free) tutorial for plotly can be found here through DataCamp.
Question answered here thanks to the author of ScatterD3. To generate a full PCA plot, you need to redefine the dataframe being plotted like so:
library(FactoMineR)
library (ScatterD3)
out<-PCA(iris[,1:4],scale.unit = TRUE, graph=FALSE)
cc1<-data.frame(out$ind$coord)
cc2<-data.frame(out$var$coord)
points <- data.frame(x = cc1$Dim.1,
y = cc1$Dim.2,
color = iris$Species,
lab = row.names(iris),
type = rep("point", 150))
arrows <- data.frame(x = cc2$Dim.1,
y = cc2$Dim.2,
color = "Blue",
lab = row.names(cc2),
type = rep("arrow", 4))
data1 <- rbind(points, arrows)
scatterD3(data1, x = x, y = y,
lab = lab, type_var = data1$type, col_var = color)

Manually creating an object that looks like a heatmap color key

I'm working on trying to create a key for a heatmap, but as far as I know, I cannot use the existing tools for adding a legend since I've generated the colors myself (I manually turn a scaled variable into rgb values for a short rainbow ( [255,0,0] to [0,0,255] ).
Basically, all I want to do is use the rightmost 10th of the screen to create a rectangle with these 10 colors: "#0000FF", "#0072FF", "#00E3FF", "#00FFAA", "#00FF38", "#39FF00", "#AAFF00", "#FFE200", "#FF7100", "#FF0000"
with three numerical labels - at 0, max/2, and max
In essence, I want to manually produce an object that looks like a rudimentary heatmap color key.
As far as I know, split.screen can only split the screen in half, which isn't what I'm looking for. I want the graphic I already know how to produce to take up the leftmost 90% of the screen, and I want this colored rectangle to take up the other 10%.
Thanks.
EDIT: I greatly appreciate the advice about the best way to the the plot - that said, I still would like to know the best way to do the task originally asked - creating the legend by hand; I already am able to produce the exact heatmap graphic that I'm looking for - the false coloring wasn't the only problem with ggplot that I was having - it was just the final factor convincing me to switch. I need a non ggplot solution.
EDIT #2: This is close to the solution I am looking for, except this only goes up to 10 instead of accepting a maximum value as a parameter (I will be running this code on multiple data-sets, all with different maximum values - I want the legend to reflect this). Additionally, if I change the size of the graph, the key falls apart into disconnected squares.
Take a look at the layouts function (link). I think you want something like this:
layout(matrix(c(1,2), 1, 2, byrow = TRUE), widths=c(9,1))
## plot heatmap
## plot legend
I would also recommend the ggplot2 package and the geom_tile function which will take care of all of this for you.
Assuming your data is in a data frame with the x and y coordinates and heatmap value (e.g. gdat <- data.frame(x_coord=c(1,2,...), y_coord=c(1,1,...), val=c(6,2,...))) Then you should be able to produce your desired heat map plot with the following ggplot command:
ggplot(gdat) + geom_tile(aes(x=x_coord, y=y_coord, fill=val)) +
scale_fill_gradient(low="#0000FF", high="#FF0000")
To get your data into the following format you may want to look into the very useful reshape2 package.
Given a script no ggplot restriction on this answer here is how one could produce the plot with just base R.
colors <- c("#0000FF", "#0072FF", "#00E3FF", "#00FFAA", "#00FF38",
"#39FF00", "#AAFF00", "#FFE200", "#FF7100", "#FF0000")
layout(matrix(c(1,2), 1, 2, byrow = TRUE), widths=c(9,1))
plot(rnorm(20), rnorm(20), col=sample(colors, 20, replace=TRUE))
par(mar=c(0,0,0,0))
plot(x=rep(1,10), y=1:10, col=colors, pch=15, cex=7.1)
You may have to adjust the cex for your device.

Odd axis label behaviour after setting xlim in pyramid.plot [plotrix]

I'm trying to make an "opposing stacked bar chart" and have found pyramid.plot from the plotrix package seems to do the job. (I appreciate ggplot2 will be the go-to solution for some of you, but I'm hoping to stick with base graphics on this one.)
Unfortunately it seems to do an odd thing with the x axis, when I try to set the limits to non integer values. If I let it define the limits automatically, they are integers and in my case that just leaves too much white space. But defining them as xlim=c(1.5,1.5) produces the odd result below.
If I understand correctly from the documentation, there is no way to pass on additional graphical parameters to e.g. suppress the axis and add it on later, or let alone define the tick points etc. Is there a way to make it more flexible?
Here is a minimal working example used to produce the plot below.
require(plotrix)
set.seed(42)
pyramid.plot(cbind(runif(7,0,1),
rep(0,7),
rep(0,7)),
cbind(rep(0,7),
runif(7,0,1),
runif(7,0,1)),
top.labels=NULL,
gap=0,
labels=rep("",7),
xlim=c(1.5,1.5))
Just in case it is of interest to anyone else, I'm not doing a population pyramid, but rather attempting a stacked bar chart with some of the values negative. The code above includes a 'trick' I use to make it possible to have a different number of sets of bars on each side, namely adding empty columns to the matrix, hopefully someone will find that useful - so sorry the working example is not as minimal as it could have been!
Setting the x axis labels using laxlab and raxlab creates a continuous axis:
pyramid.plot(cbind(runif(7,0,1),
rep(0,7),
rep(0,7)),
cbind(rep(0,7),
runif(7,0,1),
runif(7,0,1)),
top.labels=NULL,
gap=0,
labels=rep("",7),
xlim=c(1.5,1.5),
laxlab = seq(from = 0, to = 1.5, by = 0.5),
raxlab=seq(from = 0, to = 1.5, by = 0.5))

Scaling heat map colours for multiple heat maps

So I have a bunch of matrices that I am trying to plot as a heatmaps. I am using the heatmap.2() function in the ggplot2 packaage.
I have been trying for quite some time with it, and I am sure there is a very simple fix, but my issue is this:
How do I keep the colours consistent between heatmaps? For example, to make the values that provide the colours absolute as opposed to relative.
I have tried doing something similar to this question:
R/ggplot: Reuse color key for multiple heat maps
But I was unable to figure out the ggplot function; I kept receiving an error message stating that there were "no layers in plot".
After reading the comments on the above question, I tried using scales::rescale() and discrete_scale() but the former does not remove the problem, while the latter did not work.
I am fully aware that I might be doing something very simple wrong, and just being a bit of an idiot, but for the life of me I can't figure out where I am going wrong.
As for the data itself, I am trying to plot 10 matrices/heatmaps, each 10x10 cells (showing change over time) and the values in the cells range from 1.0 to 1.2.
As an example, this is the code I am using (once I have my 10x10 matrix).
Matrix1<-matrix(data=(runif(100,1.0,1.2)),nrow=10,ncol=10)
heatmap.2(Matrix1, Colv=NA, Rowv=NA, dendrogram="none",
trace="none", key=F, cellnote=round(Matrix1,digits=2),
notecex=1.6, notecol="black",
labRow=seq(10,100,10), labCol=seq(10,100,10),
main="Title1", xlab="Xlab1", ylab="Ylab1"
)
So any help with either figuring out how to create the scaled values for the heatmap.2() function, or how I can use the ggplot() function would be greatly appreciated!
It's important to note that heatmap.2 is not a ggplot2 function. The ggplot2 package is not necessarily compatible with all plotting types. If you look at the ?heatmap.2 help page, in the upper left corner it shows you where the function is from. heatmap.2 {gplots} means that function comes from the gplots package. These are different pacakges so they have different rules how they work.
To get the same colors across different heatmaps, you want to explicitly get the breaks= parameter. By default it splits the observed range of the data into equal chunks. But since each data set may have a different min and max, these chunks may have different start and end points. By specifying breaks, you can make them all consistent. Since your data ranges from 1 to 1.2, you can set
mybreaks <- seq(1.0, 1.2, length.out=7)
and then in your call add
heatmap.2(Matrix1, Colv=NA, Rowv=NA, dendrogram="none",
...
breaks=mybreaks,
...
)
That should make them all match up.
Maybe this will help you. With the following code multiple heatmaps are stored in a list and displayed in a grid later on. This will allow you to control the colours of each heatmap since each heatmap is created separately. So in this case I chose to use green and red for the number range in each chart.
data(mtcars)
require(ggplot2)
require(gridExtra)
myplotslist2 <- list()
var = c("mpg", "wt", "drat")
new = cbind(mtcars, "variable")
new = cbind(car = rownames(mtcars), new)
for (i in 1:length(var)){
t= paste("new[[\"variable\"]] = \"", var[[i]],"\"; a = ggplot(new, aes(variable, car)) + geom_tile(aes(fill = ", var[[i]], "),colour = \"white\") + scale_fill_gradient(low = \"red\", high = \"green\") + theme(axis.title.y=element_blank(), axis.text.y=element_blank(),legend.position=\"none\"); myplotslist2[[i]] = a")
eval(parse(text=t))
}
grid.arrange(grobs=myplotslist2, ncol=length(var))
The result looks like this:
I hope this helps.
I explain more in my blogpost. https://dwh-businessintelligence.blogspot.nl/2016/05/pca-3d-and-k-means.html

R: Plotting one ECDF on top of another in different colors

I have a couple of cumulative empirical density functions which I would like to plot on top of each other in order to illustrate differences in the two curves. As was pointed out in a previous question, the function to draw the ECDF is simply plot(Ecdf()) And as I read the fine manual page, I determined that I can plot multiple ECDFs on top of each other using something like the following:
require( Hmisc )
set.seed(3)
g <- c(rep(1, 20), rep(2, 20))
Ecdf(c( rnorm(20), rnorm(20)), group=g)
However my curves sometimes overlap a bit and can be hard to tell which is which, just like the example above which produces this graph:
I would really like to make the color of these two CDFs different. I can't figure out how to do that, however. Any tips?
If memory serves, I have done this in the past. As I recall, you needed to trick it as Ecdf() is so darn paramterised. I think in help(ecdf) it hints that it is just a plot of stepfunctions, so you could estimate two or more ecdfs, plot one and then annotate via lines().
Edit Turns out it is as easy as
R> Ecdf(c(rnorm(20), rnorm(20)), group=g, col=c('blue', 'orange'))
as the help page clearly states the col= argument. But I have also found some scriptlets where I used plot.stepfun() explicitly.
You can add each curve one at a time (each with its own style), e.g.
Ecdf(rnorm(20), lwd = 2)
Ecdf(rnorm(20),add = TRUE, col = 'red', lty = 1)
Without using Ecdf (doesn't look like Hmisc is available):
set.seed(3)
mat <- cbind(rnorm(20), rnorm(20))
matplot(apply(mat, 2, sort), seq(20)/20, type='s')

Resources