相关疑难解决方法(0)

删除 geom_tile 中的空白并匹配 geom_vline&hline 位置

使用函数时,我无法删除矩形之间的空白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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

直到这里我明白为什么会发生这种情况。为了继续前进,我可以转换x and y为因子水平以消除空间!但这次我失去了geom_vlinegeom_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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

当我添加因子级别来geom_vline&geom_hline得到这个错误时!

UseMethod("rescale") 中的错误:没有适用于“factor”类对象的“rescale”方法 …

r ggplot2

4
推荐指数
1
解决办法
2784
查看次数

标签 统计

ggplot2 ×1

r ×1