简体   繁体   English

使用geom_tile时,如何控制ggplot2中分类数据的y轴刻度?

[英]How can I control the y-axis scale for categorical data in ggplot2 when using geom_tile?

I am trying to make a heatmap using ggplot in an RMarkdown document. 我想在RMarkdown文档中使用ggplot创建热图。 The intended output is an html page. 预期的输出是一个html页面。 My input dataframe (npBasicLong)looks like this: 我的输入数据帧(npBasicLong)如下所示:

location    hh    medianEWS
--------    --    ---------
Alpha       1     2
Alpha       2     1
Alpha       3     3
Alpha       4     1
...(and so on)
Alpha       23    3
Beta        1     4
Beta        2     4
Beta        3     1

Using the following code... 使用以下代码......

ggplot(data = npBasicLong) +
  aes(x = hh) +
  aes(y = fct_rev(location)) + #fct_rev is used as a hack to reverse the ordering of the wards
  aes(fill = medianEWS) +
  geom_tile(colour = "white") +
  scale_fill_gradient2(name = 'Median\nNEWS\nScore', low = "#7cbc12", mid = "gold", high = "red", midpoint = 3) +
  geom_text (aes
             (label = medianEWS),
             size = 1.5) +
  scale_x_continuous('Hour',
                     expand = c(0, 0),
                     breaks = seq(0,24,1),
                     position = "top") +
  coord_fixed(ratio = 1) +
  theme(panel.grid.minor=element_blank(),
        panel.grid.major=element_blank(),
        axis.ticks = element_blank(),
        axis.title.y = element_blank(),
        text = element_text (size=8)
        )

... I get this... ......我明白了......

geom_tile热图 The problem is that the text is tiny because the only way I've found to stop the labels overlapping or looking crowded I've had to reduce the text size. 问题是文本很小,因为我发现阻止标签重叠或看起来拥挤的唯一方法我不得不减少文本大小。

If I change the coord_fixed ratio to 2 then rather than elongating the y axis ggplot just squashes the x-axis like so... 如果我将coord_fixed比率改为2然后而不是拉长y轴ggplot就像压缩x轴一样......

压扁的热图

I've tried using theme(aspect_ratio) instead of coord_fixed and that seems to make no difference. 我已经尝试使用theme(aspect_ratio)而不是coord_fixed,这似乎没有任何区别。 I've also tried setting the chunk output parameters (out.height and fig.height) but they don't seem to make any difference either. 我也尝试过设置块输出参数(out.height和fig.height),但它们似乎也没有任何区别。

How can I end up with a plot that has a sufficient vertical height that the labels can be readable on a web page without making the x axis squashed? 我怎样才能得到一个具有足够垂直高度的绘图,使得标签可以在网页上读取而不会使x轴被压扁?

tl;dr: The answer is to use fig.height and fig.width and ensure that you set them both. tl; dr:答案是使用fig.height和fig.width并确保将它们设置为两者。


More detail 更多详情

Spurred on by @Jon Spring's comments above I discovered the answer. 受@Jon Spring上述评论的推动,我发现了答案。 Posting it here in case it helps people in the future. 将它发布在这里,以防将来帮助人们。 In my previous explorations of the chunk options described in the zevross.com article I had made two mistakes. 在我之前对zevross.com文章中描述的块选项的探索中,我犯了两个错误。

Firstly I was using out.height/out.width to specify the output in px. 首先,我使用out.height / out.width来指定px中的输出。 This doesn't behave in a way that I could predict - changing the height from 800px to 1600px did not double the size of the image displayed in the knitted HTML page. 这不会以我能预测的方式运行 - 将高度从800px更改为1600px并不会使编织HTML页面中显示的图像大小翻倍。 Once I reached a certain threshold (possibly determined by the width of the container div within which all the notebook content sits) then increasing the values of out.height/width made no difference. 一旦我达到某个阈值(可能由所有笔记本内容所在的容器div的宽度决定),然后增加out.height / width的值没有任何区别。

My (possibly mistaken) reading of the zevross article was that out.height/width were like a more flexible version of fig.height/width that applied to both figures and images. 我的(可能是错误的)阅读zevross文章是out.height / width就像一个更灵活的fig.height / width版本,适用于图形和图像。 This is sort of true but I think it basically acts as a scaling factor. 这有点真实,但我认为它基本上是一个缩放因子。 If the problem is, as in my case, that the canvas aspect ratio is wrong then once it hits the max width of the container div then nothing useful happens. 如果问题是,在我的情况下,画布宽高比是错误的,那么一旦它达到容器div的最大宽度,那么没有任何有用的事情发生。

When I switched to using fig.height and fig.width then the plotting canvas size started to change, I could eliminate the pointless whitespace on either side of my plot and as a consequence the plot started to display bigger in the generated HTML document. 当我切换到使用fig.height和fig.width然后绘图画布大小开始改变时,我可以消除我的绘图两侧的无意义的空白,结果绘图开始在生成的HTML文档中显示更大。

The other mistake I had made in my previous experimentation was to only set one parameter. 我在之前的实验中犯下的另一个错误是只设置了一个参数。 My thought had been that I only cared about one dimension (the height in this case) so the content could determine the width. 我的想法是我只关心一个维度(在这种情况下是高度)所以内容可以决定宽度。 Setting only the height meant that nothing changed. 仅设置高度意味着没有任何改变。 You have to change both height and width. 你必须改变高度和宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM