Plotting means and error bars (GGPLOT2)

Source: Internet
Author: User
Tags ggplot

Library (GGPLOT2)

############################################## summaryse############################################### Summarizes data.## Gives count, mean, standard deviation, standard error of the mean, and confidence interval (default 95% ). # # DATA:A Data frame.## measurevar:the name of a column that contains the variable to be summariezed## groupvars : A vector containing names of columns that contain grouping variables## Na.rm:a Boolean that indicates whether to Igno Re NA ' s## conf.interval:the percent range of the confidence interval (default is 95%) Summaryse <-function (data=null  , Measurevar, Groupvars=null, Na.rm=false, conf.interval=.95,. drop=true) {library (PLYR) # New        Version of length which can handle NA ' S:if na.rm==t, don ' T count them length2 <-function (x, na.rm=false) { if (na.rm) sum (!is.na (x)) Else length (x)} # This does the summary.    For each group's data frame, return a vector with # N, mean, and SDDatac <-ddply (data, Groupvars,. Drop=.drop,. Fun = function (xx, col) {C (N = Length2 (Xx[[col]], na.rm=n      A.RM), mean = mean (Xx[[col], na.rm=na.rm), SD = SD (Xx[[col]], na.rm=na.rm)}, Measurevar) # Rename The "mean" column Datac <-Rename (Datac, C ("mean" = Measurevar)) Datac$se &lt     ;-Datac$sd/sqrt (datac$n) # Calculate standard error in the mean # Confidence interval multiplier for standard error # Calculate T-statistic for confidence interval: # e.g., if conf.interval are.. 975 (Above/below), and use Df=n-1 Cimult <-qt (CONF.INTERVAL/2 +. 5, datac$n-1) datac$ci <-datac$se * cimult return (DATAC)}########## #################################### Sample data############################################ #library (ggplot2) TG <-Toothgrowthhead (TG) TGC <-Summaryse (TG, measurevar= "Len", Groupvars=c ("supp", "dose")) tgc################## ############################ Line graphs############################################## standard error of the Meanggplot (TGC, AES (X=dose, Y=len, Colour=supp)) + Geom_errorbar (Aes (Ymin=len-se, Ymax=len+se), width=.1) + geom_line () + Geom_point () # The ErrorBars overlapped, so u Se position_dodge to move them horizontallypd <-Position_dodge (0.1) # move them.-The left and Rightggplot (TGC, a Es (X=dose, Y=len, Colour=supp) + Geom_errorbar (Aes (Ymin=len-se, Ymax=len+se), width=.1, POSITION=PD) + Geom_line (p OSITION=PD) + geom_point (POSITION=PD) # Use 95% confidence interval instead of Semggplot (TGC, AES (X=dose, Y=len, colour= Supp) + Geom_errorbar (Aes (YMIN=LEN-CI, Ymax=len+ci), width=.1, POSITION=PD) + geom_line (POSITION=PD) + Geom_poi NT (POSITION=PD) # Black error Bars-notice the mapping of ' Group=supp '--without it, the error# bars won ' t be DODGED!GGPL OT (TGC, AES (X=dose, Y=len, Colour=supp, Group=supp)) + Geom_errorbar (Aes (YMIN=LEN-CI, YMAX=LEN+CI), colour= "Black", WI  Dth=.1, POSITION=PD) +  Geom_line (POSITION=PD) + geom_point (POSITION=PD, size=3) # A finished graph with error bars representing Error of the mean might # look like this. The points is drawn last so then the white fill goes on top of# the lines and error Bars.ggplot (TGC, AES (X=dose, Y=len, C Olour=supp, Group=supp) + Geom_errorbar (Aes (Ymin=len-se, Ymax=len+se), colour= "Black", width=.1, POSITION=PD) + ge Om_line (POSITION=PD) + geom_point (POSITION=PD, size=3, shape=21, fill= "white") + # are filled circle Xlab ("Dose (M                     g) + Ylab ("tooth length") + Scale_colour_hue (name= "Supplement type", # Legend label, use darker colors  Breaks=c ("OJ", "VC"), Labels=c ("Orange Juice", "ascorbic Acid"), l=40) + # Use darker colors, lightness=40 ggtitle ("The Effect of Vitamin C on\ntooth growth in Guinea Pig S ") + expand_limits (y=0) + # expand Y range scale_y_continuous (breaks=0:20*4) + # Set tick every 4 THEME_BW () + Theme (Legend.justification=c (1,0), Legend.position=c (1,0)) # Position legend in bottom right############################################## Bar graphs#####################  ######################## # Use dose as a factor rather than NUMERICTGC2 <-tgctgc2$dose <-Factor (tgc2$dose) # Error Bars represent standard error of the Meanggplot (TGC2, AES (X=dose, Y=len, Fill=supp)) + Geom_bar (position=position_dod GE (), stat= "identity") + Geom_errorbar (Aes (Ymin=len-se, Ymax=len+se), width=.2, # W Idth of the error bars Position=position_dodge (. 9)) # Use 95% confidence intervals instead of Semggplot (TG C2, AES (X=dose, Y=len, Fill=supp)) + Geom_bar (Position=position_dodge (), stat= "identity") + Geom_errorbar (Aes (ymin= Len-ci, Ymax=len+ci), width=.2, # Width of the error bars Position=po Sition_dodge (. 9)) # #A finished graph might look like This.ggplot (TGC2, AES (X=dose, Y=len, Fill=supp)) + Geom_bar (Position=position_dodge () , stat= "Identity", colour= "Black", # Use black outlines, size=.3) + # thinner lines Geom_e                  Rrorbar (Aes (Ymin=len-se, Ymax=len+se), size=.3, # thinner lines width=.2, Position=position_dodge (. 9)) + Xlab ("Dose (mg)") + Ylab ("tooth length") + Scale_fill_hue (name= "Supplement t  Ype ", # Legend label, use darker colors breaks=c (" OJ "," VC "), Labels=c (" Orange Juice ", "Ascorbic acid") + Ggtitle ("The Effect of Vitamin C on\ntooth growth in Guinea Pigs") + scale_y_continuous (breaks= 0:20*4) + THEME_BW ()

REF:

http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_%28ggplot2%29/

Http://www.rdocumentation.org/packages/bear/functions/summarySE

http://www.cookbook-r.com/Manipulating_data/Summarizing_data/

Http://www.inside-r.org/packages/cran/rmisc/docs/summarySE

Plotting means and error bars (GGPLOT2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.