使用时我无法删除矩形之间的空白geom_tile
功能。
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")
直到这里我明白为什么会发生这种情况。为了前进我可以转换x and y
因子水平以消除空间!但这一次我失去了geom_vline
and geom_hline
线。这可能是发生了转换x and y
因素水平。
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")
当我将因子水平添加到geom_vline&geom_hline
收到此错误!
UseMethod(“rescale”) 中的错误:“rescale”没有适用的方法
应用于类“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")