-
Notifications
You must be signed in to change notification settings - Fork 633
Fixes for error bars and ticks #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
baa31e8
adcc2ba
8169a3d
2a37221
12d8019
f27f77a
d907817
565102d
ea78dc3
c8e98eb
dea0c1a
1d032af
c768c2f
f2d7cf3
eab0348
de10b3e
8062b9c
56e7515
ba1c206
5cad2f4
3358708
b2f3c11
a04c705
a29a3c7
3047869
f791134
e2ea24b
7926a30
93585b4
623512e
8b0a39f
96f998c
2581b0e
4766545
11522c2
5c98619
314a6ec
a5fa7b2
a23b3f2
2742345
63b2b71
7f8ad6c
5cf9a3c
c47b802
781f6bf
80536ed
4af13ae
cf9d5a4
91a5a45
3337d85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,9 +26,6 @@ ribbon.line.defaults$colour <- NA | |
polygon.line.defaults <- line.defaults | ||
polygon.line.defaults$colour <- NA | ||
|
||
# Convert R lty line type codes to plotly "dash" codes. | ||
lty2dash <- c(numeric.lty, named.lty, coded.lty) | ||
|
||
aesConverters <- list(linetype=function(lty) { | ||
lty2dash[as.character(lty)] | ||
}, | ||
|
@@ -52,6 +49,8 @@ markLegends <- | |
path=c("linetype", "size", "colour", "shape"), | ||
polygon=c("colour", "fill", "linetype", "size", "group"), | ||
bar=c("colour", "fill"), | ||
errorbar=c("colour", "linetype"), | ||
errorbarh=c("colour", "linetype"), | ||
step=c("linetype", "size", "colour"), | ||
boxplot=c("x"), | ||
text=c("colour")) | ||
|
@@ -195,7 +194,8 @@ gg2list <- function(p){ | |
traces <- layer2traces(L, df, misc) | ||
|
||
# Associate error bars with previous traces | ||
if (grepl("errorbar", L$geom$objname)) { | ||
##if (grepl("errorbar", L$geom$objname)) { #TDH 28 Jan 2015. | ||
if(FALSE){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a debugging leftover? |
||
for (j in 1:length(trace.list)) { | ||
temp <- list() | ||
ind <- traces[[1]]$x %in% trace.list[[j]]$x | ||
|
@@ -612,63 +612,58 @@ gg2list <- function(p){ | |
layout$legend$bgcolor <- toRGB(s(rect_fill)) | ||
} | ||
|
||
trace.list$kwargs <- list(layout=layout) | ||
|
||
if (length(trace.list) < 2) { | ||
if(length(trace.list) == 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment about spacing. |
||
stop("No exportable traces") | ||
} | ||
|
||
if (length(trace.list) > 2) { | ||
# Maybe some traces should be merged. | ||
nr <- length(trace.list) - 1 | ||
comp <- data.frame(matrix(ncol=2, nrow=nr)) | ||
colnames(comp) <- c("name", "mode") | ||
|
||
for (j in 1:nr) { | ||
# Use lapply to be elegant? | ||
for (d in colnames(comp)) { | ||
try(comp[[d]][j] <- trace.list[[j]][[d]], silent=TRUE) | ||
# "names" might be NULL in trace.list | ||
|
||
mode.mat <- matrix(NA, 3, 3) | ||
rownames(mode.mat) <- colnames(mode.mat) <- c("markers", "lines", "none") | ||
mode.mat["markers", "lines"] <- | ||
mode.mat["lines", "markers"] <- "lines+markers" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fun! I never do this 'two-assignments-at-once'. I believe it could/should be on one line (it's less than 80 characters in length). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it does not fit on one line for me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's your editor's config in this respect? |
||
mode.mat["markers", "none"] <- mode.mat["none", "markers"] <- "markers" | ||
mode.mat["lines", "none"] <- mode.mat["none", "lines"] <- "lines" | ||
merged.traces <- list() | ||
not.merged <- trace.list | ||
while(length(not.merged)){ | ||
tr <- not.merged[[1]] | ||
not.merged <- not.merged[-1] | ||
## Are there any traces that have not yet been merged, and can be | ||
## merged with tr? | ||
can.merge <- rep(FALSE, l=length(not.merged)) | ||
for(other.i in seq_along(not.merged)){ | ||
other <- not.merged[[other.i]] | ||
criteria <- c() | ||
for(must.be.equal in c("x", "y", "xaxis", "yaxis")){ | ||
other.attr <- other[[must.be.equal]] | ||
tr.attr <- tr[[must.be.equal]] | ||
criteria[[must.be.equal]] <- isTRUE(all.equal(other.attr, tr.attr)) | ||
} | ||
if(all(criteria)){ | ||
can.merge[[other.i]] <- TRUE | ||
} | ||
} | ||
# Compare the "name"s of the traces (so far naively inherited from layers) | ||
layernames <- unique(comp$name) | ||
if (length(layernames) < nr) { | ||
# Some traces (layers at this stage) have the same "name"s. | ||
for (j in 1:length(layernames)) { | ||
lind <- which(layernames[j] == comp$name) | ||
lmod <- c("lines", "markers") %in% comp$mode[lind] | ||
# Is there one with "mode": "lines" and another with "mode": "markers"? | ||
if (all(lmod)) { | ||
# Data comparison | ||
xcomp <- (trace.list[[lind[1]]]$x == trace.list[[lind[2]]]$x) | ||
ycomp <- (trace.list[[lind[1]]]$y == trace.list[[lind[2]]]$y) | ||
if (all(xcomp) && all(ycomp)) { | ||
# Union of the two traces | ||
keys <- unique(c(names(trace.list[[lind[1]]]), | ||
names(trace.list[[lind[2]]]))) | ||
temp <- setNames(mapply(c, trace.list[[lind[1]]][keys], | ||
trace.list[[lind[2]]][keys]), keys) | ||
# Info is duplicated in fields which are in common | ||
temp <- lapply(temp, unique) | ||
# But unique() is detrimental to line or marker sublist | ||
temp$line <- trace.list[[lind[1]]]$line | ||
temp$marker <- trace.list[[lind[2]]]$marker | ||
# Overwrite x and y to be safe | ||
temp$x <- trace.list[[lind[1]]]$x | ||
temp$y <- trace.list[[lind[1]]]$y | ||
# Specify new one mode | ||
temp$mode <- "lines+markers" | ||
# Keep one trace and remove the other one | ||
trace.list[[lind[1]]] <- temp | ||
trace.list <- trace.list[-lind[2]] | ||
# Update comparison table | ||
comp <- comp[-lind[2], ] | ||
} | ||
to.merge <- not.merged[can.merge] | ||
not.merged <- not.merged[!can.merge] | ||
for(other in to.merge){ | ||
new.mode <- tryCatch({ | ||
mode.mat[tr$mode, other$mode] | ||
}, error=function(e){ | ||
NA | ||
}) | ||
if(is.character(new.mode) && !is.na(new.mode)){ | ||
tr$mode <- new.mode | ||
} | ||
attrs <- c("error_x", "error_y", "marker", "line") | ||
for(attr in attrs){ | ||
if(!is.null(other[[attr]]) && is.null(tr[[attr]])){ | ||
tr[[attr]] <- other[[attr]] | ||
} | ||
} | ||
} | ||
merged.traces[[length(merged.traces)+1]] <- tr | ||
} | ||
|
||
merged.traces$kwargs <- list(layout=layout) | ||
|
||
trace.list | ||
merged.traces | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
print.trace <- function(x, ...){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this file? |
||
str(x) | ||
invisible(x) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
context("geom_errorbarh") | ||
|
||
test_that("geom_errorbarh gives horizontal errorbars", { | ||
|
||
df <- data.frame( | ||
trt = factor(c(1, 1, 2, 2)), | ||
resp = c(1, 5, 3, 4), | ||
group = factor(c(1, 2, 1, 2)), | ||
se = c(0.1, 0.3, 0.3, 0.4) | ||
) | ||
g <- ggplot(df, aes(resp, trt, colour=group)) + geom_point() | ||
# Define the limits of the horizontal errorbars | ||
g <- g + geom_errorbarh(aes(xmax = resp + se, xmin = resp - se)) | ||
|
||
L <- gg2list(g) | ||
|
||
# Expect 2 traces | ||
expect_equal(length(L), 3) | ||
# Expect scatter plot and its error bars to have the same color | ||
expect_identical(L[[1]]$marker$color, L[[1]]$error_x$color) | ||
expect_identical(L[[2]]$marker$color, L[[2]]$error_x$color) | ||
# Expect given errorbar values | ||
expect_equal(L[[1]]$error_x$array, c(0.1, 0.3)) | ||
expect_equal(L[[1]]$error_x$symmetric, TRUE) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
|
||
save_outputs(g, "errorbar-horizontal") | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,12 @@ wdays <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") | |
dtimes <- c("Morning", "Afternoon", "Evening") | ||
workweek <- matrix(c(1, 20, 30, 20, 1, 60, 30, 60, 1, 50, 80, -10, 1, 30, 20), | ||
nrow=5, ncol=3, byrow=TRUE, | ||
dimnames=list(wdays, dtimes)) | ||
dimnames=list(day=wdays, time=dtimes)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
ww <- reshape2::melt(workweek) | ||
ww$day <- factor(ww$day, wdays) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you add this just to make it explicit, and/or to handle some possible subsetting? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is to make sure the days occur in chronological order (not alphabetical order) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, thanks! Good to know. |
||
ww$time <- factor(ww$time, dtimes) | ||
# Plot a heatmap using geom_tile | ||
hm <- ggplot(ww) + geom_tile(aes(x=Var1, y=Var2, fill=value)) | ||
hm <- ggplot(ww) + geom_tile(aes(x=day, y=time, fill=value)) | ||
|
||
test_that("geom_tile is translated to type=heatmap", { | ||
L <- gg2list(hm) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
context("ggplot themes") | ||
|
||
iris.base <- ggplot(iris) + geom_point(aes(Petal.Width, Sepal.Width)) | ||
iris.base <- ggplot(iris) + | ||
geom_point(aes(Petal.Width, Sepal.Width))+ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spacing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you please clarify? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be a space before the |
||
theme_grey() | ||
|
||
test_that("background translated correctly",{ | ||
ggiris <- iris.base + theme(panel.background=element_rect(fill="blue")) + | ||
|
@@ -54,10 +56,9 @@ test_that("dotted/dashed grid translated as line with alpha=0.1",{ | |
for (xy in c("x", "y")) { | ||
ax.list <- info$kwargs$layout[[paste0(xy, "axis")]] | ||
expect_identical(ax.list$gridcolor, toRGB("white", 0.1)) | ||
expect_identical(ax.list$gridcolor, "rgba(255,255,255,0.1)") | ||
} | ||
|
||
save_outputs(ggiris, "theme-dashed-grid-lines") | ||
save_outputs(ggiris, "theme-dashed-grid-lines") | ||
}) | ||
|
||
countrypop <- data.frame(country=c("Paraguay", "Peru", "Philippines"), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove trailing white line.