Fernando Henrique Ferraz Pereira da Rosa
2003-Jan-25 22:22 UTC
[R] Plotting coloured histograms...
Hi, I am having some trouble trying to plot a histogram in more than one colour. What I want to do is, plot two vectors in the same histogram, but with different colours, for instance: > x <- rnorm(1000,20,4); > y <- rnorm(1000,10,2); Then I'd like to have x and y ploted on the same hist (I can do that already doing w <- c(x,y) then hist(w)) but the bars representing the x's should be in one colour and the bars representing the y should be in another one, so that I could see the overlaping areas of the two distributions etc. Is there any way to do that? I've read through the hist docummentation (>help(hist)) and also googled for "R colour histogram" but didn't find anything helpfull. Thank you for your attention, --
Try the H. Bengtsson's function plot.histogram from http://www.maths.lth.se/matstat/staff/hb/mypackages/R/plot.histogram.R Remigijus Saturday, January 25, 2003, 10:21:30 PM, you wrote: FHFPdR> Hi, I am having some trouble trying to plot a histogram in more than one FHFPdR> colour. What I want to do is, plot two vectors in the same histogram, but FHFPdR> with different colours, for instance: FHFPdR> > x <- rnorm(1000,20,4); FHFPdR> > y <- rnorm(1000,10,2); FHFPdR> Then I'd like to have x and y ploted on the same hist (I can do that FHFPdR> already doing w <- c(x,y) then hist(w)) but the bars representing the x's should FHFPdR> be in one colour and the bars representing the y should be in another one, FHFPdR> so that I could see the overlaping areas of the two distributions etc. FHFPdR> Is there any way to do that? I've read through the hist docummentation (>>help(hist)) and also googled for "R colour histogram" but didn't find FHFPdR> anything helpfull. FHFPdR> Thank you for your attention, FHFPdR> -- FHFPdR> ______________________________________________ FHFPdR> R-help at stat.math.ethz.ch mailing list FHFPdR> http://www.stat.math.ethz.ch/mailman/listinfo/r-help
Dear R helpers, What are the supported distributions in R and where to look for them?. Also, is it possible to generate cyclic arrivals in R. For example, arrival patterns that correlates with time of the day. I really appreciate your help,
On Sun, 26 Jan 2003, Mohamed A. Kerasha wrote:> What are the supported distributions in R and where to look for them?.help.search("distribution") and look also at packages SuppDists, evd, gld and sn amongst others.> Also, > is it possible to generate cyclic arrivals in R. For example, arrival > patterns that > correlates with time of the day.Yes, it is possible, provided you have a complete description. R is after all a fully-fledged programming language. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Hi All, I plotted several distribution curves in one graph but I do not know how to add legends say which is what distribution. Could someone help me? Thanks,
Hi, ----- Original Message ----- From: "Qin Xin" <qinxin2001 at yahoo.com> To: <r-help at stat.math.ethz.ch> Sent: Monday, January 27, 2003 9:42 AM Subject: [R] how could I add legends?> Hi All, > > I plotted several distribution curves in one graph but > I do not know how to add legends say which is what > distribution. Could someone help me? Thanks,Have a look at ?legend Kevin ------------------------------------------------ Ko-Kang Kevin Wang Master of Science (MSc) Student Department of Statistics University of Auckland New Zealand www.stat.auckland.ac.nz/~kwan022
> Hi, I am having some trouble trying to plot a histogram in more than one > colour. What I want to do is, plot two vectors in the same histogram, but > with different colours, for instance: > > x <- rnorm(1000,20,4); > > y <- rnorm(1000,10,2); > Then I'd like to have x and y ploted on the same hist (I can do that > already doing w <- c(x,y) then hist(w)) but the bars representing the x's should > be in one colour and the bars representing the y should be in another one, > so that I could see the overlaping areas of the two distributions etc.You haven't made it clear if you want the histograms on top of each other (which is what you get from w<-c(x,y) ) or if you want them side-by-side (so you can see overlapping areas). I plot histograms on top of each other using my own trellis function panel.surpose, like this: x <- rnorm(1000,20,4) y <- rnorm(1000,10,2) w <- c(x,y) f <- ifelse(1:length(z)<=length(x),0,1) library(lattice) histogram(~w, groups=f, panel=panel.surpose) The "groups" argument should be a factor, or some other variable taking values in a small set. The function panel.surpose takes each panel in turn, splits the dataset for that panel into groups according to "groups", and plots a histogram for each group, one on top of another. The colours can be specified like "bar.col=c(rgb(1,0,0),rgb(0,1,0))", the first colour being used for the lowest level of "groups". You could specify a different "panel.groups" function, but I can't see how that would be useful. In writing this function, I had trouble with the standard routines hist and panel.histogram, which deal with the counting. What should their behaviour be when the data has NA values? It seems to me that count, percentage and density do not handle NA values in a consistent way. Anyway, I wrote a replacement for panel.histogram to use with my panel.surpose. Damon Wischik. panel.histogram.partial <- function (x, breaks, equal.widths = TRUE, type = "density", col bar.fill$col, ...) { x <- as.numeric(x) grid.lines(x = c(0.05, 0.95), y = unit(c(0, 0), "native"), default.units = "npc") if (length(x) > 0) { bar.fill <- trellis.par.get("bar.fill") if (is.null(breaks)) { nint <- round(log2(length(x)) + 1) breaks <- if (equal.widths) do.breaks(range(x), nint) else quantile(x, 0:nint/nint) } h <- hist(x, breaks = breaks, plot = FALSE, ...) y <- if (type == "count") h$counts else if (type == "percent") 100 * h$counts/length(x) else h$intensities * length(which(!is.na(x)))/length(x) nb <- length(breaks) if (nb != (length(y) + 1)) warning("something is probably wrong") if (nb > 1) { for (i in 1:(nb - 1)) if (y[i] > 0) { grid.rect(gp = gpar(fill = col), x = breaks[i], y = 0, height = y[i], width = breaks[i + 1] - breaks[i], just = c("left", "bottom"), default.units="native") } } } } rgbmix <- function(p,col1,col2) { # p a vector. pi=0: col1. pi=1: col2 cm <- (1-p) %o% col2rgb(col1) + p %o% col2rgb(col2) rgb(cm[,"red",],cm[,"green",],cm[,"blue",],maxColorValue=255) } panel.surpose <- function (x, y = NULL, subscripts, groups, panel.groups "panel.histogram.partial", bar.col=superpose.line$col, ...) { x <- as.numeric(x) if (!is.null(y)) y <- as.numeric(y) superpose.line <- trellis.par.get("superpose.line") if (length(subscripts)>0 && length(groups)<max(subscripts)) groups <- rep(groups, length=max(subscripts)) vals <- sort(unique(groups)) nvals <- length(vals) bar.col <- if (length(bar.col)>=nvals) bar.col else { if (length(bar.col)>2 || length(bar.col)==1) rep(bar.col,length=nvals) else # linearly interpolate the colours rgbmix((1:nvals-1)/(nvals-1),bar.col[[1]],bar.col[[2]]) } panel.groups <- if(is.function(panel.groups)) panel.groups else if (is.character(panel.groups)) get(panel.groups) else eval(panel.groups) nx <- length(subscripts) for (i in nvals:1) { id <- (groups[subscripts] %in% vals[1:i]) if (any(id)) { fakex <- rep(NA,times=nx-length(which(id))) xandfake <- c(fakex,x[id]) args <- if (!is.null(y)) list(x=xandfake,y,col=bar.col[i],...) else list(x=xandfake, col=bar.col[i], lty=0, ...) do.call("panel.groups",args) } } }
On 25 Jan 2003 at 22:21, Fernando Henrique Ferraz Pereira da Rosa wrote: Hola! Maybe this may be of help:> x <- rnorm(100,2,2) > y <- rnorm(200, 4,3) > hist(x) > hist(y, add=TRUE) > # which gives a confusing result. Better is: > hist(x, freq=FALSE) > hist(y, add=TRUE, freq=FALSE) > # But it is difficult to separate visually the two histograms in > # the same plot. Trying with colors: > hist(x, freq=FALSE, col="blue") > hist(y, add=TRUE, freq=FALSE, col="red") > # is somewhat better, but it covers part of the blue histogram! > # Anybody knows of better solutions?Kjetil Halvorsen> Hi, I am having some trouble trying to plot a histogram in more than one > colour. What I want to do is, plot two vectors in the same histogram, but > with different colours, for instance: > > x <- rnorm(1000,20,4); > > y <- rnorm(1000,10,2); > Then I'd like to have x and y ploted on the same hist (I can do that > already doing w <- c(x,y) then hist(w)) but the bars representing the x's should > be in one colour and the bars representing the y should be in another one, > so that I could see the overlaping areas of the two distributions etc. > Is there any way to do that? I've read through the hist docummentation > (>help(hist)) and also googled for "R colour histogram" but didn't find > anything helpfull. > > Thank you for your attention, > > -- > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/r-help