[R] ggplot2, geomtile fill assignment

Ista Zahn istazahn at gmail.com
Thu Feb 21 22:26:01 CET 2013


Hi Ben,

How about

lim <- with(input, {
  c(min(amplitude),
    max(amplitude))})


p <- ggplot(sub1, aes(x=x,y=y,fill=amplitude))

p +
  geom_tile() +
  scale_fill_gradientn("Custom
Colours",
                       colours=c("purple",
                         "blue",
                         "green",
                         "yellow",
                         "orange",
                         "red"),
                       limits=lim)

# custom colours legend range and assigned colors is the same in the next plot
dev.new()

p <- ggplot(sub3, aes(x=x,y=y,fill=amplitude))
p +
  geom_tile() +
  scale_fill_gradientn("Custom
Colours",
                       colours=c("purple",
                         "blue",
                         "green",
                         "yellow",
                         "orange",
                         "red"),
                       limits=lim)

Best,
Ista

On Thu, Feb 21, 2013 at 3:02 PM, Benjamin Caldwell
<btcaldwell at berkeley.edu> wrote:
> Dear R help,
>
> I have some readings in three dimensions (x, y, z) and an amplitude for
> each. I'd like to visualize the data using ggplot, using tile plots, as I
> have some additional point data I would like to eventually overlay on the
> tile plots.
>
> I would like to subset the data by sections, slices if you will, in the z
> dimension, and plot the data for that slice.
>
> I can do all of this, but am having a largish hangup - ggplot automatically
> keys the fill gradient to the data which is assigned the fill aesthetic.
> This is a problem for me because I have different ranges of amplitudes for
> different depth slices. So, I'm seeing the same color assigned to a
> different amplitude for each of my plots.
>
> I would like to assign the colors in the fill gradient to a range that I
> specify, not the range that is in the subset of the data, but I don't know
> how.
>
> Some example data and code attached.
>
> Thanks
>
> Ben Caldwell
>
> ---------------------------------------------
>
> require(ggplot2)
>
> setwd("")
>
> input<-read.table("examplefile.txt", header=TRUE)
> str(input)
>
> head(input)
>
>   # X      x      y      z depth amplitude
> # 1 1 0.0125 0.0125 -0.025     1       623
> # 2 2 0.0125 0.0375 -0.025     1       654
> # 3 3 0.0125 0.0625 -0.025     1       685
> # 4 4 0.0125 0.0875 -0.025     1      1598
> # 5 5 0.0125 0.1125 -0.025     1      2200
> # 6 6 0.0125 0.1375 -0.025     1      1917
>
> depths<- with(input, sort(unique(depth)))
> depths
> # [1] 1 2 3 4
>
> mysubset<-function(input, column.name, expression.to.match){
>
> output <- input[column.name==expression.to.match,]
>
> return(output)
> }
>
> sub1 <- mysubset(input, input$depth, depths[1])
> sub2 <- mysubset(input, input$depth, depths[2])
> sub3 <- mysubset(input, input$depth, depths[3])
> sub4 <- mysubset(input, input$depth, depths[4])
>
> p <- ggplot(sub1, aes(x=x,y=y,fill=amplitude))
> p + geom_tile() + scale_fill_gradientn("Custom
> Colours",colours=c("purple","blue","green","yellow","orange","red"))
> # custom colours legend range and assigned colors is different than in the
> next plot
>
> p <- ggplot(sub3, aes(x=x,y=y,fill=amplitude))
> p + geom_tile() + scale_fill_gradientn("Custom
> Colours",colours=c("purple","blue","green","yellow","orange","red"))
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list