quibble
quibble

Reputation: 337

removing specific axis ticks in geom_raster in ggplot2

In the code below, because expand=c(0,0) is used, some axis ticks overlap. I want to delete 0.5 in the y-axis of the bottom row, and 1.00 in the x-axis of the first two columns (i.e., total three numbers).

In addition, although expand=c(0,0) is used, there are still some gray region. I want to eliminate it completely. In fact, the tick location is inaccurate (e.g., the result for (x=0,y=0) is not at (x=0,y=0)).

x.i <- seq(0,1,length=30)
z.i <- c(0.1,0.5,0.9)
y.i <- seq(0,0.5,length=30)
b.i <- c(10,100)


out <- NULL
for(i in 1:length(x.i)){
  x <- x.i[i]
for(k in 1:length(z.i)){
  z <- z.i[k]
for(l in 1:length(y.i)){
  y   <- y.i[l]
for(m in 1:length(b.i)){
    b <- b.i[m]

   cc <- rbinom(1,3,0.5)

out <- rbind(out,c(x,z,y,b,cc))
}}}}

colnames(out) <- c("x","z","y","b","qd")
out <- as.data.frame(out)

library(ggplot2)
ggplot(out,aes(x,y))+geom_raster(aes(fill=factor(qd)))+facet_grid(b~z)+
xlab("x")+ylab("y")+ scale_x_continuous(limits = c(0,1), expand = c(0, 0)) +
  scale_y_continuous(limits = c(0,0.5), expand = c(0, 0)) +
scale_fill_manual(values=c("darkgoldenrod2","tomato3","yellow","darkgreen"),name="case")

myplot

Upvotes: 0

Views: 475

Answers (1)

sssascha
sssascha

Reputation: 100

You could use geom_rect to eliminate the grey area.

ggplot(out,aes(x,y)) + 
  geom_rect(aes(xmin = x, 
                xmax = x + 0.03448276, 
                ymin = y, 
                ymax = y + 0.01724138, 
                fill = factor(qd))) +
  facet_grid(b~z)+
  xlab("x") + ylab("y") + 
  scale_x_continuous(limits = c(0, 1 + 0.03448276), 
                     expand = c(0, 0)) +
  scale_y_continuous(limits = c(0, 0.5 + 0.01724138), 
                     expand = c(0, 0)) +
  scale_fill_manual(values=c("darkgoldenrod2","tomato3","yellow","darkgreen"),name="case")

enter image description here

Upvotes: 1

Related Questions