改变情节的外观和感觉以类似于历史

发布于 2024-08-18 19:39:31 字数 208 浏览 5 评论 0原文

我使用这篇文章中的信息创建了一个具有对数刻度的直方图: 具有对数刻度的直方图

但是,plot 的输出看起来与 hist 的输出完全不同。有谁知道如何配置绘图的输出以类似于 hist 的输出?感谢您的帮助。

I used the information from this post to create a histogram with logarithmic scale:
Histogram with Logarithmic Scale

However, the output from plot looks nothing like the output from hist. Does anyone know how to configure the output from plot to resemble the output from hist? Thanks for the help.

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

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

发布评论

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

评论(2

征﹌骨岁月お 2024-08-25 19:39:31

链接答案的简化、可重现版本是

x <- rlnorm(1000)
hx <- hist(x, plot=FALSE)
plot(hx$counts, type="h", log="y", lwd=10, lend="square")

为了让轴看起来更像“hist-like”,请将最后一行替换为

plot(hx$counts, type="h", log="y", lwd=10, lend="square", axes = FALSE)
Axis(side=1)
Axis(side=2)

使用此方法将条形图连接起来将是一场噩梦。我建议使用 lwd 值进行反复试验(在本例中,34 看起来很接近正确),或者学习使用 latticeggplot.


编辑:
您无法设置边框颜色,因为条形并不是真正的矩形 - 它们只是粗线。我们可以通过在顶部绘制稍细的线条来伪造边框效果。更新后的代码是

par(lend="square")
bordercol <- "blue"
fillcol <- "pink"
linewidth <- 24
plot(hx$counts, type="h", log="y", lwd=linewidth, col=bordercol, axes = FALSE)
lines(hx$counts, type="h", lwd=linewidth-2, col=fillcol)
Axis(side=1)
Axis(side=2)

A simplified, reproducible version of the linked answer is

x <- rlnorm(1000)
hx <- hist(x, plot=FALSE)
plot(hx$counts, type="h", log="y", lwd=10, lend="square")

To get the axes looking more "hist-like", replace the last line with

plot(hx$counts, type="h", log="y", lwd=10, lend="square", axes = FALSE)
Axis(side=1)
Axis(side=2)

Getting the bars to join up is going to be a nightmare using this method. I suggest using trial and error with values of lwd (in this example, 34 is somewhere close to looking right), or learning to use lattice or ggplot.


EDIT:
You can't set a border colour, because the bars aren't really rectangles – they are just fat lines. We can fake the border effect by drawing slightly thinner lines over the top. The updated code is

par(lend="square")
bordercol <- "blue"
fillcol <- "pink"
linewidth <- 24
plot(hx$counts, type="h", log="y", lwd=linewidth, col=bordercol, axes = FALSE)
lines(hx$counts, type="h", lwd=linewidth-2, col=fillcol)
Axis(side=1)
Axis(side=2)
末骤雨初歇 2024-08-25 19:39:31

使用ggplot2怎么样?

x <- rnorm(1000)
qplot(x) + scale_y_log10()

但我同意哈德利在另一篇文章中的评论,即具有对数刻度的直方图对我来说似乎很奇怪 =)。

How about using ggplot2?

x <- rnorm(1000)
qplot(x) + scale_y_log10()

But I agree with Hadley's comment on the other post that having a histogram with a log scale seems weird to me =).

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