Reputation: 4655
I have trouble with removing the white space between the rectangles when using geom_tile
function.
df <- data.frame(
x = c(seq(2,16,2),seq(17,39,2)),
y = c(rep(c(seq(8,26,2),seq(27,45,2)),each=20)),
z = c(1:400))
library(ggplot2)
ggplot(df, aes(x, y)) +
geom_tile(aes(fill = factor(z)), colour = "grey50")+
geom_vline(aes(xintercept=6),linetype="dashed",colour="red",size=1)+
geom_hline(aes(yintercept=24),linetype="dashed",colour="red",size=1)+
scale_x_continuous(expand = c(0, 0),breaks=seq(0,50,1))+
scale_y_continuous(expand = c(0, 0),breaks=seq(0,50,1))+
theme(legend.position = "none")
until here I understand why this is happens. To move forward I can convert x and y
to factor levels to remove the space! But this time I lost the geom_vline
and geom_hline
lines. This is probably happened converted x and y
factor levels.
ggplot(df, aes(factor(x), factor(y))) +
geom_tile(aes(fill = factor(z)), colour = "grey50")+
geom_vline(aes(xintercept=6),linetype="dashed",colour="red",size=1)+
geom_hline(aes(yintercept=24),linetype="dashed",colour="red",size=1)+
#scale_x_continuous(expand = c(0, 0),breaks=seq(0,50,1))+
#scale_y_continuous(expand = c(0, 0),breaks=seq(0,50,1))+
theme(legend.position = "none")
and when I add factor levels to geom_vline&geom_hline
getting this error!
Error in UseMethod("rescale") : no applicable method for 'rescale' applied to an object of class "factor"
ggplot(df, aes(factor(x), factor(y))) +
geom_tile(aes(fill = factor(z)), colour = "grey50")+
geom_vline(aes(xintercept=factor(6)),linetype="dashed",colour="red",size=1)+ geom_hline(aes(yintercept=factor(24)),linetype="dashed",colour="red",size=1)+
#scale_x_continuous(expand = c(0, 0),breaks=seq(0,50,1))+
#scale_y_continuous(expand = c(0, 0),breaks=seq(0,50,1))+
theme(legend.position = "none")
Upvotes: 4
Views: 2891
Reputation: 48251
Here are two possible solutions. The first one is to adjust width
and height
of the tiles:
library(ggplot2)
ggplot(df, aes(x, y)) +
geom_tile(aes(fill = factor(z)), colour = "grey50", width = 2, height = 2)+
geom_vline(aes(xintercept=6),linetype="dashed",colour="red",size=1)+
geom_hline(aes(yintercept=24),linetype="dashed",colour="red",size=1)+
scale_x_continuous(expand = c(0, 0),breaks=seq(0,50,1))+
scale_y_continuous(expand = c(0, 0),breaks=seq(0,50,1))+
theme(legend.position = "none")
The second one is to change the values of xintercept
and yintercept
:
ggplot(df, aes(factor(x), factor(y))) +
geom_tile(aes(fill = factor(z)), colour = "grey50")+
geom_vline(aes(xintercept=3),linetype="dashed",colour="red",size=1)+
geom_hline(aes(yintercept=9),linetype="dashed",colour="red",size=1)+
theme(legend.position = "none")
which come from
match(6, unique(df$x))
# [1] 3
match(24, unique(df$y))
# [1] 9
That is, now we need to specify the number of a factor level of interest. In this case both 6 and 24 were used as factor levels, so we could do this, but in general this approach may not work as you may want a line at a nonexistent factor level.
Upvotes: 6