Hello all, I'm now using image() to show image data (in my case dumps of SOM weights) but would like to show RGB colour data, not just single "z" colour values. I've currently been using seq() to skip 4 values, so I can show the R, G or B channels separately as "z". But is there a way I can show all three channels simultaneously as a proper colour image? Thanks, B. Bogart
> I'm now using image() to show image data (in my case dumps of SOM > weights) but would like to show RGB colour data, not just single "z" > colour values.You can do this fairly readily with ggplot2: install.packages("ggplot2") library(ggplot2) qplot(x, y, data=mydata, fill=rgb, geom="tile") + scale_fill_identity() (assuming that your variable containing the rgb colour is called rgb) If your data is originally in the matrix form used by image, see the examples on http://had.co.nz/ggplot2/geom_tile.html on how to change to the data.frame form used by ggplot. Hadley -- http://had.co.nz/
B. Bogart
2008-Feb-22 19:35 UTC
[R] best practise for organizing data for ggplot faceting?
Hello all,, What is the best practise for organizing data for easy ggplot faceting? Right now I have a list of data.frames, each being an image. I can plot them with geom_tile separately, but as a list of data.frames I see no easy way to facet. For this kind of data what is the best practise to organize it for faceting? Here is what I have now. (note the data originally comes from a huge dataframe with the RGBA values for each 7500 pixels has its own variable. som_images <- list() for (unit in seq(1:36)) { print(unit) som_image <- expand.grid(x=1:100,y=1:75) som_image$r <- as.numeric(som_weights[unit,seq(1,30000,4)]) som_image$g <- as.numeric(som_weights[unit,seq(2,30000,4)]) som_image$b <- as.numeric(som_weights[unit,seq(3,30000,4)]) som_image$rgb <- rgb(som_image$r,som_image$g,som_image$b) som_images[[unit]] <- som_image } qplot(x, y, data=som_images[[1]], geom="tile", fill=rgb) + scale_fill_identity() + opts(aspect.ratio = .75) I would like to facet so each "som_image" is in a 6x6 matrix (0 in the lower left, 36 in the upper right) Thanks so much for your time, B. Bogart hadley wickham wrote:> Probably the easiest thing is to do: > > df$rgb <- with(data, rgb(r, g, b) > qplot(x,y,data=df,fill=rgb) + scale_fill_identity() > > That creates a new variable that stores that actual colour, and then > tells ggplot to use the raw value, not to scale it in anyway. > > Hadley > > On Wed, Feb 20, 2008 at 2:41 PM, B. Bogart <bbogart at sfu.ca> wrote: >> Hello Hadley, >> >> I have to say ggplot2 was worth the upgrade! I'm really enjoying the >> approach and wondering what I did with the base graphics. >> >> Anyhow I've looked over the documentation and scales, and read through >> the book section on the website and I still can't see how to apply my >> RGB channels to the fill of tiles in a plot. >> >> I'm working with a simple test case with 6x6 items, each with RGB >> components, ranging from 0 to 1. >> >> df <- expand.grid(x=1:6,y=1:6) # position of items in grid >> df$r <- c(rep(1,12),rep(0,24)) >> df$g <- c(rep(0,12),rep(1,12),rep(0,12)) >> df$b <- c(rep(0,24),rep(1,12)) >> >> gives me three groups of values, each pure red, green and blue. >> >> So if that is the data, how can I make a geom_tile plot where each tile >> is coloured using the rgb components from the dataframe? I imagine >> something like: >> >> qplot(x,y,data=df,fill=rgb(r=r,g=g,b=b)) >> >> where rgb() would be some function to combine the components into >> individual colours understood for tile filling. >> >> The only way I've been able to see how to specify colours are using the >> raw values and having the qplot scale figure out the mapping, or >> specifying the character colour for each element. Is there a way of >> supplying colour components? >> >> Thanks for your time, >> >> >> B. Bogart >> >> hadley wickham wrote: >> > You'll need 2.6.1. >> > Hadley >> > >> > On Feb 5, 2008 3:12 PM, B. Bogart <bbogart at sfu.ca> wrote: >> >> I'm using the r-cran R debian packages. >> >> >> >> R.Version() tells me: >> >> >> >> "R version 2.4.0 Patched (2006-11-25 r39997)" >> >> >> >> What is the min required version to use ggplot2? >> >> >> >> >> >> Thanks, >> >> B. Bogart >> >> >> >> hadley wickham wrote: >> >>> You'll need to make sure you have a recent version of R - what >> >>> version are you using? >> >>> >> >>> Hadley >> >>> >> >>> On Feb 5, 2008 1:45 PM, B. Bogart <bbogart at sfu.ca> wrote: >> >>> >> >>>> Hello Hadley, >> >>>> >> >>>> ggplot2 looks great! >> >>>> >> >>>> "install.packages("ggplot2")" did not work though, I get a message >> >>>> saying the package is not available in the repos. I choose the nearest >> >>>> mirror (Canada (BC)), could that mirror be out of date? >> >>>> >> >>>> I did not see any debian packages for ggplot2. >> >>>> >> >>>> Thanks, >> >>>> B. Bogart >> >>>> >> >>>> >> >>>> >> >>>> hadley wickham wrote: >> >>>> >> >>>>>> I'm now using image() to show image data (in my case dumps of SOM >> >>>>>> weights) but would like to show RGB colour data, not just single "z" >> >>>>>> colour values. >> >>>>>> >> >>>>> You can do this fairly readily with ggplot2: >> >>>>> >> >>>>> install.packages("ggplot2") >> >>>>> library(ggplot2) >> >>>>> qplot(x, y, data=mydata, fill=rgb, geom="tile") + scale_fill_identity() >> >>>>> >> >>>>> (assuming that your variable containing the rgb colour is called rgb) >> >>>>> >> >>>>> If your data is originally in the matrix form used by image, see the >> >>>>> examples on http://had.co.nz/ggplot2/geom_tile.html on how to change >> >>>>> to the data.frame form used by ggplot. >> >>>>> >> >>>>> Hadley >> >>>>> >> >>>>> >> >>> >> >>> >> >>> >> >> >> > >> > >> > >> >> > > >