|
| 1 | +set.seed(955) |
| 2 | +# Make some noisily increasing data |
| 3 | +dat <- data.frame(cond = rep(c("A", "B"), each=10), |
| 4 | + xvar = c(1.475957, -3.423712, 1.966129, 5.575364, 2.954719, 2.768286, 3.507499, 6.945000, 12.135050, 10.231673, 13.040393, 12.231689, 13.506993, 13.590874, 15.455178, 28.431185, 17.758937, 24.730797, 22.954238, 21.122766), |
| 5 | + yvar = c(-1.315387, 3.323239, 4.452183, 4.597885, 5.697203, 5.991221, 5.764561, 10.163165, 14.805634, 11.447913, 12.163597, 10.930851, 13.491366, 11.800783, 19.246991, 13.870457, 11.031923, 22.700302, 24.877547, 22.520114)) |
| 6 | +# cond xvar yvar |
| 7 | +# A -4.252354091 3.473157275 |
| 8 | +# A 1.702317971 0.005939612 |
| 9 | +# ... |
| 10 | +# B 17.793359218 19.718587761 |
| 11 | +# B 19.319909163 19.647899863 |
| 12 | + |
| 13 | +g <- ggplot(dat, aes(x=xvar, y=yvar)) + |
| 14 | + geom_point(shape=1) # Use hollow circles |
| 15 | +save_outputs(g, "scatterplots-hollow") |
| 16 | + |
| 17 | +g <- ggplot(dat, aes(x=xvar, y=yvar)) + |
| 18 | + geom_point(shape=1) + |
| 19 | + geom_smooth(method=lm) # Add linear regression line |
| 20 | +save_outputs(g, "scatterplots-smooth-lm") |
| 21 | + |
| 22 | +g <- ggplot(dat, aes(x=xvar, y=yvar)) + |
| 23 | + geom_point(shape=1) + |
| 24 | + geom_smooth(method=lm, se=FALSE) # Don't add shaded confidence region |
| 25 | +save_outputs(g, "scatterplots-smooth-lm-se-false") |
| 26 | + |
| 27 | + |
| 28 | +g <- ggplot(dat, aes(x=xvar, y=yvar)) + |
| 29 | + geom_point(shape=1) + # Use hollow circles |
| 30 | + geom_smooth() # Add a loess smoothed fit curve with confidence region |
| 31 | +save_outputs(g, "scatterplots-loess") |
| 32 | + |
| 33 | +# Set color by cond |
| 34 | +g <- ggplot(dat, aes(x=xvar, y=yvar, color=cond)) + geom_point(shape=1) |
| 35 | +save_outputs(g, "scatterplots-color") |
| 36 | + |
| 37 | +# # Same, but with different colors and add regression lines |
| 38 | +g <- ggplot(dat, aes(x=xvar, y=yvar, color=cond)) + geom_point(shape=1) + |
| 39 | + scale_colour_hue(l=50) + # Use a slightly darker palette than normal |
| 40 | + geom_smooth(method=lm, se=FALSE) |
| 41 | +save_outputs(g, "scatterplots-scale-color-hue") |
| 42 | + |
| 43 | +# Extend the regression lines beyond the domain of the data |
| 44 | +g <- ggplot(dat, aes(x=xvar, y=yvar, color=cond)) + geom_point(shape=1) + |
| 45 | + scale_colour_hue(l=50) + |
| 46 | + geom_smooth(method=lm, se=FALSE, fullrange=T) |
| 47 | +save_outputs(g, "scatterplots-full-range") |
| 48 | + |
| 49 | +# Set shape by cond |
| 50 | +g <- ggplot(dat, aes(x=xvar, y=yvar, shape=cond)) + geom_point() |
| 51 | +save_outputs(g, "scatterplots-shape") |
| 52 | + |
| 53 | +# Same, but with different shapes |
| 54 | +g <- ggplot(dat, aes(x=xvar, y=yvar, shape=cond)) + geom_point() + |
| 55 | + scale_shape_manual(values=c(1,2)) # Use a hollow circle and triangle |
| 56 | +save_outputs(g, "scatterplots-shape-manual") |
| 57 | + |
| 58 | +# Round xvar and yvar to the nearest 5 |
| 59 | +dat$xrnd <- round(dat$xvar/5)*5 |
| 60 | +dat$yrnd <- round(dat$yvar/5)*5 |
| 61 | + |
| 62 | +# Make each dot partially transparent, with 1/4 opacity |
| 63 | +# For heavy overplotting, try using smaller values |
| 64 | +g <- ggplot(dat, aes(x=xrnd, y=yrnd)) + |
| 65 | + geom_point(shape=19, # Use solid circles |
| 66 | + alpha=1/4) # 1/4 opacity |
| 67 | +save_outputs(g, "scatterplots-overlap") |
| 68 | + |
| 69 | +# Jitter the points |
| 70 | +# Jitter range is 1 on the x-axis, .5 on the y-axis |
| 71 | +g <- ggplot(dat, aes(x=xrnd, y=yrnd)) + |
| 72 | + geom_point(shape=1, # Use hollow circles |
| 73 | + position=position_jitter(width=1,height=.5)) |
| 74 | +save_outputs(g, "scatterplots-jitter") |
0 commit comments