Like this presentation? Why not share!

# 05 Tips Tricks

## by Hadley Wickham, Working at Rice Unversity on Sep 09, 2009

• 1,613 views

### Views

Total Views
1,613
Views on SlideShare
1,610
Embed Views
3

Likes
1
45
0

### 1 Embed3

 http://www.slideshare.net 3

## 05 Tips TricksPresentation Transcript

• Stat405 Graphic tips & tricks Hadley Wickham Wednesday, 9 September 2009
• 1. Homework 2. Reading a scatterplot 3. Scatterplot techniques for large data 4. Iteration & story telling 5. Project & homework Wednesday, 9 September 2009
• Homework Great start! Remember the grading scheme: 4.5–5 = A+, 4–4.5 = A, 3.5–4 = A- Shorter is better than longer. Check aspect ratios. Read the comments! Wednesday, 9 September 2009
• Revision: reading a scatterplot • Big patterns • Small patterns • Deviations from the pattern • Strange patterns Wednesday, 9 September 2009
• Wednesday, 9 September 2009
• Strong linear relationship. A number of outliers. Wednesday, 9 September 2009
• Wednesday, 9 September 2009
• Unusual striations. Two groups? Little relationship between table and price? Wednesday, 9 September 2009
• Wednesday, 9 September 2009
• Curved (exponential?) relationship. Outliers mostly cheaper than expected. Wednesday, 9 September 2009
• But what’s the problem with all these plots? qplot(carat, price, data = diamonds) Wednesday, 9 September 2009
• But what’s the problem with all these plots? In pairs, brainstorm solutions for 2 minutes. qplot(carat, price, data = diamonds) Wednesday, 9 September 2009
• Ideas If x discrete, use boxplots. Use semi-transparent points. Divide into bins and count number of points in each bin (2d histogram). Display statistical summary. Wednesday, 9 September 2009
• Box and whisker plots Wednesday, 9 September 2009
• Boxplots Less information than a histogram, but take up much less space. Already seen them used with discrete x values. Can also use with continuous x values, by specifying how we want the data grouped. Wednesday, 9 September 2009
• qplot(table, price, data = diamonds) Wednesday, 9 September 2009
• ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 15000 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 10000 price 5000 50 60 70 80 90 qplot(table, price, data = diamonds, geom = "boxplot") table Wednesday, 9 September 2009
• ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 15000 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 10000 ● ● ● ● ● ● ● ● ● ● price ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 5000 qplot(table, price, data = diamonds, geom 80 "boxplot", 50 60 70 = 90 group = round(table)) table Wednesday, 9 September 2009
• ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 15000 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 10000 ● ● ● ● ● ● ● ● ● ● price ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 5000 One boxplot for each unique value of this aesthetic qplot(table, price, data = diamonds, geom 80 "boxplot", 50 60 70 = 90 group = round(table)) table Wednesday, 9 September 2009
• Alpha blending Wednesday, 9 September 2009
• qplot(carat, price, data = diamonds, alpha = I(1/10)) Wednesday, 9 September 2009
• qplot(carat, price, data = diamonds, alpha = I(1/50)) Wednesday, 9 September 2009
• qplot(carat, price, data = diamonds, alpha = I(1/250)) Wednesday, 9 September 2009
• Statistical summary Wednesday, 9 September 2009
• qplot(carat, price, data = diamonds) + geom_smooth() Wednesday, 9 September 2009
• qplot(log10(carat), log10(price), data = diamonds) + geom_smooth() Wednesday, 9 September 2009
• qplot(log10(carat), log10(price), data = diamonds) + geom_smooth(method = "lm") Wednesday, 9 September 2009
• 2d bins Wednesday, 9 September 2009
• # Very basic cleaning diamonds\$x[diamonds\$x == 0] <- NA diamonds\$y[diamonds\$y == 0] <- NA diamonds\$y[diamonds\$y > 12] <- NA qplot(x, y, data = diamonds) qplot(x, y, data = diamonds, geom = "bin2d") qplot(x, y, data = diamonds, geom = "hex") qplot(x, y, data = diamonds, geom = "bin2d", bins = 100) qplot(x, y, data = diamonds, geom = "hex", bins = 100) # Zoom in qplot(x, y, data = diamonds, geom = "bin2d", bins = 100) + xlim(4,7) + ylim(4,7) qplot(x, y, data = diamonds, geom = "bin2d", bins = 100) + xlim(4,5) + ylim(4,5) Wednesday, 9 September 2009
• qplot(x, x / y, data = diamonds, geom = "bin2d") qplot(x, log(x / y), data = diamonds, geom = "bin2d") clean <- subset(diamonds, abs(log(x / y)) < 0.1) qplot(x, log(x / y), data = clean, geom = "bin2d") qplot(x, log(x / y), data = clean, geom = "bin2d", bins = 80) Wednesday, 9 September 2009
• qplot(x, x / y, data = diamonds, geom = "bin2d") qplot(x, log(x / y), data = diamonds, geom = "bin2d") clean <- subset(diamonds, abs(log(x / y)) < 0.1) qplot(x, log(x / y), data = clean, geom = "bin2d") qplot(x, log(x / y), data = clean, geom = "bin2d", bins = 80) What would be a good name for log(x / y)? What other variable might you create to go with it? Wednesday, 9 September 2009
• Your turn Continue to explore the relationship between x, y, z and depth. Create new variables as necessary. (Hint: rerun the cleaning code from last week, and create more as necessary) Some good ideas here: http:// www.diamondhelpers.com/ﬁvesteps/4- certiﬁed-diamonds.shtml Wednesday, 9 September 2009
• x table width z depth = z / diameter table = table width / x * 100 Wednesday, 9 September 2009
• y_big <- diamonds\$y > 10 z_big <- diamonds\$z > 6 x_zero <- diamonds\$x == 0 y_zero <- diamonds\$y == 0 z_zero <- diamonds\$z == 0 diamonds\$x[x_zero] <- NA diamonds\$y[y_zero | y_big] <- NA diamonds\$z[z_zero | z_big] <- NA Wednesday, 9 September 2009
• qplot(z/y * 100, depth, data = diamonds) last_plot() + xlim(50, 100) last_plot() + xlim(50, 80) + ylim(50, 80) qplot(z/x * 100, depth, data = diamonds) + xlim(50, 80) + ylim(50, 80) qplot(z/x * 100, depth / (z/x), data = diamonds) last_plot() + xlim(50, 80) + ylim(80, 120) last_plot() + ylim(95, 105) # ... Wednesday, 9 September 2009
• Iteration & stories Wednesday, 9 September 2009
• Stories Best data analyses tell a story, with a natural ﬂow from beginning to end. For homeworks, try and come up with three plots that tell a story. Stories about a small sample of the data can work well. Wednesday, 9 September 2009
• qplot(cty, hwy, data = mpg) qplot(cty, hwy, data = mpg, geom = "jitter") qplot(cty, hwy, data = mpg, geom = "jitter", colour = class) qplot(cty, cty / hwy, data = mpg, geom = "jitter", colour = class) qplot(cty, cty / hwy, data = mpg, colour = class) qplot(displ, cty / hwy, data = mpg, colour = class) qplot(displ, cty / hwy, data = mpg) + facet_wrap(~ class) qplot(displ, cty / hwy, data = mpg) + facet_wrap(~ class) + geom_smooth(se = F) qplot(displ, cty / hwy, data = mpg) + facet_wrap(~ class) + geom_smooth(method = "lm", se = F) qplot(displ, cty, data = mpg) + facet_wrap(~ class) Wednesday, 9 September 2009
• Project Due in 3.5 weeks. Bigger group data analysis project. (Will be discussing group dynamics on Monday) Homework is to get you started working with the data. Wednesday, 9 September 2009
• Next week Checking on a slot machine. Learning how to write functions. Basics of simulation. Wednesday, 9 September 2009
• Feedback http://hadley.wufoo.com/forms/stat405-feedback/ Wednesday, 9 September 2009