4

"a" is a data frame.

set.seed(2)
a<-data.frame(group= rep(c("A","B","C"),each=4),factor=rep(c(1,1,2,2),3),
              model=rep(c("old","new"),6),mean=runif(12),sd=runif(12)/10)

>a
           group factor model      mean          sd
        1      A      1   old 0.1848823 0.076051331
        2      A      1   new 0.7023740 0.018082010
        3      A      2   old 0.5733263 0.040528218
        4      A      2   new 0.1680519 0.085354845
        5      B      1   old 0.9438393 0.097639849
        6      B      1   new 0.9434750 0.022582546
        7      B      2   old 0.1291590 0.044480923
        8      B      2   new 0.8334488 0.007497942
        9      C      1   old 0.4680185 0.066189876
        10     C      1   new 0.5499837 0.038754954
        11     C      2   old 0.5526741 0.083688918
        12     C      2   new 0.2388948 0.015050144

I want to draw a "mean±sd" line graph with ggplot2. My aim is to draw a picture (x-axis is "group", y-axis is "mean±sd", different "factor" should have different color, different "model" should have different connecting line type (old model is dashed line, new model is solid line))

I use following code:

library("ggplot2")
pd <- position_dodge(0.1)  #The errorbars overlapped, so use position_dodge to move 
                           #them horizontally 
plot<-ggplot(a, aes(x=group, y=mean, colour=as.factor(factor), linetype=model)) + 
    geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.1, position=pd) +
    geom_point(position=pd, size=3, shape=21, fill="white") + # 21 is filled circle
    xlab("Groups") +
    ylab("Power") +
    geom_line(position=pd) +
    scale_linetype_manual(values = c(new = "solid", old = "dashed"))

enter image description here

but "mean±sd" should all be solid lines, and connection between points should be added. Actually I want it to be like this:

enter image description here Can you give me some advice, thank you!

1 Answer 1

3

I'd suggest adding a new grouping variable:

a$group2 <- paste(a$factor, a$model, sep="_")

Then remove linetype from ggplot() and modify geom_line():

    ggplot(a, aes(x = group, y = mean, colour = as.factor(factor))) + 
      geom_errorbar(aes(ymin = mean-sd, ymax = mean+sd), 
                    width = .1, position = pd) +
      geom_point(position = pd, size = 3, shape = 21, fill = "white") + 
      xlab("Groups") +
      ylab("Power") +
      geom_line(aes(x = group, y = mean, colour = as.factor(factor), 
                    group = group2, linetype = model)) +
      scale_linetype_manual(values = c(new = "solid", old = "dashed"))

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much! It looks nice! But there is one problem: the new model and old model in the same color are overlapped, could you help to handle this, thank you! @beetroot
For example, in Group B, two red "mean ± sd" are overlapped, it's better not to arrange them on the same x-axis, a minor gap is better.
If i set "pd <- position_dodge(1)", then the gragh using your code will become a little strange. @beetroot
OK, I see what you mean now, but unfortunately I can't think of a solution at the moment, sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.