Hi, I've been really struggling with this. If I have a vector like dat <- c(0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,1,0,0,0) I want to plot each element as a colored rectangle (red=1, blue=1) in the right order, so they all stack up forming a vertical column on the graph. Sort of like a building, with each floor in the appropriate color. Any ideas? I've tried using ggplot and geom_tile, but my data has a million elements and the plots take forever to generate. I've also tried using a heatmap, but I need 2 columns at least, and I only have 1. [[alternative HTML version deleted]]
Hi Nacho,
You should look grid.rect or grid.polygon function from "grid"
library.
I attach you an example, you can play with this:
dat <- c(0,0,0,0,1,1,1,0,0,1)
colour=ifelse(dat == 0, "red", "blue")
library(grid)
library(foreach)
grid.newpage()
vp1 <- grid.layout(nrow = 1, ncol = 1,
widths = unit(0.80, "native"),
heights = unit(0.80, "native"))
pushViewport(viewport(width= unit(0.9, "npc"), height=
unit(0.9,"npc"),
xscale = c(0, 5), yscale = c(0, 15)))
foreach(i=1:10) %do%
grid.rect(x=0, y=1+i, width=4, height=1, draw=TRUE, default.units =
'native', gp=gpar(fill=colour[i]))
Regards,
John Ortiz
Smithsonian Tropical Research Institute
Geologist
>Hi, I've been really struggling with this.
>
>If I have a vector like
>dat <- c(0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,1,0,0,0)
>
>I want to plot each element as a colored rectangle (red=1, blue=1) in the
>right order, so they all stack up forming a vertical column on the graph.
>Sort of like a building, with each floor in the appropriate color.
>
>Any ideas?
>I've tried using ggplot and geom_tile, but my data has a million
elements
>and the plots take forever to generate.
>I've also tried using a heatmap, but I need 2 columns at least, and I
only
>have 1.
Hi, You could try grid.colorstrip() from the gridExtra package, grid.colorstrip(ifelse(dat, "blue", "red")) or grid.raster(), which should be more efficient, grid.raster(matrix(ifelse(dat, "blue", "red")), interp=FALSE, width=unit(1,"npc"), height=unit(1,"npc")) HTH, baptiste On 15 July 2011 22:20, Nacho Caballero <nachocab at gmail.com> wrote:> Hi, I've been really struggling with this. > > If I have a vector like > dat <- c(0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,1,0,0,0) > > I want to plot each element as a colored rectangle (red=1, blue=1) in the > right order, so they all stack up forming a vertical column on the graph. > Sort of like a building, with each floor in the appropriate color. > > Any ideas? > I've tried using ggplot and geom_tile, but my data has a million elements > and the plots take forever to generate. > I've also tried using a heatmap, but I need 2 columns at least, and I only > have 1. > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >