Geomass geometry with a logarithmic scale

I am a bit stuck in building a raster with a magazine scale. Consider this graph, for example:

ggplot(faithfuld, aes(waiting, eruptions)) +
 geom_raster(aes(fill = density))

enter image description here

But how to use the log scale with this geome? None of the usual methods are very satisfying:

 ggplot(faithfuld, aes(waiting, log10(eruptions))) +
   geom_raster(aes(fill = density))

enter image description here

 ggplot(faithfuld, aes(waiting, (eruptions))) +
   geom_raster(aes(fill = density)) + 
   scale_y_log10()

enter image description here

and this does not work at all:

 ggplot(faithfuld, aes(waiting, (eruptions))) +
   geom_raster(aes(fill = density)) + 
   coord_trans(x="log10")

Error: geom_raster only works with Cartesian coordinates

Are there any options for using a raster log scale?

To be precise, I have three data columns. The z value is the value that I want to use to color the raster, and it is not calculated from the x and y values. So I need to put all three columns in a function ggplot. For example:

dat <- data.frame(x = rep(1:10, 10), 
                  y = unlist(lapply(1:10, function(i) rep(i, 10))), 
                  z = faithfuld$density[1:100])

ggplot(dat, aes(x = log(x), y = y, fill = z)) +
  geom_raster()

enter image description here

What can I do to get rid of these spaces in the raster?

Note that this question is related to these two:

R-, ( , ). : https://gist.github.com/benmarwick/9a54cbd325149a8ff405

+4
1

faithfuld , . , . geom_raster, . x, y, . , y, y ( ), . :

library(ggplot2)
library(gridExtra)

# Use point to visualize the effect of log on the dataset
g1 <- ggplot(faithfuld, aes(x=waiting, y=eruptions)) +
  geom_point(size=0.5)    

g2 <- ggplot(faithfuld, aes(x=waiting, y=log(eruptions))) +
  geom_point(size=0.5)    

grid.arrange(g1, g2, ncol=2)    

enter image description here

y , faithful geom_density_2d.

# Use geom_density_2d
ggplot(faithful, aes(x=waiting, y=log(eruptions))) +
  geom_density_2d() +
  stat_density_2d(geom="raster", aes(fill=..density..),
                  contour=FALSE)

enter image description here

: geom_rect xmin, xmax, ymin, ymax, .

geom_raster , , , geom_tile geom_rect . , , () , xmin xmax , .

 dat <- data.frame(x = rep(1:10, 10), 
                  y = unlist(lapply(1:10, function(i) rep(i, 10))), 
                  z = faithfuld$density[1:100])
library(ggplot2)
library(gridExtra)   

g <- ggplot(dat, aes(x = log(x), y = y, fill = z)) +
  geom_raster()   

# Replace the ymin and ymax
distance <- diff((unique(dat$x)))/2
upper <- (unique(dat$x)) + c(distance, distance[length(distance)])
lower <- (unique(dat$x)) - c(distance[1], distance) 

# Create xmin, xmax, ymin, ymax
dat$xmin <- dat$x - 0.5 # default of geom_raster is 0.5
dat$xmax <- dat$x + 0.5
dat$ymin <- unlist(lapply(lower, function(i) rep(i, rle(dat$y)$lengths[1])))
dat$ymax <- unlist(lapply(upper, function(i) rep(i, rle(dat$y)$lengths[1])))        

# You can also use geom_tile with the width argument
g2 <- ggplot(dat, aes(x=log(x), y=y, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, fill=z)) +
  geom_rect() 

# show the plots     
grid.arrange(g, g2, ncol=2)

enter image description here

+3

All Articles