R 中同一图中 2 个因子的值的绘图频率

发布于 2024-12-11 21:16:04 字数 270 浏览 0 评论 0 原文

我想绘制针对 2 个因子水平编码的变量颜色的频率,例如蓝色条应该是水平 A 的历史记录,绿色条应该是水平 B 的历史记录,两者都在同一个图表中?这可以用 hist 命令实现吗? hist 的帮助不允许有任何因素。还有其他办法吗?

我设法通过 barplots 手动完成此操作,但我想问是否有更自动的方法

在此处输入图像描述

非常感谢 欧共体

PS。我不需要密度图

I'd like to plot the frequency of a variable color coded for 2 factor levels for example blue bars should be the hist of level A and green the hist of level B both n the same graph? Is this possible with the hist command? The help of hist does not allow for a factor. Is there another way around?

I managed to do this by barplots manually but i want to ask if there is a more automatic method

enter image description here

Many thanks
EC

PS. I dont need density plots

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

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

发布评论

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

评论(5

早乙女 2024-12-18 21:16:05

这是很有可能的。

我没有可以使用的数据,但这里有一个带有不同颜色条的直方图示例。从这里开始,您需要使用我的代码并弄清楚如何使其适用于因子而不是尾部。

基本设置
直方图 <- hist(scale(vector))、breaks= 、plot=FALSE)
绘图(直方图,col=ifelse(abs(直方图$breaks)<#of SD,颜色1,颜色2))

#EXAMPLE
x<-rnorm(1000)
histogram <- hist(scale(x), breaks=20 , plot=FALSE)
plot(histogram, col=ifelse(abs(histogram$breaks) < 2, "red", "green"))

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))

#EXAMPLE
x<-rnorm(1000)
histogram <- hist(scale(x), breaks=20 , plot=FALSE)
plot(histogram, col=ifelse(abs(histogram$breaks) < 2, "red", "green"))
┼── 2024-12-18 21:16:05

万一其他人没有回答,这是一种令人满意的方式。我最近不得不处理堆积直方图,这就是我所做的:

data_sub <- subset(data, data$V1 == "Yes") #only samples that have V1 as "yes" in my dataset #are added to the subset

hist(data$HL)
hist(data_sub$HL, col="red", add=T)

希望这就是您的意思?

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:

data_sub <- subset(data, data$V1 == "Yes") #only samples that have V1 as "yes" in my dataset #are added to the subset

hist(data$HL)
hist(data_sub$HL, col="red", add=T)

Hopefully, this is what you meant?

离不开的别离 2024-12-18 21:16:05

我认为你不能用条形直方图轻松做到这一点,因为你必须“交错”两个因子水平的条形......它需要对现在连续的 x 轴进行某种“离散化”(即它必须分成“类别”,并且在每个类别中,对于每个因子水平,您将有 2 个条形...

但是,如果您擅长绘制密度线函数,那么这很容易并且没有问题:

y <- rnorm(1000, 0, 1)
x <- rnorm(1000, 0.5, 2)
dx <- density(x)
dy <- density(y)
plot(dx, xlim = range(dx$x, dy$x), ylim = range(dx$y, dy$y), 
     type = "l", col = "red")
lines(dy, col = "blue")

在此处输入图像描述

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:

y <- rnorm(1000, 0, 1)
x <- rnorm(1000, 0.5, 2)
dx <- density(x)
dy <- density(y)
plot(dx, xlim = range(dx$x, dy$x), ylim = range(dx$y, dy$y), 
     type = "l", col = "red")
lines(dy, col = "blue")

enter image description here

随风而去 2024-12-18 21:16:05

我同意其他人的观点,即密度图比合并直方图的彩色条更有用,特别是当组的值混合时。这会非常困难,而且不会告诉你太多信息。您从其他人那里得到了关于密度图的一些很好的建议,这是我有时使用的密度图的 2 美分:

y <- rnorm(1000, 0, 1) 
x <- rnorm(1000, 0.5, 2) 
DF <- data.frame("Group"=c(rep(c("y","x"), each=1000)), "Value"=c(y,x))

library(sm)

with(DF, sm.density.compare(Value, Group, xlab="Grouping"))
title(main="Comparative Density Graph")
legend(-9, .4, levels(DF$Group), fill=c("red", "darkgreen")) 

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:

y <- rnorm(1000, 0, 1) 
x <- rnorm(1000, 0.5, 2) 
DF <- data.frame("Group"=c(rep(c("y","x"), each=1000)), "Value"=c(y,x))

library(sm)

with(DF, sm.density.compare(Value, Group, xlab="Grouping"))
title(main="Comparative Density Graph")
legend(-9, .4, levels(DF$Group), fill=c("red", "darkgreen")) 
我也只是我 2024-12-18 21:16:04

目前尚不清楚您的数据布局是什么。直方图要求您有一个有序或连续的变量,以便可以创建中断。如果您还有一个单独的分组因素,您可以根据该因素绘制直方图。 lattice 包中的 histogram 函数的帮助页面上的第二个示例提供了这种分组和叠加密度曲线的一个很好的示例。
Secondlattice::histgram example

学习 R 博客是学习lattice和ggplot2绘图相对优点的一个很好的资源。 这是两个绘图系统并排比较的多部分系列的第一篇

library(lattice)
 library(ggplot2)
 data(Chem97, package = "mlmRev")
#The lattice method:
pl <- histogram(~gcsescore | factor(score), data = Chem97)
 print(pl)

点阵直方图

# The ggplot method:
 pg <- ggplot(Chem97, aes(gcsescore)) + geom_histogram(binwidth = 0.5) +
     facet_wrap(~score)
 print(pg)

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.
Second lattice::histgram example

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:

library(lattice)
 library(ggplot2)
 data(Chem97, package = "mlmRev")
#The lattice method:
pl <- histogram(~gcsescore | factor(score), data = Chem97)
 print(pl)

Lattice histogram

# The ggplot method:
 pg <- ggplot(Chem97, aes(gcsescore)) + geom_histogram(binwidth = 0.5) +
     facet_wrap(~score)
 print(pg)

enter image description here

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