Manuela Lanzafame
Manuela Lanzafame

Reputation: 1

ggplot gene expression of different samples with different colors

I have a table of gene expression data (dataset). The first column is gene_names and the other columns is the expression in 2 different seamples, as shown below

dataset ...

with the function ggplot(data=dataset, mapping = aes(x=gene_name, y=sample1)) + geom_point (color="navy")

I'm able to plot the expression profile of one sample along all the genes. what I would like to do is a plot in which for each gene I have the expression level of the two samples (so 2 dots) colored with 2 different colors. How should I do? thanks a lot

Upvotes: 0

Views: 267

Answers (1)

Mohan Govindasamy
Mohan Govindasamy

Reputation: 906

You need to convert your dataset into a long dataset.

library(tidyverse)

tribble(~gene_name, ~sample1, ~sample2,
        "gene1", 1.25, 2.5,
        "gene2", 2.3, 3.4,
        "gene3", -1.5, 6.2) -> dataset

dataset %>% 
  pivot_longer(-gene_name) -> long_dataset

long_dataset
#> # A tibble: 6 × 3
#>   gene_name name    value
#>   <chr>     <chr>   <dbl>
#> 1 gene1     sample1  1.25
#> 2 gene1     sample2  2.5 
#> 3 gene2     sample1  2.3 
#> 4 gene2     sample2  3.4 
#> 5 gene3     sample1 -1.5 
#> 6 gene3     sample2  6.2

ggplot(data=long_dataset, mapping = aes(x=gene_name, y=value, color = name)) + geom_point ()

Created on 2022-07-14 by the reprex package (v2.0.1)

Upvotes: 0

Related Questions