R 中的维恩图指示每个类别中的不同颜色

问题描述 投票:0回答:1

我有以下脚本,它返回以下错误:

# Load required libraries
library(ggplot2)
library(ggVennDiagram)

# Define the tool categories based on the dataframe you provided
tools <- data.frame(
  Tools = c("ANNOVAR", "Ensembl VEP", "SnpEff & SnpSift", "MAGMA", "HaploReg",
            "OpenTargets Genetics", "GWAVA", "VarSome", "InterVar", "FUMA", 
            "VAT", "VAAST 2.0", "VPMBench", "OpenCRAVAT", "FAVORannotator"),
  Exonic = c(1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1),
  Intragenic_Non_Exonic = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1),
  Intergenic = c(1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1)
)

# Define the tool lists for each category
exonic <- tools$Tools[tools$Exonic == 1]
intragenic_non_exonic <- tools$Tools[tools$Intragenic_Non_Exonic == 1]
intergenic <- tools$Tools[tools$Intergenic == 1]

# Create the Venn diagram data
venn_data <- list(
  Exonic = exonic,
  Intragenic_Non_Exonic = intragenic_non_exonic,
  Intergenic = intergenic
)

# Save the diagram as a jpeg file
jpeg("venn_diagram_tools.jpg", units = "in", width = 5, height = 5, res = 300)

# Create the Venn diagram with different colors
ggVennDiagram(venn_data, label_alpha = 0) +
  scale_fill_manual(values = c("Exonic" = "red", "Intragenic_Non_Exonic" = "green", "Intergenic" = "blue")) + 
  theme_minimal() +
  ggtitle("Venn Diagram of Tools by Genomic Regions") +
  theme(
    axis.title = element_blank(),       # Remove axis titles
    axis.text = element_blank(),        # Remove axis text
    axis.ticks = element_blank(),       # Remove axis ticks
    panel.grid = element_blank(),       # Remove gridlines
    panel.background = element_blank(), # Remove background
    plot.background = element_blank()   # Remove plot background
  )

# Turn off the device to save the file
dev.off()

错误:

Error in `scale_fill_manual()`:
! Continuous values supplied to discrete scale.
ℹ Example values: 2, 2, 2, 2, and 2

这与分类变量有关,你能帮我解决这个问题,以便在 3 个类别中的每个类别中都有另一种颜色吗?

谢谢你

r venn-diagram
1个回答
0
投票

您的代码的问题是 ggVennDiagram 函数不直接支持通过 scale_fill_manual 指定的维恩图颜色。 您可以使用scale_fill_gradientn()定义多种颜色,包括第三种颜色。如果需要,您可以更改这些颜色

# Load required libraries
library(ggplot2)
library(ggVennDiagram)

# Define the tool categories based on the dataframe you provided
tools <- data.frame(
  Tools = c("ANNOVAR", "Ensembl VEP", "SnpEff & SnpSift", "MAGMA", "HaploReg",
            "OpenTargets Genetics", "GWAVA", "VarSome", "InterVar", "FUMA", 
            "VAT", "VAAST 2.0", "VPMBench", "OpenCRAVAT", "FAVORannotator"),
  Exonic = c(1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1),
  Intragenic_Non_Exonic = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1),
  Intergenic = c(1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1)
)

# Define the tool lists for each category
exonic <- tools$Tools[tools$Exonic == 1]
intragenic_non_exonic <- tools$Tools[tools$Intragenic_Non_Exonic == 1]
intergenic <- tools$Tools[tools$Intergenic == 1]

# Create the Venn diagram data
venn_data <- list(
  Exonic = exonic,
  Intragenic_Non_Exonic = intragenic_non_exonic,
  Intergenic = intergenic
)
# Save the diagram as a jpeg file
jpeg("venn_diagram_tools.jpg", units = "in", width = 5, height = 5, res = 300)
 
ggVennDiagram(venn_data, label_alpha = 0) +
  scale_fill_gradientn(colors = c("red", "green", "blue")) +  # Use three colors
  theme_minimal() +
  ggtitle("Venn Diagram of Tools by Genomic Regions") +
  theme(
    axis.title = element_blank(),       # Remove axis titles
    axis.text = element_blank(),        # Remove axis text
    axis.ticks = element_blank(),       # Remove axis ticks
    panel.grid = element_blank(),       # Remove gridlines
    panel.background = element_blank(), # Remove background
    plot.background = element_blank()   # Remove plot background
  )

# Turn off the device to save the file
dev.off()

我还附上了你的图表 enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.