Question: how to plot lollipop in which dots show mean/median for each variable?
0
gravatar for Rahil
8 months ago by
Rahil170
Rahil170 wrote:

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 • 308 views
ADD COMMENTlink modified 8 months ago by Asaf8.4k • written 8 months ago by Rahil170
1
gravatar for Asaf
8 months ago by
Asaf8.4k
Israel
Asaf8.4k wrote:

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 COMMENTlink modified 8 months ago • written 8 months ago by Asaf8.4k

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 REPLYlink modified 8 months ago • written 8 months ago by Rahil170
1

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

ADD REPLYlink written 8 months ago by Asaf8.4k

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 REPLYlink modified 8 months ago • written 8 months ago by Rahil170

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

ADD REPLYlink written 8 months ago by Asaf8.4k

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 REPLYlink modified 8 months ago • written 8 months ago by Rahil170

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

ADD REPLYlink written 8 months ago by Asaf8.4k

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 REPLYlink written 8 months ago by Rahil170
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 REPLYlink written 8 months ago by Asaf8.4k

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 REPLYlink written 8 months ago by Rahil170

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

ADD REPLYlink written 8 months ago by Asaf8.4k

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

ADD REPLYlink written 8 months ago by Rahil170

Are you sure you have variability in your data?

ADD REPLYlink modified 8 months ago • written 8 months ago by Asaf8.4k

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 REPLYlink written 8 months ago by Rahil170
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1658 users visited in the last hour