I am trying to construct a graph consisting of 2-3 filled.contour plots next to each other. The color scale is the same for all plots, and I would like only one z value key plot. I am having difficulties to do this with par(mfrow=c(1,3))
Example code:
x <- 1:5
y <- 1:5
z <- matrix(outer(x,y,"+"),nrow=5)
filled.contour(x,y,z)
filled.contour(x,y,z,color.palette=rainbow)
z2 <- z
z2[5,5] <- Inf
filled.contour(x,y,z2,col=rainbow(200),nlevels=200)
Is it possible to stack 2-3 of these plots next to each other with only one z value color key? I can do this in GIMP but I was wondering if it is natively in R possible.
No I do not think this is possible in filled.contour.
Although extensions have been written for you already. To be found here, here and here and a legend code here.
[If you are using the filled.contour3 function referred to on those sites, and using a more recent version, then you need to use the upgrade fix referred to in this SO post].
Using those codes I produced:
It can be solved with cowplot.
The clue is the cowplot::draw_plot function which successfully represent the plot (cowplot::draw_grob(~filled.contour(x,y,z))).
Later the grob representations can be binded.
The possible problem is that each plot will have own legend.
x <- 1:5
y <- 1:5
z <- matrix(outer(x,y,"+"),nrow=5)
z2 <- z
z2[5,5] <- Inf
cowplot::plot_grid(~filled.contour(x,y,z), ~filled.contour(x,y,z,color.palette=rainbow), ~filled.contour(x,y,z2,col=rainbow(200),nlevels=200))
Created on 2023-02-14 with reprex v2.0.2
Related
I have 23 different groups,each of them consists of from 7 to 20 individual samples (totally approximately 350-400 observations) with their own x,y & z coordinates. I'd like to produce 3D plot based on the data i have by means of plot3d function of rgl R package. It's not a big deal in general. The problem, that i'd like to make each one from the mentioned above 23 groups to be easy distinguishable on the 3D plot. I tried to use different colors for each group, but unfortunately it's not possible to find a 23 well recognizable by human eyes colors. I was thinking about pch parameter like in the plot function of base R library. But, again, as i can see there is not such option in the plot3d function. Besides, i have to explain, that there are too much points in my data set and adding the labels to each point (e.g. with text3d rgl function) is not a good idea (they will overlap with each other and give in result some kind of a mess on the 3D plot). Is there way to figure out it (i gues it's very common problem)? Thank you in advance!
Below is code of some toy example for better explanation:
# generate data
prefix=rep("ID",69)
suffix=rep(1:23,3)
suffix_2=as.character(suffix[order(suffix)])
titles_1=paste(prefix,suffix,sep="_")
titles_2=titles_1[order(titles_1)]
x=1:69
y=x+20
z=x+50
df=data.frame(titles_2,x,y,z)
# load rgl library
library('rgl')
# make 3D plot
plot3d(x,y,z)
If you like living on the bleeding edge, there's a new function rgl::pch3d() that draws symbols using the same codes as points() does
in base graphics. It's in rgl 0.95.1475, available on R-forge (and within a few hours on Github; see How do I install the latest version of rgl?). It's not completely working with rglwidget() yet.
The example code
open3d()
i <- 0:25; x <- i %% 5; y <- rep(0, 26); z <- i %/% 5
pch3d(x, y, z, pch = i, bg = "green")
text3d(x, y, z + 0.3, i)
pch3d(x + 5, y, z, pch = LETTERS[i+1])
text3d(x + 5, y, z + 0.3, i+65)
produces this display (after some resizing and rotation):
It's not perfect, but how about using letters a-w to distinguish the groups?
with(df,plot3d(x,y,z))
with(df,text3d(x,y,z,texts=letters[titles_2]))
Because i'm going to use the 3D plot for publication purposes i used this solution for now. It's not pretended to be the best one.
# generate data
prefix=rep("ID",69)
suffix=rep(1:23,3)
suffix_2=as.character(suffix[order(suffix)])
titles_1=paste(prefix,suffix,sep="_")
titles_2=titles_1[order(titles_1)]
x=1:69
y=x+20
z=x+50
df=data.frame(titles_2,x,y,z)
# load rgl library
library('rgl')
# load randomcoloR library
library(randomcoloR)
# create a custom palette
palette <- distinctColorPalette(23)
palette(palette)
# make 3D plot
plot3d(x,y,z,size = 10,col=suffix[order(suffix)])
To create a parallel coordinate plot I wanted to use ggparcoord() function in package GGally. The following codes show a reproducible example.
set.seed(3674)
k <- rep(1:3, each=30)
x <- k + rnorm(mean=10, sd=.2,n=90)
y <- -2*k + rnorm(mean=10, sd=.4,n=90)
z <- 3*k + rnorm(mean=10, sd=.6,n=90)
dat <- data.frame(group=factor(k),x,y,z)
library(GGally)
ggparcoord(dat,columns=1:4,groupColumn = 1)
Notice in the picture that the color for group was continuous even though I have the group variable as a factor. Is there any way I can display the plot with three discrete color instead?
I have looked at some other posts where they discuss various other ways of doing parallel coordinate plots in here. But I really wanted to do this in ggparcoord() function of package GGally. I appreciate your time in thinking about this problem.
Your code was almost correct. I spotted that columns=1:4 was not right in this case. You need to drop the column for groupColumn in columns
ggparcoord(dat,columns=2:4,groupColumn = 1)
I'd like to make a true heat map in R, much like a weather map, except my data is much more simple.
Consider this 3d data:
x <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4)
y <- c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
z <- rnorm(20)
The z would be color.
Here is what a discrete looking heatmap would like for this data:
How can I make a heatmap such that the colors are smooth and the full 2d space is filled with smoothed out colors based on the z values.
Please include sample code, not just a link that will probably confuse me even more, and I've probably already visited that site anyhow. Thanks :)
Use the following:
interp in the akima package
image.plot in the fields package
x <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4)
y <- c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
z <- rnorm(20)
library(fields)
library(akima)
s <- interp(x,y,z)
image.plot(s)
smooth.2d in the fields package does a good job (and it is much faster than interp from akima package for larger number of input points.
library(fields)
x <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4)
y <- c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
z <- rnorm(20)
s = smooth.2d(z, x=cbind(x,y), theta=0.5)
image.plot(s)
I have this problem. I got a heatmap, (but i suppose this applies to every plot) but I need to mirror my y-axis.
I got here some example code:
library(gstat)
x <- seq(1,50,length=50)
y <- seq(1,50,length=50)
z <- rnorm(1000)
df <- data.frame(x=x,y=y,z=z)
image(df,col=heat.colors(256))
This will generate the following heatmap
But I need the y-axis mirrored. Starting with 0 on the top and 50 on the bottom. Does anybody has a clue as to what I must do to change this?
See the help page for ?plot.default, which specifies
xlim: the x limits (x1, x2) of the plot. Note that ‘x1 > x2’ is
allowed and leads to a ‘reversed axis’.
library(gstat)
x <- seq(1,50,length=50)
y <- seq(1,50,length=50)
z <- rnorm(1000)
df <- data.frame(x=x,y=y,z=z)
So
image(df,col=heat.colors(256), ylim = rev(range(y)))
Does this work for you (it's a bit of a hack, though)?
df2<-df
df2$y<-50-df2$y #reverse oredr
image(df2,col=heat.colors(256),yaxt="n") #avoid y axis
axis(2, at=c(0,10,20,30,40,50), labels=c(50,40,30,20,10,0)) #draw y axis manually
The revaxis function in the plotrix package "reverses the sense of either or both the ‘x’ and ‘y’ axes". It doesn't solve your problem (Nick's solution is the correct one) but can be useful when you need to plot a scatterplot with reversed axes.
I would use rev like so:
df <- data.frame(x=x,y=rev(y),z=z)
In case you were not aware, notice that df is actually a function. You might want to be careful when overwriting. If you rm(df), things will go back to normal.
Don't forget to relabel the y axis as Nick suggests.
For the vertical axis increasing in the downward direction, I provided two ways (two different answers) for the following question:
R - image of a pixel matrix?
I have come across a number of situations where I want to plot more points than I really ought to be -- the main holdup is that when I share my plots with people or embed them in papers, they occupy too much space. It's very straightforward to randomly sample rows in a dataframe.
if I want a truly random sample for a point plot, it's easy to say:
ggplot(x,y,data=myDf[sample(1:nrow(myDf),1000),])
However, I was wondering if there were more effective (ideally canned) ways to specify the number of plot points such that your actual data is accurately reflected in the plot. So here is an example.
Suppose I am plotting something like the CCDF of a heavy tailed distribution, e.g.
ccdf <- function(myList,density=FALSE)
{
# generates the CCDF of a list or vector
freqs = table(myList)
X = rev(as.numeric(names(freqs)))
Y =cumsum(rev(as.list(freqs)));
data.frame(x=X,count=Y)
}
qplot(x,count,data=ccdf(rlnorm(10000,3,2.4)),log='xy')
This will produce a plot where the x & y axis become increasingly dense. Here it would be ideal to have fewer samples plotted for large x or y values.
Does anybody have any tips or suggestions for dealing with similar issues?
Thanks,
-e
I tend to use png files rather than vector based graphics such as pdf or eps for this situation. The files are much smaller, although you lose resolution.
If it's a more conventional scatterplot, then using semi-transparent colours also helps, as well as solving the over-plotting problem. For example,
x <- rnorm(10000); y <- rnorm(10000)
qplot(x, y, colour=I(alpha("blue",1/25)))
Beyond Rob's suggestions, one plot function I like as it does the 'thinning' for you is hexbin; an example is at the R Graph Gallery.
Here is one possible solution for downsampling plot with respect to the x-axis, if it is log transformed. It log transforms the x-axis, rounds that quantity, and picks the median x value in that bin:
downsampled_qplot <- function(x,y,data,rounding=0, ...) {
# assumes we are doing log=xy or log=x
group = factor(round(log(data$x),rounding))
d <- do.call(rbind, by(data, group,
function(X) X[order(X$x)[floor(length(X)/2)],]))
qplot(x,count,data=d, ...)
}
Using the definition of ccdf() from above, we can then compare the original plot of the CCDF of the distribution with the downsampled version:
myccdf=ccdf(rlnorm(10000,3,2.4))
qplot(x,count,data=myccdf,log='xy',main='original')
downsampled_qplot(x,count,data=myccdf,log='xy',rounding=1,main='rounding = 1')
downsampled_qplot(x,count,data=myccdf,log='xy',rounding=0,main='rounding = 0')
In PDF format, the original plot takes up 640K, and the downsampled versions occupy 20K and 8K, respectively.
I'd either make image files (png or jpeg devices) as Rob already mentioned, or I'd make a 2D histogram. An alternative to the 2D histogram is a smoothed scatterplot, it makes a similar graphic but has a more smooth cutoff from dense to sparse regions of space.
If you've never seen addictedtor before, it's worth a look. It has some very nice graphics generated in R with images and sample code.
Here's the sample code from the addictedtor site:
2-d histogram:
require(gplots)
# example data, bivariate normal, no correlation
x <- rnorm(2000, sd=4)
y <- rnorm(2000, sd=1)
# separate scales for each axis, this looks circular
hist2d(x,y, nbins=50, col = c("white",heat.colors(16)))
rug(x,side=1)
rug(y,side=2)
box()
smoothscatter:
library("geneplotter") ## from BioConductor
require("RColorBrewer") ## from CRAN
x1 <- matrix(rnorm(1e4), ncol=2)
x2 <- matrix(rnorm(1e4, mean=3, sd=1.5), ncol=2)
x <- rbind(x1,x2)
layout(matrix(1:4, ncol=2, byrow=TRUE))
op <- par(mar=rep(2,4))
smoothScatter(x, nrpoints=0)
smoothScatter(x)
smoothScatter(x, nrpoints=Inf,
colramp=colorRampPalette(brewer.pal(9,"YlOrRd")),
bandwidth=40)
colors <- densCols(x)
plot(x, col=colors, pch=20)
par(op)