如何使用ggplot2使y轴的最大值设置为计数的最大值上方的一个固定值?

huangapple go评论76阅读模式
英文:

How to make max of y-axis a set value above the maximum value of a count using ggplot2?

问题

我正在尝试使用ggplot2绘制一个简单的柱状图,包括实际计数的标签。这是在一个定期更新的数据库中,因此实际计数会随时间而变化。

因此,我希望在生成图表时,始终将y轴设置为最大计数的上方至少5个单位。我只能找到如何显式设置坐标轴的方法。

是否有办法获得我所期望的行为?

以下是我所做的示例:

library(ggplot2)

x <- c("A", "B", "B", "A", "B", "B", "A", "B", "B", "A", "B", "B", "A", "B", "B", "A", "B", "B", "A", "B", "B", "A", "B", "B", "A", "B", "B", "A", "B", "B", "A", "B", "B", "A", "B", "B", "A", "B", "B", "A", "B", "B", "A", "B", "B", "A", "B", "B", "A", "B", "B", "A", "B", "B")

df %>% 
  mutate(x = as.factor(x)) %>% 
  ggplot(aes(x))+
  geom_bar()+ 
  geom_text(stat='count', aes(label=..count..), hjust=-1)

默认示例

如何使用ggplot2使y轴的最大值设置为计数的最大值上方的一个固定值?

我想要的示例

如何使用ggplot2使y轴的最大值设置为计数的最大值上方的一个固定值?


<details>
<summary>英文:</summary>

I am trying to plot a simple bar chart of counts using ggplot2, including labels of the actual counts. This is in a regularly updated database so that the actual counts will change over time.

As a result, I want to bake in the y-axis always to be at least 5 above the maximum count when I generate the plot. I an only find how to explicitly set the axes. 

Is there a way to get the behavior I am looking for?   

Below is an example of what I have done

library(ggplot2)

x<- c("A","B","B","A","B","B","A","B","B","A","B","B","A","B","B","A","B","B","A","B","B","A", "B","B","A","B","B","A","B","B","A","B","B","A","B","B","A","B","B","A","B","B","A", "B","B","A","B","B","A","B","B","A","B","B","A","B","B","A","B","B")

df %>%
mutate(x = as.factor(x)) %>%
ggplot(aes(x))+
geom_bar()+
geom_text(stat='count', aes(label=..count..), hjust=-1)

Example of defaults

[![Example output of what I have been doing][1]][1]

Example of what I want

[![Example output of what I want to happen][2]][2]


  [1]: https://i.stack.imgur.com/jIJXs.png
  [2]: https://i.stack.imgur.com/NfLZv.png

</details>


# 答案1
**得分**: 2

我们可以像这样将 `n` 的最大值增加 `+5%`:

```R
library(tidyverse)

df %>% 
  count(x) %>% 
  mutate(max_x = max(n) * 1.05) %>% 
  ggplot(aes(x = n, y = x)) +
  geom_col() +
  geom_text(aes(label = n), hjust = -1) +
  xlim(0, unique(df1$max_x))

如何使用ggplot2使y轴的最大值设置为计数的最大值上方的一个固定值?

英文:

We could add +5% to the max of n like this:

library(tidyverse)

df %&gt;% 
  count(x) %&gt;% 
  mutate(max_x = max(n)*1.05) %&gt;% 
  ggplot( aes(x = n, y=x))+
  geom_col()+
  geom_text(aes(label = n),  hjust=-1)+
  xlim(0, unique(df1$max_x))

如何使用ggplot2使y轴的最大值设置为计数的最大值上方的一个固定值?

答案2

得分: 2

一种选择是使用隐形点技巧,即在您的条形图下添加一个带有透明彩色点的 geom_point 层,其中您可以添加所需数量的“填充”。这将自动增加比例的范围:

library(ggplot2)

df <- data.frame(x = factor(x))

df %>%
  ggplot(aes(y = x)) +
  geom_point(aes(x = after_stat(count + 5)),
    stat = "count", color = "transparent"
  ) +
  geom_bar() +
  geom_text(
    stat = "count",
    aes(label = after_stat(count)), hjust = -1
  )

如何使用ggplot2使y轴的最大值设置为计数的最大值上方的一个固定值?

英文:

One option would be to use the invisible point trick, i.e. add a geom_point layer with transparent colored points beneath your bar charts for which you add your desired amount of "padding" to the count. This will automatically increase the range of the scale:

library(ggplot2)

df &lt;- data.frame(x = factor(x))

df |&gt;
  ggplot(aes(y = x)) +
  geom_point(aes(x = after_stat(count + 5)),
    stat = &quot;count&quot;, color = &quot;transparent&quot;
  ) +
  geom_bar() +
  geom_text(
    stat = &quot;count&quot;,
    aes(label = after_stat(count)), hjust = -1
  )

如何使用ggplot2使y轴的最大值设置为计数的最大值上方的一个固定值?

huangapple
  • 本文由 发表于 2023年3月10日 00:55:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定