Skip to content

Commit 09f9dac

Browse files
committed
Merge pull request #183 from ropensci/carson-smooth
geom_smooth()
2 parents d185f67 + a97ba2b commit 09f9dac

File tree

6 files changed

+132
-6
lines changed

6 files changed

+132
-6
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: plotly
22
Type: Package
33
Title: Interactive, publication-quality graphs online.
4-
Version: 0.5.24
4+
Version: 0.5.25
55
Authors@R: c(person("Chris", "Parmer", role = c("aut", "cre"),
66
email = "[email protected]"),
77
person("Scott", "Chamberlain", role = "aut",

NEWS

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
0.5.25 -- 10 March 2015
2+
3+
Implemented geom_smooth() #183
4+
15
0.5.24 -- 10 March 2015
26

3-
Implemented #167
7+
Implemented facet_wrap(scales="free") #167
48

59
0.5.23 -- 10 March 2015.
610

7-
geom_ribbon now respects alpha transparency
11+
geom_ribbon() now respects alpha transparency
812

913
0.5.22 -- 2 March 2015.
1014

R/ggplotly.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ gg2list <- function(p){
218218

219219
# This extracts essential info for this geom/layer.
220220
traces <- layer2traces(L, df, misc)
221-
221+
222222
possible.legends <- markLegends[[L$geom$objname]]
223223
actual.legends <- possible.legends[possible.legends %in% names(L$mapping)]
224224
layer.legends[[paste(i)]] <- actual.legends

R/trace_generation.R

+32-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ 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+
1617
# needed for when group, etc. is an expression.
1718
g$aes <- sapply(l$mapping, function(k) as.character(as.expression(k)))
1819
# Partial conversion for geom_violin (Plotly does not offer KDE yet)
@@ -22,6 +23,22 @@ layer2traces <- function(l, d, misc) {
2223
probability density estimation is not supported in Plotly yet.")
2324
}
2425

26+
# geom_smooth() means geom_line() + geom_ribbon()
27+
# Note the line is always drawn, but ribbon is not if se = FALSE.
28+
if (g$geom == "smooth") {
29+
# If smoothLine has been compiled already, consider smoothRibbon.
30+
if (isTRUE(misc$smoothLine)) {
31+
misc$smoothLine <- FALSE
32+
if (isTRUE(l$stat_params$se == FALSE)) {
33+
return(NULL)
34+
} else {
35+
g$geom <- "smoothRibbon"
36+
}
37+
} else {
38+
misc$smoothLine <- TRUE
39+
g$geom <- "smoothLine"
40+
}
41+
}
2542
# Barmode and bargap
2643
barmode <- "group"
2744
if (g$geom == "bar" || g$geom == "histogram") {
@@ -187,7 +204,6 @@ layer2traces <- function(l, d, misc) {
187204
data.list <- structure(list(list(data=basic$data, params=basic$params)),
188205
names=basic$params$name)
189206
}
190-
191207
getTrace <- geom2trace[[basic$geom]]
192208
if(is.null(getTrace)){
193209
warning("Conversion not implemented for geom_",
@@ -282,7 +298,13 @@ layer2traces <- function(l, d, misc) {
282298
}
283299
no.sort[[tr.i]]$sort <- NULL
284300
}
285-
no.sort
301+
# if line portion of geom_smooth was compiled, call layer2traces()
302+
# again for ribbon portion
303+
if (isTRUE(misc$smoothLine)) {
304+
c(layer2traces(l, d, misc), no.sort)
305+
} else {
306+
no.sort
307+
}
286308
}#layer2traces
287309

288310

@@ -378,6 +400,14 @@ toBasic <- list(
378400
g$params$sizemax <- max(g$prestats.data$globsizemax)
379401
}
380402
g
403+
},
404+
smoothLine=function(g) {
405+
if (length(unique(g$data$group)) == 1) g$params$colour <- "#3366FF"
406+
group2NA(g, "path")
407+
},
408+
smoothRibbon=function(g) {
409+
if (is.null(g$params$alpha)) g$params$alpha <- 0.1
410+
group2NA(g, "ribbon")
381411
}
382412
)
383413

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")

tests/testthat/test-ggplot-smooth.R

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
context("smooth")
2+
3+
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + geom_smooth()
4+
5+
test_that("geom_point() + geom_smooth() produces 3 traces", {
6+
info <- gg2list(p)
7+
expect_true(sum(names(info) == "") == 3)
8+
save_outputs(p, "smooth")
9+
})
10+
11+
p2 <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + geom_smooth(se = FALSE)
12+
13+
test_that("geom_point() + geom_smooth(se = FALSE) produces 2 traces", {
14+
info2 <- gg2list(p2)
15+
expect_true(sum(names(info2) == "") == 2)
16+
save_outputs(p2, "smooth-se-false")
17+
})
18+

0 commit comments

Comments
 (0)