简体   繁体   English

R-根据p值的颜色图

[英]R - Colour plots according to p-value

I have hundreds of boxplots that are printed automatically from my huge dataset and I have fabricated the code to colour the plots 'coral' if the variable has a statistical significance across the selected groups and 'aquamarine' if significance is not detected. 我有数百个箱型图,这些箱型图是从庞大的数据集中自动打印出来的,如果变量在所选组中具有统计显着性,则我编写了代码以对图“珊瑚”上色;如果未检测到显着性,则制造了代码。 This I did with col=ifelse(...< 0.05,'coral','aquamarine') . 我用col=ifelse(...< 0.05,'coral','aquamarine')做到了。

I though my output would be much easier to skim through if the boxplots were coloured according to the p-value. 尽管如果根据p值对箱形图进行着色,我的输出将更容易浏览。 So I would like to tell col=... to colour plots from variable with p-value in the range 0.05 - 0.01 to yellow, 0.01 - 0.005 to orange and <0.005 to red. 因此,我想告诉col=...为p值介于0.05-0.01到黄色,0.01-0.005到橙色和<0.005到红色的变量的彩色图着色。

I tried this with 我尝试过

boxplot(a~b, data=df, col=if(pv1<0.05)"yellow" else if (pv1<0.01) "orange" else if (pv1<0.005) "red" else "green")

As you can see that this wont do because the values overlap. 如您所见,因为值重叠,所以不会这样做。 What I need to do is write the ranges inside the if sentence but I just dont know how to. 我需要做的是将范围写在if语句中,但我只是不知道该怎么做。

Here is some data to work with. 这是一些数据。 Note that the actual p-values of the groups are not the same as give here. 请注意,各组的实际p值与此处给出的值不同。 I just display pv1, 2 and 3 so you can test out the code. 我只显示pv1、2和3,以便您可以测试代码。

a <- c(23,24,64,12,4,75,12,65,86,76)
b <- c(1,2,2,1,2,1,3,3,1,3)
df  <-  data.frame(a,b)
pv1  <- c(0.05)
pv2  <- c(0.01)
pv3  <- c(0.005)

To modify your if...else statement, you should think it the other way around (here, you're asking that "if it is not < 0.05, then if it is < 0.01..." which is not possible), so you can write: 要修改if...else语句,您应该反过来考虑(在这里,您要求“如果它不<0.05,那么它<0.01 ...”,那是不可能的),所以你可以这样写:

col=if(pv1<0.005)"red" else if (pv1<0.01) "orange" else if (pv1<0.05) "yellow" else "green"

Example

pv1 <- 0.04 ; if(pv1<0.005)"red" else if (pv1<0.01) "orange" else if (pv1<0.05) "yellow" else "green" # "yellow"
pv1 <- 0.004 ; if(pv1<0.005)"red" else if (pv1<0.01) "orange" else if (pv1<0.05) "yellow" else "green" # "red"
pv1 <- 0.06 ; if(pv1<0.005) "red" else if (pv1<0.01) "orange" else if (pv1<0.05) "yellow" else "green" # "green"
pv1 <- 0.006 ; if(pv1<0.005) "red" else if (pv1<0.01) "orange" else if (pv1<0.05) "yellow" else "green" # "orange"

Another option , if your pvalues are in a vector, you can try with cut : 另一个选择 ,如果您的p值在向量中,则可以尝试使用cut

mycolours <- as.character(cut(pv1, ,c(1,0.05,0.01,0.005,0), right=F, labels=c("red","orange","yellow","aquamarine"), include.lowest=T))

And then 接着

boxplot(a~b, data=df, col=mycolours)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM