1

I'm trying to use ggplot to create a pseudo-boxplot by way of colour/transparency but am struggling a little. The idea is that for any point on the x axis, the more number of times a specific y value occurs, the darker that (x,y) point should be. To add to the complication, I don't have a count for each (x,y), but instead I have a count for (x , ymin:ymax) which means that this count holds for all points between (x,ymin) and (x,ymax)

Searching seems to be a little tricky and I"m unable to unearth anything I can use. The closest I could get by myself was as below, however this takes very long for larger datasets.

library(ggplot2)
set.seed(1)
(d <- data.frame(cbind(x = sample(1:3, 10, replace = TRUE), y = sample(3:8, 10, replace = TRUE)), fac = sample(1:3, 10, replace = TRUE)))

p1 <- ggplot()

for ( i in seq(nrow(d)))
{
  p1 <- p1 + geom_rect(
    data=d[i,],
    aes(
      xmin = fac - .4,
      xmax = fac + .4,
      ymin = x,
      ymax = y),
    alpha = .25
  )

}

enter image description here

Any suggestions? I'd prefer a ggplot solution. Thanks

Update on 23 Nov 13, With Troy's help I've reached here. The only issues now is that the legend is ranging between the t values for each rectangle, whereas it should range from the cumulative t at an x,y.

library(ggplot2)
set.seed(1)
d <- data.frame(cbind(x = sample(1:3, 6, replace = TRUE), 
                      ymin = sample(3:8, 6, replace = TRUE),
                      ymax = sample(3:8, 6, replace = TRUE),
                      t = sample(3:8, 6, replace = TRUE)/10
                      ))

ggplot(data=d) +
  geom_rect(aes(xmin=x-0.4,xmax=x+0.4,ymin=ymin,ymax=ymax,alpha=t),fill="red")
TheComeOnMan
  • 12,535
  • 8
  • 39
  • 54
  • Maybe add a rug plot to the edges of the plot? http://stackoverflow.com/questions/11546256/two-way-density-plot-combined-with-one-way-density-plot-with-selected-regions-in – americo Nov 21 '13 at 18:30

1 Answers1

2

ggplot WILL support vectors using geom_rect

this will at least get rid of the loop, if you're happy with the mechanics of the original plot?

library(ggplot2)
set.seed(1)
(d <- data.frame(cbind(x = sample(1:3, 10, replace = TRUE), y = sample(3:8, 10, replace = TRUE)), fac = sample(1:3, 10, replace = TRUE)))
ggplot(data=d) +
geom_rect(aes(xmin=fac-0.4,xmax=fac+0.4,ymin=x,ymax=y),alpha=0.25,fill="red")

plot

or did you want to change how the transparency was calculated using a different implementation of count()?

EDIT - if you are just looking to block out a grid with "count" indications:

library(ggplot2)
set.seed(1)
d <- data.frame(cbind(x = sample(1:3, 15, replace = TRUE), 
     y = sample(3:8, 18, replace = TRUE)))

ggplot(data=d) +
   geom_tile(aes(x=x,y=y),alpha=0.25,fill="red")

plot-tile

Troy
  • 8,581
  • 29
  • 32
  • +1 Looking at your answer I cannot figure out why I ran the loop in the first place. And you have good insight into the problem. I completely forgot to mention that transparency part. I just associated another column to the `alpha` argument inside the `aes` which fixed that. Any way to make this a continuous plot instead of rectangles on each x category? – TheComeOnMan Nov 22 '13 at 10:14
  • if you are just looking to grid out x-y with transparency, then you can use geom_tile as above in the edit? Not sure if that's exactly what you're looking to do, if it's different, could you describe based on d? – Troy Nov 22 '13 at 10:34
  • No no you were on the right track. I meant that with the plot I have no added to the question, each rectangle is a data point so the legend marks the transparencies from lowest to highest count value of the rectangle. However, the graph actually has rectangle overlap and the transparency needs to range between the cumulative transparencies at any x,y. Any way to do that? – TheComeOnMan Nov 23 '13 at 06:17