+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 16

Thread: Plot mean and standard deviation

  1. #1
    Points: 9,891, Level: 66
    Level completed: 61%, Points required for next Level: 159

    Posts
    187
    Thanks
    30
    Thanked 2 Times in 2 Posts

    Plot mean and standard deviation




    How do I create a simple plot showing the mean and standard deviation in R, like that attached?
    Attached Images  

  2. #2
    ggplot2orBust
    Points: 34,783, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,933
    Thanks
    1,347
    Thanked 743 Times in 667 Posts

    Re: Plot mean and standard deviation

    Pretty easy to do, however it's common practice to provide a sample data set for use to work with. You're more likely to get a response if you provide the leg work. When you provide the data sey (you can use dput(dataframe of choice) or just cut and paste the data frame but enclose either one with code tags by either clicking the pound (#) sign icon or wrap with [CODE]some code[/CODE] which produces:
    Code: 
    some code
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  3. #3
    Points: 9,891, Level: 66
    Level completed: 61%, Points required for next Level: 159

    Posts
    187
    Thanks
    30
    Thanked 2 Times in 2 Posts

    Re: Plot mean and standard deviation

    Sorry, don't understand your post. Let's say somebody posted asking how to make a boxplot in R, I would post back saying that they need to enter boxplot(INSERT VARIABLE NAME HERE).

  4. #4
    ggplot2orBust
    Points: 34,783, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,933
    Thanks
    1,347
    Thanked 743 Times in 667 Posts

    Re: Plot mean and standard deviation

    You need a data set to run a function. Provide us with a data set that would make the means and groups you describe. Functions in R generally need some sort of data set supplied to them. What data might cause the output you're showing?

    We do this because your post may be a question other Talk Stats Users may have. If we do it the way you suggest I'll likely be explaining this to someone again next week because they couldn't follow along or reproduce what you were asking. However, if they have a reproducible example to follow along they are much more likely to learn from your question and not need to ask the same question their self.

    Something like:
    Code: 
    set.seed(10)
    DF<-data.frame(AEA=rnorm(abs(10)), variant.group=sample(c('control', 'cases.NC', 'cases.C'), 10, replace=T))
    or

    Code: 
               AEA variant.group
    1   0.01874617       cases.C
    2  -0.18425254      cases.NC
    3  -1.37133055       cases.C
    4  -0.59916772      cases.NC
    5   0.29454513      cases.NC
    6   0.38979430       cases.C
    7  -1.20807618       cases.C
    8  -0.36367602       control
    9  -1.62667268       cases.C
    10 -0.25647839      cases.NC

    or

    Code: 
    > dput(DF)
    structure(list(AEA = c(0.0187461709418264, -0.184252542069064, 
    -1.37133054992251, -0.599167715783718, 0.294545126567508, 0.389794300700167, 
    -1.20807617542949, -0.363676017470862, -1.62667268170309, -0.256478394123992
    ), variant.group = structure(c(1L, 2L, 1L, 2L, 2L, 1L, 1L, 3L, 
    1L, 2L), .Label = c("cases.C", "cases.NC", "control"), class = "factor")), .Names = c("AEA", 
    "variant.group"), row.names = c(NA, -10L), class = "data.frame")
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  5. #5
    Points: 9,891, Level: 66
    Level completed: 61%, Points required for next Level: 159

    Posts
    187
    Thanks
    30
    Thanked 2 Times in 2 Posts

    Re: Plot mean and standard deviation

    I don't actually have a dataset, I just want to know how I would it. For example, in the dataset 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 the mean is 5.5 and SD is 3.03. How would I plot the mean and SD in graphical form? Is not simply a case of saying something like 'enter plot.meansd()'?

  6. #6
    Beep
    Points: 60,927, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,037
    Thanks
    260
    Thanked 2,134 Times in 1,815 Posts

    Re: Plot mean and standard deviation

    Well it's a pretty easy plot to make using base plotting functions like points, lines, ..., all that good stuff. But in the library 'gplots' there is a function called plotCI which plots points and an interval around them. You could specify the width of the interval to be the standard deviation instead of the standard error so that could probably work for you.

  7. #7
    ggplot2orBust
    Points: 34,783, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,933
    Thanks
    1,347
    Thanked 743 Times in 667 Posts

    Re: Plot mean and standard deviation

    Layo909 often you need to provide a reproducible example but don't have a data set or are working with protected data and can't provide a data set. rnorm and sample are your friends for creating a fictitious data set. The problem with not providing a data set is dason gave you an idea but we don't know if his idea works (It's dason so I'm sure it does ). If you provided a data set then he'd probably have also used your data set to generate code to produce exactly what you want. That way you and future searchers know exactly how to reproduce the example. does that make sense?

    I actually keep a data frame generator right in my work space just for such situations and ask for exactly how many observations I want and take what variables I need:
    THE FUNCTION
    Code: 
    DFgen <- function(n = 10, type = wide) {
        TYPE <- as.character(substitute(type))
        time1 <- sample(1:100, n, replace = TRUE) + abs(rnorm(n))
        
        DF <- data.frame(id = paste("ID", 1:n, sep = ""), hs.grad = sample(c("yes", "no"), n, replace = TRUE), race = sample(c("black", 
            "white", "asian"), n, replace = TRUE, prob=c(.25, .5, .25)), gender = sample(c("male", "female"), n, replace = TRUE), age = sample(18:40, 
            n, replace = TRUE), score = rnorm(n), time1, time2 = c(time1 + 2 * abs(rnorm(n))), time3 = c(time1 + (4 * 
            abs(rnorm(n)))))
    
        switch(TYPE, 
            wide = DF, 
            long = {require(reshape)
                   melt(DF, id = c(names(DF)[which(!names(DF) %in% c("time1", "time2", "time3"))]))}, 
            stop("Invalid Data \"type\""))
    }
    TRY IT OUT
    Code: 
    #DFgen with no arguments will give you:
    DFgen()
    
    > DFgen()
         id hs.grad  race gender age      score    time1    time2    time3
    1   ID1     yes black   male  35 -0.5541989 45.31701 49.98212 49.98195
    2   ID2      no white   male  29  0.0384142 74.41086 74.42238 78.99173
    3   ID3     yes asian   male  40 -0.5198690 63.08642 64.14907 63.25083
    4   ID4     yes white   male  31 -1.5452401 74.18115 74.78235 77.94657
    5   ID5      no black   male  39  0.9053654 58.26851 62.05737 59.96906
    6   ID6      no white   male  30  1.1836036 76.14062 76.43534 77.76279
    7   ID7      no white female  26 -0.7695334 59.36268 59.47636 60.77766
    8   ID8      no asian   male  36  0.1909032 73.27114 74.36726 73.30970
    9   ID9      no black female  25 -0.2097491 65.21114 66.31425 70.21061
    10 ID10     yes white   male  38 -1.0693030 58.71412 58.76808 60.10323
    PLAY WITH IT A LITTLE AND USE DPUT FOR POSTING TO FORUMS
    Code: 
    #Let's say I just want some columns then I use:
    (sample.data <- DFgen(n = 10)[,c(2,3,4,5,6)])
    
    #OR I want it in long format
    (sample.data <- DFgen(n = 10, type=long))
    
    #Any of these are easily given in threads with dput()
    dput(sample.data)
    I hope this is helpful!
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  8. The Following User Says Thank You to trinker For This Useful Post:

    Dason (12-23-2011)

  9. #8
    Ninja say what!?!
    Points: 8,297, Level: 61
    Level completed: 49%, Points required for next Level: 153
    Link's Avatar
    Posts
    1,165
    Thanks
    37
    Thanked 84 Times in 76 Posts

    Re: Plot mean and standard deviation

    Trinker is so helpful! I love how kind he is!

    PS. I should take this time to thank Dason as well. This forum wouldn't be what it is without him!

  10. #9
    Super Moderator
    Points: 19,174, Level: 87
    Level completed: 65%, Points required for next Level: 176
    bugman's Avatar
    Posts
    1,824
    Thanks
    184
    Thanked 221 Times in 175 Posts

    Re: Plot mean and standard deviation

    There is also the errbar function in the package Hmisc. This is the method I prefer, but have been using the gplots method a bit lately.
    The earth is round: P<0.05

  11. #10
    Points: 9,891, Level: 66
    Level completed: 61%, Points required for next Level: 159

    Posts
    187
    Thanks
    30
    Thanked 2 Times in 2 Posts

    Re: Plot mean and standard deviation

    Attached is a dataset. Could someone tell me in the absolute simplest possible manner how I make a plot the same as in post #1.

    Please give step by step instructions as if you were talking to a 4 year old - I am an absolute beginner in R and don't understand anything posted in the thread so far.

    For example, if someone asked how to calculate the mean of the attached dataset in excel, I would respond by saying (a) place cursor in any empty cell in the spreadsheet (b) type the following '=AVERAGE(A2:A244)' (c) hit enter key to reveal the mean.
    Attached Files

  12. #11
    ggplot2orBust
    Points: 34,783, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,933
    Thanks
    1,347
    Thanked 743 Times in 667 Posts

    Re: Plot mean and standard deviation

    With the data you've given what you want is not possible because you don't have a grouping variable (well i guess you could do it for the data but it's a waste to use a fancy graph to display a mean and sd). I'll figure it out for the data set I provided (I'd advise against attaching an excel file and would encourage you to use the methods I described above instead).
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  13. #12
    Beep
    Points: 60,927, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,037
    Thanks
    260
    Thanked 2,134 Times in 1,815 Posts

    Re: Plot mean and standard deviation

    It sounds like what you really need to do is sit down and learn a little bit about R. Having us literally make that graph for you isn't going to help you learn R - learning how to make that graph (with helpful suggestions from us) will help you learn. There is quite a bit of material that can help you learn R and a lot of suggestions have been made in this thread: http://www.talkstats.com/showthread....uals-Books-etc.)

  14. #13
    ggplot2orBust
    Points: 34,783, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,933
    Thanks
    1,347
    Thanked 743 Times in 667 Posts

    Re: Plot mean and standard deviation

    It sounds to me like you're pretty new to R (glad to hear of new R users). Dason provided the easiest to work with answer. The CRAN manuals for functions are very helpful in working through problems and you access them pretty easily by typing ?function.name into the command line. So I did ?plotCI and got a help page for this function. The best part of these help pages are the examples that you can either cut and paste (sometimes this is the only way to use the examples) or by typing example(plotCI) into the command line. I actually prefer cutting and pasting the example so I can see what each line of the code is doing. I included code for how to do what you want with some aesthetics added to the plotCI function to match what you want.



    Code: 
    #######################################
    # HERE'S A FAKE DATA SET TO WORK WITH #
    #######################################
    set.seed(10)
    DF<-data.frame(AEA=sample(10:40, 100, T), variant.group=sample(c('control', 
                   'cases.NC', 'cases.C'), 100, replace=T))
    DF$AEA <- with(DF, ifelse(variant.group=='cases.C', AEA + 10, 
                    ifelse(variant.group=='cases.NC', AEA + 20, AEA)))
    DF
    
    #######################################################
    # HERE'S A USER DEFINES FUNTIONBEING APPLIED TO       #
    # YOUR DATA BY GROUP USING TAPPLY.  THE USER DEFINED  #
    # FUNCTION COULD BE WHAT EVER YOU WANT IT TO BE.      #
    #######################################################
    meansd <- function(x) c(mean=mean(x), sd=sd(x), min.3sd=mean(x)-2*sd(x), plus.2sd=mean(x) + 2*sd(x))
    with(DF, tapply(AEA, list(variant.group), meansd))
    
    #############################################################
    # YOU NEED THESE TWO LIBRARY (YOU MAY HAVE TO INSTALL THEM) #
    #############################################################
    library(gplots)
    library(caTools)
    #if you need to install them uncomment and run the following lines
    #install.packages("gplots")
    #install.packages("caTools")
    
    #######################################################################
    # I USE ATTACH JUST FOR EASE.  USUALLY YOU SHOULD USE with() INSTEAD. #
    #######################################################################
    attach(DF)
    
    ########################################################
    # USING SAPPLY TO GET SOME DESCRIPTIVESFOR PLOTTING.   #
    # WE DID THIS WITH TAPPLY ALREAD.                      #
    ########################################################
    tmp <- split(AEA, variant.group)
    means <- sapply(tmp, mean)
    stdev <- sapply(tmp, sd)
    n <- sapply(tmp,length)
    ciw <- qt(0.975, n) * stdev / sqrt(n)
    
    ###############################################################################
    # THE MAIN PLOT.  lwd CONTROLS LINE SIZE.  pch CONTROLS THE SYMBOL SHAPE.     #
    # cex IS THE SIZE OF THE SYMBOL.  xaxt AND yaxt PREVENT THE DEFAULT AXES FROM #
    # BEING PLOTTED FOR BETTER CONTROL LATER.  xlim CONTROLS WHERE THE MEANS      #
    # WILL BE PLOTTED.  THE LABELS SHOULD BE SELF EXPLANATORY.                    #
    ###############################################################################
    plotCI(x=means, uiw=stdev, col="black", barcol="black", lwd=1, pch=18,
    cex=2, xaxt="n", yaxt="n", xlim=c(.5,3.5), ylim=c(0, 60), ylab='AEA(pmlo/mL)',
    xlab='FAAH Rare Varient Group', yaxs = 'i')
    
    #################################
    # PLOTS THE NAMES OF THE GROUPS #
    #################################
    axis(side=1, at=1:3, labels=names(tmp), cex=0.7)
    
    ####################################
    # BETTER CONTROL OF THE SCALE USED #
    ####################################
    axis(side=2, at=c(seq(0, 60, by=5)), )
    
    ##########################################################
    # PLOTS THE CUTE DOTTED GRAY LINES.  lty MAKES IT DOTTED #
    ##########################################################
    abline(h = c(seq(0, 60, by=5)), col = "lightgray", lty=3)
    
    #############################################################
    # THIS DOES A QUICKER JOB AND MAY BE WHAT YOU ACTUALLY WANT #
    #############################################################
    plotmeans( AEA ~ variant.group )
    The ?function.name is very helpful though and I suggest you use this often with the examples that are provided.

    EDIT: Dason gives very good advice here. His advice is teaching you to fish rather than giving you fish. I suggest you check out the link he provided.
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  15. The Following User Says Thank You to trinker For This Useful Post:

    shazjazz (01-15-2013)

  16. #14
    Points: 9,891, Level: 66
    Level completed: 61%, Points required for next Level: 159

    Posts
    187
    Thanks
    30
    Thanked 2 Times in 2 Posts

    Re: Plot mean and standard deviation

    Post #13 looks very clear. Many thanks, looking forward to giving it a whirl.

  17. #15
    Beep
    Points: 60,927, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,037
    Thanks
    260
    Thanked 2,134 Times in 1,815 Posts

    Re: Plot mean and standard deviation


    Quote Originally Posted by Layo909 View Post
    Post #13 looks very clear. Many thanks, looking forward to giving it a whirl.
    Let us know if you need any more help. Until then I'm marking this as solved.

+ Reply to Thread
Page 1 of 2 1 2 LastLast

           




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats