Skip to content

Instantly share code, notes, and snippets.

@s-u
Forked from smschauhan/part10.R
Created May 15, 2014 22:43
Show Gist options
  • Save s-u/2f638b6b141fc490a703 to your computer and use it in GitHub Desktop.
Save s-u/2f638b6b141fc490a703 to your computer and use it in GitHub Desktop.
wDCPlot with mtcars
# We don't want a numeric vector. We want a data.frame slice.
head(mtcars["mpg"])
# Did we do it right? What's the type/class?
class(mtcars["mpg"])
# How do I get more than one column?
head(mtcars[c("mpg","hp")])
# How do we know the row names?
row.names(mtcars)
# How do we access a specific value?
mtcars["Mazda RX4", "cyl"]
# OR
mtcars[1,2]
# How do I access a row?
mtcars["Mazda RX4", ]
# Also the same as ...
mtcars[1, ]
# Vehicles with automatic transmission?
head(mtcars[mtcars$am==0,])
# mileage for automatic transmission vehicles
mtcars[mtcars$am==0,]$mpg
# Let's know our data better
summary(mtcars)
# We don't want to keep typing mtcars$... so we use attach
attach(mtcars)
# You can now directly refer mtcars$mpg as mpg
identical(mtcars$mpg, mpg)
# Let's load the popular mtcars dataset. It comes pre-bundled with R
data(mtcars)
# Let's summarize our data
table(cyl)
# Plotting is easy
barplot(table(cyl))
# OR a histogram
hist(mpg)
# OR a boxplot
boxplot(mpg)
# Looks like mpg are pretty low
mean(mpg)
# Mean of mileage for 4 cylinder cars
mean(mpg[cyl==4])
# vs 8 cylinder cars
mean(mpg[cyl==8])
# Is that because of a higher number of cylinders?
plot(cyl, mpg)
# Let's fit a regression line
lm(mpg~cyl+hp)
# Pearson correlation, is that a good fit?
cor(mpg,cyl)^2
cor(mpg, hp)^2
# Let's plot based on number of cylinders
plot(hp,mpg,pch=19, col=cyl)
# add a legend
legend(250, 30, pch=19, col=c(4,6,8), legend=c("4 cylinders","6 cylinders","8 cylinders"))
#Now we do the cool stuff
wdcplot(mtcars, 
dimensions(..index.., mpg, wt, cyl),
groups(mpgGroup = group(mpg, bin(2)),
       wtGroup = group(wt, group = bin(0.5)),
       cylinders = group(cyl)),
charts(SWvL = bubble('Miles Per Gallon vs. Weight',
                     dimension = ..index..,
                     color = cyl,  
                     x = wt,
                     y = mpg,
                     r = ..selected.. * 3,
                     label = NULL),
       cyl = pie("Cylinders",group = cylinders),
       PW = bar('Miles Per Gallon', group = mpgGroup, x.domain = c(10.4,33.9)),
       PL = bar('Weight', group = wtGroup, x.domain = c(1.5,5.5))
))
detach(mtcars)
# What does the data look like?
head(mtcars)
# Looks like mtcars is a data frame
class(mtcars)
# So the structure is ...
str(mtcars)
# What are the total number of rows?
nrow(mtcars)
# How do we access a column?
head(mtcars$mpg)
# What's the type/class?
class(mtcars$mpg)
# OR
head(mtcars[,1])
# OR
head(mtcars[, "mpg"])
# scratch file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment