Adding median to Violin Plots ggplot2
1
0
Entering edit mode
12 months ago
Anupam ▴ 40

Hello All,

I want to make grouped violin plots from a table. I have a table of the following format:

Cell_Id         Cell_Type     nox4_expr        TP53
AAA1_AAACCCAAGGCCGCTT-1         Fibroblasts no_NOX4_Cells  0.42292722
AA1_AAACCCAGTATTTCGG-1 Smooth_Muscle_Cells no_NOX4_Cells -1.02533342
AAA1_AAACGAACAGGCTATT-1   Endothelial_Cells no_NOX4_Cells -1.23918040
AAA1_AAACGCTCACAAGTGG-1 Smooth_Muscle_Cells no_NOX4_Cells -0.30592846
AAA1_AAAGAACAGGAAAGGT-1 Smooth_Muscle_Cells no_NOX4_Cells -0.73323183
AAA1_AAAGGGCAGATGCCGA-1 Smooth_Muscle_Cells no_NOX4_Cells  0.75156034
AAA1_AAAGGTATCTGTCCCA-1   Endothelial_Cells no_NOX4_Cells -0.52521605
AAA1_AAAGTCCTCTTGGTCC-1 Smooth_Muscle_Cells no_NOX4_Cells -0.19304152
AAA1_AACAAAGCATGTGTCA-1   Endothelial_Cells no_NOX4_Cells -0.28536052
AAA1_AACAAAGTCCTGTTGC-1   Endothelial_Cells no_NOX4_Cells -0.48373745
AAA1_AACAAGATCGCCATAA-1         Fibroblasts no_NOX4_Cells -0.54716342
AAA1_AACACACTCACTCGAA-1         Fibroblasts no_NOX4_Cells  0.07636655
AAA1_AACAGGGCAGAGGAAA-1 Smooth_Muscle_Cells no_NOX4_Cells -1.96604142
AAA1_AACAGGGTCTACTATC-1 Smooth_Muscle_Cells no_NOX4_Cells -0.97004485
AAA1_AACCAACAGTGAGTGC-1   Endothelial_Cells NOX4_Cells -1.06456474
AAA1_AACCAACCATACCAGT-1 Smooth_Muscle_Cells no_NOX4_Cells -0.04470668
AAA1_AACCAACGTCACCGCA-1         Fibroblasts no_NOX4_Cells  1.51190518
AAA1_AACCAACGTGAATTGA-1         Fibroblasts no_NOX4_Cells -0.76614605
AAA1_AACCATGAGGATTCAA-1 Smooth_Muscle_Cells no_NOX4_Cells  0.71925013
AAA1_AACCTGAAGATGACAT-1 Smooth_Muscle_Cells no_NOX4_Cells -0.98475028
AAA1_AACCTGATCGCACGGT-1 Smooth_Muscle_Cells no_NOX4_Cells -1.09628709
AAA1_AACCTTTTCGGTGAAG-1         Fibroblasts NOX4_Cells -0.45117008
AAA1_AACGAAATCTCGTGGG-1 Smooth_Muscle_Cells NOX4_Cells -1.14700998
AAA1_AACGGGACATGAAGCG-1         Fibroblasts no_NOX4_Cells -0.34016949
AAA1_AACGTCACACAAGCAG-1 Smooth_Muscle_Cells no_NOX4_Cells -0.17526431
AAA1_AACGTCATCACCATCC-1         Fibroblasts no_NOX4_Cells -0.35808476
AAA1_AACGTCATCCACCTCA-1         Fibroblasts no_NOX4_Cells -0.26573515
AAA1_AAGAACACATATGAAG-1 Smooth_Muscle_Cells no_NOX4_Cells -0.39395033
AAA1_AAGAACAGTCCATACA-1         Fibroblasts no_NOX4_Cells  1.08556113
AAA1_AAGAACAGTTGACTAC-1         Fibroblasts    NOX4_Cells  2.41693267
AAA1_AAGACAACAGCGCTTG-1   Endothelial_Cells no_NOX4_Cells -1.14250292
AAA1_AAGCCATCACATGTTG-1 Smooth_Muscle_Cells no_NOX4_Cells -0.41232271
AAA1_AAGCGAGCAGACCATT-1   Endothelial_Cells no_NOX4_Cells  0.24044021
AAA1_AAGCGAGGTCTTGCGG-1         Fibroblasts NOX4_Cells -0.47326943
AAA1_AAGCGAGGTTATCTGG-1   Endothelial_Cells NOX4_Cells  0.06141235
AAA1_AAGCGAGTCCATACTT-1 Smooth_Muscle_Cells NOX4_Cells -1.00187397
AAA1_AAGCGTTCATTGACCA-1   Endothelial_Cells no_NOX4_Cells -1.98551134
AAA1_AAGGAATAGCTGTTCA-1   Endothelial_Cells no_NOX4_Cells  0.58095744
AAA1_AAGTCGTCACATATCG-1         Fibroblasts no_NOX4_Cells  0.07679935
AAA1_AAGTCGTCAGACCAAG-1         Fibroblasts no_NOX4_Cells -0.79393816
AAA1_AAGTGAACACTCTCGT-1         Fibroblasts no_NOX4_Cells  1.22087911
AAA1_AAGTGAATCTGCCTCA-1 Smooth_Muscle_Cells NOX4_Cells -0.24164262
AAA1_AAGTTCGTCAAACGAA-1 Smooth_Muscle_Cells no_NOX4_Cells -0.61149856
AAA1_AATCACGAGCACGTCC-1   Endothelial_Cells NOX4_Cells  0.90173135
AAA1_AATCACGGTCTCGCGA-1         Fibroblasts no_NOX4_Cells -0.47399920
AAA1_AATCACGGTGATTCTG-1         Fibroblasts no_NOX4_Cells -0.23503119
AAA1_AATCGACGTATCGAGG-1         Fibroblasts no_NOX4_Cells  0.94648594
AAA1_AATCGACGTCGTACTA-1         Fibroblasts    NOX4_Cells  0.98152549
AAA1_AATCGTGCATCGATAC-1 Smooth_Muscle_Cells NOX4_Cells -0.56436443
AAA1_AATGACCTCGCTAATG-1         Fibroblasts NOX4_Cells  1.54377599

Following is my code:

ggplot(t_t,aes(x=Cell_Type,y=TP53,fill=nox4_expr),pt.size=0,y.max=5)+ geom_violin()+ 
 stat_compare_means(aes(group=nox4_expr)) + 
  stat_summary(data=t_t,mapping=aes(x=Cell_Type,y=TP53,fill=nox4_expr),fun.data = "median",geom = "crossbar", width = 0.5,colour = "red")

But I keep getting this error. The first two layers work fine and I get violin plots with p-values. But the stat_summary never works. I need to add median to the violin plots.

Error in `stat_summary()`:
 ! Problem while setting up geom.
  Error occurred in the 3rd layer.
  Caused by error in `compute_geom_1()`:
 ! `geom_crossbar()` requires the following missing aesthetics: y, ymin, and ymax or y, xmin,
     and xmax
    Run `rlang::last_trace()` to see where the error occurred.

Can anyone please help me out?

Thanks in advance

Anupam

seurat ViolinPlots ggplot2 • 1.2k views
ADD COMMENT
0
Entering edit mode

Did you build the ggplot2 layers by yourself or get the code from somewhere?

ADD REPLY
0
Entering edit mode

Whats the point of your question ?

ADD REPLY
0
Entering edit mode

I'm trying to gain more context so I can help you better. If I were you, I'd have tried adding layer by layer until I got to the layer causing the problem, then adding parameter by parameter until I got to the parameter causing the problem - but that will only work if you understand the details of each layer, which can be difficult if you can get the code from somewhere.

You're asking for help on a volunteer forum. Why act offended when asked for details?

ADD REPLY
0
Entering edit mode

Well, I was trying to the understand the coding context of your question and was in no way offended. I have already stated that I am not getting Error with the first two layers. Its the third layer which is causing the issue.

ADD REPLY
1
Entering edit mode
12 months ago
Ram 44k

My bad on my comments - I did not read the part where you specify which layer is causing the problem.

Can you try fun="median" instead of fun.data="median" please?

ADD COMMENT
0
Entering edit mode

Thanks a lot for your response. Works like a charm...

ADD REPLY
0
Entering edit mode

I'll move my comment to an answer. Please accept it to provide closure to the post.

ADD REPLY

Login before adding your answer.

Traffic: 1978 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6