how to plot lollipop in which dots show mean/median for each variable?
1
0
Entering edit mode
4.2 years ago
Raheleh ▴ 260

I am trying to make a lollipop plot that the dots show median/mean for each variable and the stick lines out of the dots show variance? The code that I wrote is this:

mydata <- df
Avg = rowMeans(df)
SD = apply(df, 1, sd)
Above = ifelse(SD - Avg, TRUE, FALSE)
df$NAME <- rownames(df)

ggplot(df, aes(Avg, NAME, color = Above)) +
  geom_segment(aes(x = SD, y = NAME, xend = Avg, yend = NAME), color = "grey50") +
  geom_point()

and this is the output:

enter image description here

but I want sth like this image:

enter image description here

Can anyone help me?

Thanks!

lollipop plot ggplot2 • 2.2k views
ADD COMMENT
1
Entering edit mode
4.2 years ago
Asaf 10k

You can use stat_summary to plot the means and the sd. Something like:

ggplot(df, aes(grp, value, group=grp) + stat_summary(fun.data='mean', geom='point') + stat_summary(fun.data = "mean_se", geom = "errorbar")
ADD COMMENT
0
Entering edit mode

Thank you Asaf! could you please tell me what is mean_se in your script? Can you put it in a simple example? I used melt function to put all my variable (15 genes) in one column and all value in the second column. Then I ran this script:

ggplot(m_df_cl2, aes(variable, value, group=variable)) + stat_summary(function='mean', geom='point') + stat_summary(fun.data = mean_se, geom = "errorbar")

and I got this error:

Error: unexpected '=' in "ggplot(m_df_cl2, aes(variable, value, group=variable)) + stat_summary(function="

Can you help me out? Many thanks!

ADD REPLY
1
Entering edit mode

Sorry, forgot to parenthesize mean_se. Edited. Take a look at stat_summary help:https://ggplot2.tidyverse.org/reference/stat_summary.html

ADD REPLY
0
Entering edit mode

Many thanks Asaf, but still I am getting the same error. I thought maybe it is because of function in the script I change it to fun

ggplot(m_df_cl2, aes(value, variable, group=variable)) + stat_summary(fun = 'mean', geom='point') + stat_summary(fun.data = mean_se(), geom = "errorbar")

now I am getting this error:

Error in stats::na.omit(x) : argument "x" is missing, with no default

any idea? Thanks!

ADD REPLY
0
Entering edit mode

Sorry, I meant quotes not parentheses, also the function was wrong, changed to fun.data (edited the answer again)

ADD REPLY
0
Entering edit mode

Many thanks Asaf! I changed the script as you said. Now I am getting this error :-(

Error: geom_point requires the following missing aesthetics: y

This is the structure of my data; do you think there is sth wrong about my data?

str(m_df_cl2)
'data.frame':   1042110 obs. of  2 variables:
 $ variable: Factor w/ 15 levels "pS6","S6","BetaCatenin",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value   : num  -1.18 -1.18 -1.18 -1.18 -1.18 ...
ADD REPLY
0
Entering edit mode

try changing to fun.y instead of fun.data in the mean part

ADD REPLY
0
Entering edit mode

Many thanks Asaf for your time and help. with this script:

ggplot(m_df_cl2, aes(value, variable, group=variable)) + stat_summary(fun.y = 'mean', geom='point') + stat_summary(fun.data = "mean_se", geom = "errorbar")

Finally I got this image: enter image description here

But it is not the same as I wanted. how to make the lines horizontal?

ADD REPLY
0
Entering edit mode
ggplot(m_df_cl2, aes(variable, value, group=variable)) + stat_summary(fun.y = 'mean', geom='point') + stat_summary(fun.data = "mean_se", geom = "errorbar") + coord_flip()
ADD REPLY
0
Entering edit mode

Thank you very much Asaf for keep helping me but this time I got this:

ggplot(m_df_cl2, aes(value, variable, group=variable)) + stat_summary(fun.y = 'mean', geom='point') + stat_summary(fun.data = "mean_se", geom = "errorbar")+ coord_flip()

enter image description here

I want variable to be in y axis. coord_flip() is changing the amount of x with y and vice versa.

ADD REPLY
0
Entering edit mode

X should be variable, Y should be value and then use coor_flip to have it opposite.

ADD REPLY
0
Entering edit mode

Yes I already did, but I got the same image as the previous one. The stick lines are vertical not horizontal :-(

ADD REPLY
0
Entering edit mode

Are you sure you have variability in your data?

ADD REPLY
0
Entering edit mode

What do you mean? this is the structure of my data:

str(m_df_cl2)
'data.frame':   1042110 obs. of  2 variables:
 $ variable: Factor w/ 15 levels "pS6","S6","BetaCatenin",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value   : num  -1.18 -1.18 -1.18 -1.18 -1.18 ...
ADD REPLY

Login before adding your answer.

Traffic: 2372 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