使用R中的ggscatter通过组着色

时间:2021-02-22 06:12:31

I am using ggscatter function from ggpubr library to make a scatter plot. My data frame looks like this

我正在使用ggpubr库中的ggscatter函数来制作散点图。我的数据框看起来像这样

1   a   b   chr17   +   0.003   0.005   0,2 282232  4,0 253259  non_sig
10  a   b   chr22   -   0.733   0.6855  16,17   3,3 24,45   11,4    non_sig
12  a   b   chr13   +   0.7625  0.7965  22,14   1,7 7,18    1,4 non_sig
14  a   b   chr13   +   0.4555  0.369   20,16   19,12   4,23    17,11   non_sig
15  a   b   chr13   +   0.488   0.384   27,15   19,12   7,18    17,11   non_sig
16  a   b   chr16   -   0.9715  0.978   200141  3,2 260280  3,3 non_sig
21  a   b   chr1    +   0.9365  0.933   149118  1,12    133175  11,5    non_sig
22  a   b   chrX    +   0.6475  0.7265  129,57  58,35   104,78  37,29   non_sig
26  a   b   chr3    +   0.05    0.0475  54,32   721503  46,27   519617  non_sig
27  a   b   chr3    +   0.0475  0.045   57,34   721503  47,30   519617  non_sig

This is the command I am using

这是我正在使用的命令

library("ggpubr")
df <- read.table("test.txt",header =F,sep="\t")
ggscatter(df,x= "V6",y= "V7",color = "V12", shape = 21, size = 1,add = "reg.line",cor.coef = TRUE, cor.method = "pearson",conf.int = TRUE,title="A3SS(4561)",xlab="Ψ2",ylab = "Ψ1",
                  palette = c("black", "red"))

I want to color the points using 12th column which has either non_sig or sig as the value and on base of that, if non_sig, I want it to be black and if sig, I want it to be red

我想使用第12列为点着色,其中包含non_sig或sig作为值,如果是non_sig,我希望它是黑色的,如果是sig,我希望它是红色的

When I use the above code it does what I want, but how can I specifically code here for

当我使用上面的代码时它会做我想要的,但我怎么能在这里专门编写代码

sig=>red
non_sig=>black

Thanks for the help!!

谢谢您的帮助!!

2 个解决方案

#1


3  

I assume by "color the points using 12the column" you mean to fill points with a colour based on column V12.

我假设“使用12列的颜色”,你的意思是用基于列V12的颜色填充点。

Note that your sample data only contains V12 = "non_sig" entries, so I have manually changed one entry to "sig"

请注意,您的示例数据仅包含V12 =“non_sig”条目,因此我手动将一个条目更改为“sig”

library(ggpubr)
ggscatter(
    df,
    x= "V6", y= "V7",
    fill = "V12",
    shape = 21,
    size = 5,
    add = "reg.line",
    cor.coef = TRUE,
    cor.method = "pearson",
    conf.int = TRUE,
    title="A3SS(4561)",
    xlab="Ψ2",
    ylab = "Ψ1",
    palette = c("black", "red"))

使用R中的ggscatter通过组着色


Sample data

df <- read.table(text =
    "1   a   b   chr17   +   0.003   0.005   0,2 282232  4,0 253259  non_sig
10  a   b   chr22   -   0.733   0.6855  16,17   3,3 24,45   11,4    non_sig
12  a   b   chr13   +   0.7625  0.7965  22,14   1,7 7,18    1,4 non_sig
14  a   b   chr13   +   0.4555  0.369   20,16   19,12   4,23    17,11   non_sig
15  a   b   chr13   +   0.488   0.384   27,15   19,12   7,18    17,11   sig
16  a   b   chr16   -   0.9715  0.978   200141  3,2 260280  3,3 non_sig
21  a   b   chr1    +   0.9365  0.933   149118  1,12    133175  11,5    non_sig
22  a   b   chrX    +   0.6475  0.7265  129,57  58,35   104,78  37,29   non_sig
26  a   b   chr3    +   0.05    0.0475  54,32   721503  46,27   519617  non_sig
27  a   b   chr3    +   0.0475  0.045   57,34   721503  47,30   519617  non_sig", header = F)

Update

In response to your comment, you can use a named vector for your palette argument; e.g.

为了回应您的评论,您可以为调色板参数使用命名向量;例如

df <- read.table(text =
    "1   a   b   chr17   +   0.003   0.005   0,2 282232  4,0 253259  non_sig
10  a   b   chr22   -   0.733   0.6855  16,17   3,3 24,45   11,4    non_sig
12  a   b   chr13   +   0.7625  0.7965  22,14   1,7 7,18    1,4 non_sig
14  a   b   chr13   +   0.4555  0.369   20,16   19,12   4,23    17,11   non_sig
15  a   b   chr13   +   0.488   0.384   27,15   19,12   7,18    17,11   sig
16  a   b   chr16   -   0.9715  0.978   200141  3,2 260280  3,3 non_sig
21  a   b   chr1    +   0.9365  0.933   149118  1,12    133175  11,5    non_sig
22  a   b   chrX    +   0.6475  0.7265  129,57  58,35   104,78  37,29   test
26  a   b   chr3    +   0.05    0.0475  54,32   721503  46,27   519617  non_sig
27  a   b   chr3    +   0.0475  0.045   57,34   721503  47,30   519617  non_sig", header = F)


ggscatter(
    df,
    x= "V6", y= "V7",
    fill = "V12",
    shape = 21,
    size = 5,
    palette = c(test = "black", sig = "red", non_sig = "orange"))

使用R中的ggscatter通过组着色

#2


1  

Assuming that the variables in column 12 are factors, their default ordering is alphabetical. So in your example the first palette colour ("black") goes to the first factor level ("non_sig"); the second colour ("red") goes to the second factor ("sig").

假设第12列中的变量是因子,它们的默认排序是按字母顺序排列的。所以在你的例子中,第一个调色板颜色(“黑色”)转到第一个因子级别(“non_sig”);第二种颜色(“红色”)转到第二个因子(“sig”)。

If you want to assign colours differently, you need to reorder either the factor levels or the colour names in the palette. For example to assign "black", "red" and "green" to the factors "sig", "non_sig" and "new_var", you could do something like:

如果要以不同方式指定颜色,则需要重新排序调色板中的因子级别或颜色名称。例如,为“sig”,“non_sig”和“new_var”分配“黑色”,“红色”和“绿色”,您可以执行以下操作:

df$V12 <- factor(df$V12, levels = c("sig", "non_sig", "new_var"))

then in the plot:

然后在情节中:

palette = c("black", "red", "green")

#1


3  

I assume by "color the points using 12the column" you mean to fill points with a colour based on column V12.

我假设“使用12列的颜色”,你的意思是用基于列V12的颜色填充点。

Note that your sample data only contains V12 = "non_sig" entries, so I have manually changed one entry to "sig"

请注意,您的示例数据仅包含V12 =“non_sig”条目,因此我手动将一个条目更改为“sig”

library(ggpubr)
ggscatter(
    df,
    x= "V6", y= "V7",
    fill = "V12",
    shape = 21,
    size = 5,
    add = "reg.line",
    cor.coef = TRUE,
    cor.method = "pearson",
    conf.int = TRUE,
    title="A3SS(4561)",
    xlab="Ψ2",
    ylab = "Ψ1",
    palette = c("black", "red"))

使用R中的ggscatter通过组着色


Sample data

df <- read.table(text =
    "1   a   b   chr17   +   0.003   0.005   0,2 282232  4,0 253259  non_sig
10  a   b   chr22   -   0.733   0.6855  16,17   3,3 24,45   11,4    non_sig
12  a   b   chr13   +   0.7625  0.7965  22,14   1,7 7,18    1,4 non_sig
14  a   b   chr13   +   0.4555  0.369   20,16   19,12   4,23    17,11   non_sig
15  a   b   chr13   +   0.488   0.384   27,15   19,12   7,18    17,11   sig
16  a   b   chr16   -   0.9715  0.978   200141  3,2 260280  3,3 non_sig
21  a   b   chr1    +   0.9365  0.933   149118  1,12    133175  11,5    non_sig
22  a   b   chrX    +   0.6475  0.7265  129,57  58,35   104,78  37,29   non_sig
26  a   b   chr3    +   0.05    0.0475  54,32   721503  46,27   519617  non_sig
27  a   b   chr3    +   0.0475  0.045   57,34   721503  47,30   519617  non_sig", header = F)

Update

In response to your comment, you can use a named vector for your palette argument; e.g.

为了回应您的评论,您可以为调色板参数使用命名向量;例如

df <- read.table(text =
    "1   a   b   chr17   +   0.003   0.005   0,2 282232  4,0 253259  non_sig
10  a   b   chr22   -   0.733   0.6855  16,17   3,3 24,45   11,4    non_sig
12  a   b   chr13   +   0.7625  0.7965  22,14   1,7 7,18    1,4 non_sig
14  a   b   chr13   +   0.4555  0.369   20,16   19,12   4,23    17,11   non_sig
15  a   b   chr13   +   0.488   0.384   27,15   19,12   7,18    17,11   sig
16  a   b   chr16   -   0.9715  0.978   200141  3,2 260280  3,3 non_sig
21  a   b   chr1    +   0.9365  0.933   149118  1,12    133175  11,5    non_sig
22  a   b   chrX    +   0.6475  0.7265  129,57  58,35   104,78  37,29   test
26  a   b   chr3    +   0.05    0.0475  54,32   721503  46,27   519617  non_sig
27  a   b   chr3    +   0.0475  0.045   57,34   721503  47,30   519617  non_sig", header = F)


ggscatter(
    df,
    x= "V6", y= "V7",
    fill = "V12",
    shape = 21,
    size = 5,
    palette = c(test = "black", sig = "red", non_sig = "orange"))

使用R中的ggscatter通过组着色

#2


1  

Assuming that the variables in column 12 are factors, their default ordering is alphabetical. So in your example the first palette colour ("black") goes to the first factor level ("non_sig"); the second colour ("red") goes to the second factor ("sig").

假设第12列中的变量是因子,它们的默认排序是按字母顺序排列的。所以在你的例子中,第一个调色板颜色(“黑色”)转到第一个因子级别(“non_sig”);第二种颜色(“红色”)转到第二个因子(“sig”)。

If you want to assign colours differently, you need to reorder either the factor levels or the colour names in the palette. For example to assign "black", "red" and "green" to the factors "sig", "non_sig" and "new_var", you could do something like:

如果要以不同方式指定颜色,则需要重新排序调色板中的因子级别或颜色名称。例如,为“sig”,“non_sig”和“new_var”分配“黑色”,“红色”和“绿色”,您可以执行以下操作:

df$V12 <- factor(df$V12, levels = c("sig", "non_sig", "new_var"))

then in the plot:

然后在情节中:

palette = c("black", "red", "green")