This is a test version of Biostars. For the public version, visit https://www.biostars.org.
how to plot lollipop in which dots show mean/median for each variable?

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

1 answer

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")

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!

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!

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

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 ...

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

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?

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()

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.

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

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

Are you sure you have variability in your data?

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 ...

Log in to answer this question.