Skip to content

Commit 91e6e3d

Browse files
committed
Add scatterplot cookbook tests; geom_smooth now respects group aes
1 parent 30813df commit 91e6e3d

File tree

2 files changed

+87
-26
lines changed

2 files changed

+87
-26
lines changed

R/trace_generation.R

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ layer2traces <- function(l, d, misc) {
1313
g <- list(geom=l$geom$objname,
1414
data=not.na(d),
1515
prestats.data=not.na(misc$prestats.data))
16+
if (g$geom == "smooth") {
17+
if (isTRUE(misc$smoothRibbon)) g$geom <- "smoothRibbon"
18+
if (isTRUE(misc$smoothLine)) g$geom <- "smoothLine"
19+
}
1620
# needed for when group, etc. is an expression.
1721
g$aes <- sapply(l$mapping, function(k) as.character(as.expression(k)))
1822
# Partial conversion for geom_violin (Plotly does not offer KDE yet)
@@ -187,13 +191,7 @@ layer2traces <- function(l, d, misc) {
187191
data.list <- structure(list(list(data=basic$data, params=basic$params)),
188192
names=basic$params$name)
189193
}
190-
if (isTRUE(misc$smoothLine)) {
191-
getTrace <- geom2trace[["smoothLine"]]
192-
} else if (isTRUE(misc$smoothRibbon)) {
193-
getTrace <- geom2trace[["smoothRibbon"]]
194-
} else {
195-
getTrace <- geom2trace[[basic$geom]]
196-
}
194+
getTrace <- geom2trace[[basic$geom]]
197195
if(is.null(getTrace)){
198196
warning("Conversion not implemented for geom_",
199197
g$geom, " (basic geom_", basic$geom, "), ignoring. ",
@@ -383,6 +381,14 @@ toBasic <- list(
383381
g$params$sizemax <- max(g$prestats.data$globsizemax)
384382
}
385383
g
384+
},
385+
smoothLine=function(g) {
386+
if (length(unique(g$data$group)) == 1) g$params$colour <- "#3366FF"
387+
group2NA(g, "path")
388+
},
389+
smoothRibbon=function(g) {
390+
if (is.null(g$params$alpha)) g$params$alpha <- 0.1
391+
group2NA(g, "ribbon")
386392
}
387393
)
388394

@@ -645,24 +651,5 @@ geom2trace <- list(
645651
type="scatter",
646652
mode="lines",
647653
line=paramORdefault(params, aes2line, line.defaults))
648-
},
649-
smoothRibbon=function(data, params) {
650-
list(x=c(data$x[1], data$x, rev(data$x)),
651-
y=c(data$ymin[1], data$ymax, rev(data$ymin)),
652-
type="scatter",
653-
line=paramORdefault(params, aes2line, ribbon.line.defaults),
654-
fill="tonexty",
655-
fillcolor=toFill(params$fill, ifelse(is.null(params$alpha),
656-
0.1, params$alpha)))
657-
},
658-
smoothLine=function(data, params) {
659-
line.defaults$colour <- "#3366FF"
660-
list(x=data$x,
661-
y=data$y,
662-
name=params$name,
663-
text=data$text,
664-
type="scatter",
665-
mode="lines",
666-
line=paramORdefault(params, aes2line, line.defaults))
667654
}
668655
)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)