如何在直方图中去除低频箱

发布于 2025-02-01 18:00:37 字数 606 浏览 3 评论 0原文

假设我有一个数据框架,其中包含一个数字数组,我想在直方图中可视化。我要实现的是仅显示包含更多的垃圾箱,而不是说50个观察结果。

步骤1

set.seed(10)
x <- data.frame(x = rnorm(1000, 50, 2))
p <- 
  x %>% 
  ggplot(., aes(x)) +
  geom_histogram()

p

”在此处输入映像说明“

步骤2

pg <- ggplot_build(p)

pg$data[[1]]

作为检查当我打印pg $ data [[1]] i'时d喜欢只有<代码> count&gt; = 50 的行。

谢谢

Let's say I've a data frame containing an array of numbers which I want to visualise in a histogram. What I want to achieve is to show only the bins containing more than let's say 50 observations.

Step 1

set.seed(10)
x <- data.frame(x = rnorm(1000, 50, 2))
p <- 
  x %>% 
  ggplot(., aes(x)) +
  geom_histogram()

p

enter image description here

Step 2

pg <- ggplot_build(p)

pg$data[[1]]

As a check when I print the pg$data[[1]] I'd like to have only rows where count >= 50.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸印温柔 2025-02-08 18:00:37
library(ggplot2)

ggplot(x, aes(x=x, y = ifelse(..count.. > 50, ..count.., 0))) +
  geom_histogram(bins=30) 

使用此代码,您可以看到已删除的垃圾箱的计数:

library(ggplot2)

ggplot(x, aes(x=x, y = ifelse(..count.. > 50, ..count.., 0))) +
  geom_histogram(bins=30, fill="green", color="grey") +
  stat_bin(aes(label=..count..), geom="text", vjust = -0.7)

“在此处输入图像说明”

library(ggplot2)

ggplot(x, aes(x=x, y = ifelse(..count.. > 50, ..count.., 0))) +
  geom_histogram(bins=30) 

enter image description here

With this code you can see the counts of the deleted bins:

library(ggplot2)

ggplot(x, aes(x=x, y = ifelse(..count.. > 50, ..count.., 0))) +
  geom_histogram(bins=30, fill="green", color="grey") +
  stat_bin(aes(label=..count..), geom="text", vjust = -0.7)

enter image description here

雨巷深深 2025-02-08 18:00:37

您可以做这样的事情,很可能您真的不喜欢X轴上的分解名称,但是您可以做的就是将两个值分开,并以平均值为例绘制X轴。

x %>%
  mutate(bin = cut(x, breaks = 30)) %>%
  group_by(bin) %>%
  mutate(count = n()) %>%
  filter(count > 50) %>% 
  ggplot(., aes(bin)) +
  geom_histogram(stat = "count")

You could do something like this, most likely you do not really like the factorized names on the x-axis, but what you can do is split the two values and take the average to take that one to plot the x-axis.

x %>%
  mutate(bin = cut(x, breaks = 30)) %>%
  group_by(bin) %>%
  mutate(count = n()) %>%
  filter(count > 50) %>% 
  ggplot(., aes(bin)) +
  geom_histogram(stat = "count")

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文