如何改变ggplot中的binwidth?

发布于 2024-12-14 02:17:00 字数 422 浏览 1 评论 0原文

我正在用 ggplot 做直方图。

p <- ggplot(TotCalc, aes(x=x,y=100*(..count../sum(..count..)))) + 
    xlab(xlabel) + ylab(ylabel) +
    geom_histogram(colour = "darkblue", fill = "white", binwidth=500)

我的 x 介于 2 到 6580 之间,我有 2600 个数据。

我想绘制一个具有不同 binwidth 的直方图。是否可以?

例如,我想要 8 个条形,宽度如下:

c(180,100,110,160,200,250,1000,3000)

我该怎么做?

I'am doing histogram with ggplot.

p <- ggplot(TotCalc, aes(x=x,y=100*(..count../sum(..count..)))) + 
    xlab(xlabel) + ylab(ylabel) +
    geom_histogram(colour = "darkblue", fill = "white", binwidth=500)

my x is between 2 and 6580 and I have 2600 data.

I want to plot one histogram with different binwidth. Is it possible?

For example, I want to have 8 bars, with width like this:

c(180,100,110,160,200,250,1000,3000)

How can I do it?

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

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

发布评论

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

评论(2

骷髅 2024-12-21 02:17:00

使用休息时间怎么样?

x <- rnorm(100)
ggplot(NULL, aes(x)) + 
  geom_histogram(breaks = c(-5, -2, 0, 5), 
  position = "identity", colour = "black", fill = "white")

PS 未经明确通知,请勿转载。

在此处输入图像描述

how about using breaks?

x <- rnorm(100)
ggplot(NULL, aes(x)) + 
  geom_histogram(breaks = c(-5, -2, 0, 5), 
  position = "identity", colour = "black", fill = "white")

P.S. Please don't cross post without explicit notification.

enter image description here

负佳期 2024-12-21 02:17:00

使用 breaksposition="dodge"

例如:

ggplot(mtcars,aes(x=hp))+geom_histogram(breaks=c(50,100,200,350),position="dodge")

没有您的数据,但对于您的示例:

p <- ggplot(TotCalc, aes(x=x,y=100*(..count../sum(..count..)))) +
     xlab(xlabel) + ylab(ylabel) +
    geom_histogram(colour = "darkblue", fill = "white", breaks=cumsum(c(180,100,110,160,200,250,1000,3000)), position="dodge")

Use breaks and position="dodge"

eg:

ggplot(mtcars,aes(x=hp))+geom_histogram(breaks=c(50,100,200,350),position="dodge")

Don't have your data, but for your example:

p <- ggplot(TotCalc, aes(x=x,y=100*(..count../sum(..count..)))) +
     xlab(xlabel) + ylab(ylabel) +
    geom_histogram(colour = "darkblue", fill = "white", breaks=cumsum(c(180,100,110,160,200,250,1000,3000)), position="dodge")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文