R 中同一图中 2 个因子的值的绘图频率
我想绘制针对 2 个因子水平编码的变量颜色的频率,例如蓝色条应该是水平 A 的历史记录,绿色条应该是水平 B 的历史记录,两者都在同一个图表中?这可以用 hist 命令实现吗? hist 的帮助不允许有任何因素。还有其他办法吗?
我设法通过 barplots 手动完成此操作,但我想问是否有更自动的方法
非常感谢 欧共体
PS。我不需要密度图
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是很有可能的。
我没有可以使用的数据,但这里有一个带有不同颜色条的直方图示例。从这里开始,您需要使用我的代码并弄清楚如何使其适用于因子而不是尾部。
基本设置
直方图 <- hist(scale(vector))、breaks= 、plot=FALSE)
绘图(直方图,col=ifelse(abs(直方图$breaks)<#of SD,颜色1,颜色2))
It's very possible.
I didn't have data to work with but here's an example of a histogram with different colored bars. From here you'd need to use my code and figure out how to make it work for factors instead of tails.
BASIC SETUP
histogram <- hist(scale(vector)), breaks= , plot=FALSE)
plot(histogram, col=ifelse(abs(histogram$breaks) < #of SD, Color 1, Color 2))
万一其他人没有回答,这是一种令人满意的方式。我最近不得不处理堆积直方图,这就是我所做的:
希望这就是您的意思?
Just in case the others haven't answered this is a way that satisfies. I had to deal with stacking histograms recently, and here's what I did:
Hopefully, this is what you meant?
我认为你不能用条形直方图轻松做到这一点,因为你必须“交错”两个因子水平的条形......它需要对现在连续的 x 轴进行某种“离散化”(即它必须分成“类别”,并且在每个类别中,对于每个因子水平,您将有 2 个条形...
但是,如果您擅长绘制密度线函数,那么这很容易并且没有问题:
I don't think you can do that easily with a bar histogram, as you would have to "interlace" the bars from both factor levels... It would need some kind of "discretization" of the now continuous x axis (i.e. it would have to be split in "categories" and in each category you would have 2 bars, for each factor level...
But it is quite easy and without problems if you are just fine with plotting the density line function:
我同意其他人的观点,即密度图比合并直方图的彩色条更有用,特别是当组的值混合时。这会非常困难,而且不会告诉你太多信息。您从其他人那里得到了关于密度图的一些很好的建议,这是我有时使用的密度图的 2 美分:
I agree with the others that a density plot is more useful than merging colored bars of a histogram, particularly if the group's values are intermixed. This would be very difficult and wouldn't really tell you much. You've got some great suggestions from others on density plots, here's my 2 cents for density plots that I sometimes use:
目前尚不清楚您的数据布局是什么。直方图要求您有一个有序或连续的变量,以便可以创建中断。如果您还有一个单独的分组因素,您可以根据该因素绘制直方图。 lattice 包中的
histogram
函数的帮助页面上的第二个示例提供了这种分组和叠加密度曲线的一个很好的示例。学习 R 博客是学习lattice和ggplot2绘图相对优点的一个很好的资源。 这是两个绘图系统并排比较的多部分系列的第一篇:
It's rather unclear what you have as a data layout. A histogram requires that you have a variable that is ordinal or continuous so that breaks can be created. If you also have a separate grouping factor you can plot histograms conditional on that factor. A nice worked example of such a grouping and overlaying a density curve is offered in the second example on the help page for the
histogram
function in the lattice package.A nice resource for learning relative merits of lattice and ggplot2 plotting is the Learning R blog. This is from the first of a multipart series on side-by=side comparison of the two plotting systems: