Skip to content

DECISION STATS

Better Decisions === Faster Stats

  • Interviews
  • About DecisionStats
    • Decisionstats.org
  • Interviews
  • About DecisionStats
DECISION STATS

Tag: Plot (graphics)

Quick-R and Statmethods.net

Syntax
Image via Wikipedia

I was searching for some basic syntax in R (basically cross tabs and density plots) and I came across the Quick R site.

http://www.statmethods.net/

Its really a nice site for R beginners and anyone trying to remember some syntax.

R syntax can be very simple- a histoigram is just hist(), boxplot is just boxplot() and t test is just t.test(dataset)

Here is an example from the site-

http://www.statmethods.net/graphs/density.html


# Simple Histogram
hist(mtcars$mpg)

simple histogram click to view

# Colored Histogram with Different Number of Bins
hist(mtcars$mpg, breaks=12, col="red")

colored histogram click to view

# Add a Normal Curve (Thanks to Peter Dalgaard)
x <- mtcars$mpg
h<-hist(x, breaks=10, col="red", xlab="Miles Per Gallon",
main="Histogram with Normal Curve")
xfit<-seq(min(x),max(x),length=40)
yfit<-dnorm(xfit,mean=mean(x),sd=sd(x))
yfit <- yfit*diff(h$mids[1:2])*length(x)
lines(xfit, yfit, col="blue", lwd=2)

histogram with normal curve click to view

Histograms can be a poor method for determining the shape of a distribution because it is so strongly affected by the number of bins used.

KERNEL DENSITY PLOTS

Kernal density plots are usually a much more effective way to view the distribution of a variable. Create the plot using plot(density(x)) where x is a numeric vector.

# Kernel Density Plot
d <- density(mtcars$mpg) # returns the density data
plot(d) # plots the results

simple density plot click to view

# Filled Density Plot
d <- density(mtcars$mpg)
plot(d, main="Kernel Density of Miles Per Gallon")
polygon(d, col="red", border="blue")

colored density plot click to view

COMPARING GROUPS VIA KERNAL DENSITY

The sm.density.compare( ) function in the sm package allows you to superimpose the kernal density plots of two or more groups. The format is sm.density.compare(x, factor) where x is a numeric vector and factor is the grouping variable.

# Compare MPG distributions for cars with
# 4,6, or 8 cylinders
library(sm)
attach(mtcars)

# create value labels
cyl.f <- factor(cyl, levels= c(4,6,8),
labels = c("4 cylinder", "6 cylinder", "8 cylinder"))

# plot densities
sm.density.compare(mpg, cyl, xlab="Miles Per Gallon")
title(main="MPG Distribution by Car Cylinders")

# add legend via mouse click
colfill<-c(2:(2+length(levels(cyl.f))))
legend(locator(1), levels(cyl.f), fill=colfill)

comparing densities click to view

 

It is not as exhaustive as http://cran.r-project.org/doc/manuals/R-intro.html

but it is much more simpler and easy to follow.

The site is created by Robert I. Kabacoff, Ph.D.

and he is working on a book called “R in Action”

R in Action

I have received numerous requests for a hardcopy version of this site, so over the past year I have been writing a book that takes the material here and significantly expands upon it. If you are interested, early access is available.

 

If you have not been to that website, I recommend it highly (though the tagline or logo of R for SAS/SPSS/Stata users seems a bit familiar)-http://www.statmethods.net/index.html

Quick-R

for SAS/SPSS/Stata Users


Related Articles
  • Two Thoughts on Lisp Syntax. (kazimirmajorinc.blogspot.com)
  • Some Basics about Stats (psipsychologytutor.org)
  • Bone Density Tests: A Clue to Your Future (webmd.com)
  • Net Access Corporation Unveils 50,000 Square Foot, State-of-the-Art Data Center in Parsippany, New Jersey (prweb.com)
  • programming languages – What makes lisp macros so special – Stack Overflow (stackoverflow.com)
  • Thinking about Syntax (latenightpc.com)
  • Our minds use syntax to understand actions, just like with language [Mad Psychology] (io9.com)
  • Syntax highlighting for Django using Pygments (ofbrooklyn.com)
  • People of HTML5 – Bruce Lawson (hacks.mozilla.org)
  • Haskell syntax vs. Lisp syntax | LispCast (lispcast.com)
28.635308 77.224960

Please share:

  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X
Like Loading...
Unknown's avatarAuthor Ajay OhriPosted on January 10, 2011Categories AnalyticsTags Bar chart, book, compare, data, Distributions, easy, groups, Histogram, html, html5, Language, Languages, logo, normal, Normal distribution, Plot (graphics), Promotion, R, rstats, Searching, stack overflow, stats, syntax, use, version, Web Design and Development, writingLeave a comment on Quick-R and Statmethods.net

Blog Stats

  • 1,339,958 hits

BitCoin Address

BitCoin Wallet

12FxL4og4MqtZt6pRZ1LkcMQq6ddgJK4Yg

Latest Book

SAS for R Users

Books by Ajay Ohri

Python for R USers

R for Business Analytics


R for Cloud Computing

On FB

On FB

Archives

  • February 2025 (1)
  • July 2024 (1)
  • January 2024 (1)
  • August 2023 (1)
  • June 2023 (8)
  • April 2023 (1)
  • March 2022 (1)
  • August 2021 (1)
  • June 2021 (1)
  • March 2021 (1)
  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (1)
  • July 2020 (1)
  • June 2020 (1)
  • May 2020 (1)
  • March 2020 (1)
  • February 2020 (2)
  • January 2020 (8)
  • December 2019 (1)
  • November 2019 (2)
  • October 2019 (1)
  • May 2019 (2)
  • March 2019 (3)
  • February 2019 (1)
  • January 2019 (1)
  • December 2018 (3)
  • October 2018 (1)
  • September 2018 (7)
  • August 2018 (3)
  • July 2018 (6)
  • June 2018 (2)
  • May 2018 (1)
  • April 2018 (2)
  • March 2018 (7)
  • February 2018 (16)
  • January 2018 (4)
  • December 2017 (10)
  • November 2017 (8)
  • October 2017 (6)
  • September 2017 (4)
  • August 2017 (6)
  • July 2017 (6)
  • June 2017 (3)
  • May 2017 (4)
  • March 2017 (1)
  • February 2017 (2)
  • January 2017 (2)
  • December 2016 (3)
  • November 2016 (7)
  • September 2016 (7)
  • August 2016 (12)
  • July 2016 (12)
  • June 2016 (8)
  • May 2016 (11)
  • April 2016 (5)
  • March 2016 (22)
  • February 2016 (6)
  • January 2016 (4)
  • December 2015 (26)
  • November 2015 (19)
  • October 2015 (44)
  • September 2015 (18)
  • August 2015 (28)
  • July 2015 (12)
  • June 2015 (6)
  • May 2015 (9)
  • April 2015 (9)
  • March 2015 (11)
  • February 2015 (5)
  • January 2015 (8)
  • December 2014 (9)
  • November 2014 (10)
  • October 2014 (3)
  • September 2014 (5)
  • August 2014 (5)
  • July 2014 (14)
  • June 2014 (21)
  • May 2014 (10)
  • April 2014 (14)
  • March 2014 (15)
  • February 2014 (11)
  • January 2014 (4)
  • December 2013 (16)
  • November 2013 (19)
  • October 2013 (23)
  • September 2013 (12)
  • August 2013 (14)
  • July 2013 (9)
  • June 2013 (8)
  • May 2013 (15)
  • April 2013 (16)
  • March 2013 (8)
  • February 2013 (16)
  • January 2013 (18)
  • December 2012 (7)
  • November 2012 (8)
  • October 2012 (10)
  • September 2012 (1)
  • August 2012 (26)
  • July 2012 (31)
  • June 2012 (12)
  • May 2012 (10)
  • April 2012 (12)
  • March 2012 (27)
  • February 2012 (31)
  • January 2012 (26)
  • December 2011 (27)
  • November 2011 (40)
  • October 2011 (32)
  • September 2011 (29)
  • August 2011 (30)
  • July 2011 (30)
  • June 2011 (33)
  • May 2011 (50)
  • April 2011 (51)
  • March 2011 (31)
  • February 2011 (38)
  • January 2011 (36)
  • December 2010 (35)
  • November 2010 (77)
  • October 2010 (73)
  • September 2010 (65)
  • August 2010 (19)
  • July 2010 (14)
  • June 2010 (11)
  • May 2010 (20)
  • April 2010 (18)
  • March 2010 (8)
  • February 2010 (10)
  • January 2010 (42)
  • December 2009 (38)
  • November 2009 (60)
  • October 2009 (41)
  • September 2009 (45)
  • August 2009 (37)
  • July 2009 (21)
  • June 2009 (28)
  • May 2009 (49)
  • April 2009 (29)
  • March 2009 (39)
  • February 2009 (34)
  • January 2009 (42)
  • December 2008 (18)
  • November 2008 (37)
  • October 2008 (20)
  • September 2008 (52)
  • August 2008 (62)
  • July 2008 (42)
  • June 2008 (16)
  • May 2008 (10)
  • April 2008 (20)
  • March 2008 (15)
  • February 2008 (30)
  • January 2008 (20)
  • December 2007 (13)

This slideshow requires JavaScript.

  • Interviews
  • About DecisionStats
    • Decisionstats.org
  • Interviews
  • About DecisionStats
DECISION STATS Blog at WordPress.com.
  • Subscribe Subscribed
    • DECISION STATS
    • Join 440 other subscribers
    • Already have a WordPress.com account? Log in now.
    • DECISION STATS
    • Subscribe Subscribed
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar
 

Loading Comments...
 

    %d