R 如何使用ggplot2中的geom_tile控制连续轴和离散轴的纵横比?

R 如何使用ggplot2中的geom_tile控制连续轴和离散轴的纵横比?,r,ggplot2,aspect-ratio,R,Ggplot2,Aspect Ratio,我想用ggplot2和geom\u tile()生成一个绘图。我希望瓷砖具有相同的高度和宽度,如果x轴和y轴都是离散的(正如这里所讨论的:),这似乎很好 但是,如果x轴上的值被解释为连续的,则当我使用x轴的离散比例(MWE1)时,我要么在绘图中获得大量灰色空间,且宽高比不为1,要么在绘图中没有灰色空间,且具有连续比例,但宽高比仍不为1(MWE2)。(我会插入图片,但这似乎是不允许的,因为我的声誉不够高。) MWE1: 我的我不确定我是否完全理解你的问题,但我认为这正是你想要的: p <-

我想用
ggplot2
geom\u tile()
生成一个绘图。我希望瓷砖具有相同的高度和宽度,如果x轴和y轴都是离散的(正如这里所讨论的:),这似乎很好

但是,如果x轴上的值被解释为连续的,则当我使用x轴的离散比例(MWE1)时,我要么在绘图中获得大量灰色空间,且宽高比不为1,要么在绘图中没有灰色空间,且具有连续比例,但宽高比仍不为1(MWE2)。(我会插入图片,但这似乎是不允许的,因为我的声誉不够高。)

MWE1:


我的我不确定我是否完全理解你的问题,但我认为这正是你想要的:

p <- ggplot(my, aes(factor(x), y)) + geom_tile(aes(fill = z)) +
  scale_fill_gradient(low = "white",high = "steelblue") +
  theme_grey() +
  labs(x = "", y= "") +
  coord_fixed() +
  theme(axis.ticks = element_blank())

print(p)
p
my <- data.frame(x=c(rep(c(0.1),3),rep(c(0.3),3),rep(c(0.5),3)),y=rep(c("frac(SP1)=0.5", "frac(SP1)=0.7", "frac(SP2)=0,3"),3),z=sample(seq(1:50),9))

p <- ggplot(my, aes(x, y)) + geom_tile(aes(fill = z)) +
  scale_fill_gradient(low = "white",high = "steelblue") +
  theme_grey() +
  labs(x = "", y= "") +
  scale_x_continuous(breaks=c(0.1,0.3,0.5),expand = c(0,0)) +
  scale_y_discrete(expand = c(0,0)) +
  coord_fixed(ratio=1) +
  theme(axis.ticks = element_blank())
p <- ggplot(my, aes(factor(x), y)) + geom_tile(aes(fill = z)) +
  scale_fill_gradient(low = "white",high = "steelblue") +
  theme_grey() +
  labs(x = "", y= "") +
  coord_fixed() +
  theme(axis.ticks = element_blank())

print(p)