Skip to content

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

Merged
merged 50 commits into from
Feb 23, 2015
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
baa31e8
test for error bars first or last
tdhock Jan 27, 2015
adcc2ba
test different colors
tdhock Jan 27, 2015
8169a3d
test only error bars
tdhock Jan 28, 2015
2a37221
only error bars passes test
tdhock Jan 28, 2015
12d8019
error bars after passes test
tdhock Jan 28, 2015
f27f77a
error bars before passes test
tdhock Jan 28, 2015
d907817
colored error bars
tdhock Feb 2, 2015
565102d
colored error bar test fails
tdhock Feb 2, 2015
ea78dc3
symmetric and colored error bar test passes
tdhock Feb 2, 2015
c8e98eb
errorbarh merges, ggplot-errorbar tests pass
tdhock Feb 2, 2015
dea0c1a
fix tile and theme tests
tdhock Feb 2, 2015
1d032af
move back constants
tdhock Feb 4, 2015
c768c2f
tick tests fail
tdhock Feb 5, 2015
f2d7cf3
for discussion
tdhock Feb 9, 2015
eab0348
test y axis range
tdhock Feb 9, 2015
de10b3e
keep marianne additions
tdhock Feb 9, 2015
8062b9c
reverse y axis
tdhock Feb 9, 2015
56e7515
tick tests complete
tdhock Feb 9, 2015
ba1c206
facet tests
tdhock Feb 10, 2015
5cad2f4
disable coord_flip + facet tests
tdhock Feb 11, 2015
3358708
order traces
tdhock Feb 11, 2015
b2f3c11
do not order if there is no order
tdhock Feb 11, 2015
a04c705
flip traces
tdhock Feb 11, 2015
a29a3c7
more breaks, labels, limits tests
tdhock Feb 12, 2015
3047869
scale(labels) for trace names
tdhock Feb 12, 2015
f791134
works with no scale(labels)
tdhock Feb 12, 2015
e2ea24b
breaks=NULL hides ticks, lines, labels
tdhock Feb 12, 2015
7926a30
scale(limits) means axis$range
tdhock Feb 12, 2015
93585b4
get trace range from ggplot range
tdhock Feb 12, 2015
623512e
scale(breaks=numeric) means axis$dtick and autotick
tdhock Feb 12, 2015
8b0a39f
negative reverse scale
tdhock Feb 12, 2015
96f998c
test for correct values with reverse scale
tdhock Feb 12, 2015
2581b0e
multiply by -1 for reversed aes
tdhock Feb 12, 2015
4766545
exclude NA data from traces
tdhock Feb 16, 2015
11522c2
just use is.na not apply
tdhock Feb 17, 2015
5c98619
add save_ouputs
tdhock Feb 17, 2015
314a6ec
explicit theme_grey and fill=NA for readable ggplot
tdhock Feb 17, 2015
a5fa7b2
delete old trace merging code
tdhock Feb 20, 2015
a23b3f2
tests fail for disappearing boxes
tdhock Feb 20, 2015
2742345
test pass, 3 boxes render
tdhock Feb 20, 2015
63b2b71
remove TO DISCUSS
tdhock Feb 20, 2015
7f8ad6c
replace #+ with #
tdhock Feb 20, 2015
5cf9a3c
delete print.trace and trace class
tdhock Feb 20, 2015
c47b802
fix marianne minor comments
tdhock Feb 20, 2015
781f6bf
add space
tdhock Feb 23, 2015
80536ed
delete theme_grey
tdhock Feb 23, 2015
4af13ae
update NEWS/DESCRIPTION
tdhock Feb 23, 2015
cf9d5a4
Remove funny file
mkcor Feb 23, 2015
91a5a45
Revert testthat.R file to latest commit on master
mkcor Feb 23, 2015
3337d85
Revert unrelated test file to latest commit on master
mkcor Feb 23, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions R/corresp_one_one.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ coded.lty <- c("22"="dash",
"224282F2"="dash",
"F1"="dash")

# Convert R lty line type codes to plotly "dash" codes.
lty2dash <- c(numeric.lty, named.lty, coded.lty)

# Convert ggplot2 aes to line parameters.
aes2line <- c(linetype="dash",
colour="color",
size="width",
direction="shape")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove trailing white line.

103 changes: 49 additions & 54 deletions R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
},
Expand All @@ -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"))
Expand Down Expand Up @@ -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){
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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"
Copy link
Contributor

Choose a reason for hiding this comment

The 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).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does not fit on one line for me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's your editor's config in this respect?
Anyway, I can't argue, because this complies with our guideline "keep the lines to max 80 characters", I was just curious.

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
}
4 changes: 4 additions & 0 deletions R/print.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
print.trace <- function(x, ...){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this file?

str(x)
invisible(x)
}
42 changes: 32 additions & 10 deletions R/trace_generation.R
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ layer2traces <- function(l, d, misc) {
data.params <- data.list[[data.i]]
data.params$params$stat.type <- l$stat$objname
tr <- do.call(getTrace, data.params)
class(tr) <- "trace"
for (v.name in c("x", "y")) {
vals <- tr[[v.name]]
if (length(vals) > 0 && is.na(vals[length(vals)])) {
Expand Down Expand Up @@ -378,6 +379,35 @@ group2NA <- function(g, geom) {
g
}

### Make a trace for geom_errorbar -> error_y or geom_errorbarh ->
### error_x.
make.errorbar <- function(data, params, xy){
tr <-
list(x=data$x,
y=data$y,
type="scatter",
mode="none")
err.name <- paste0("error_", xy)
min.name <- paste0(xy, "min")
max.name <- paste0(xy, "max")
e <-
list(array=data[[max.name]]-data[[xy]],
type="data",
width=params$width,
symmetric=TRUE,
color=if(!is.null(params$colour)){
toRGB(params$colour)
}else{
toRGB(data$colour)
})
arrayminus <- data[[xy]]-data[[min.name]]
if(!isTRUE(all.equal(e$array, arrayminus))){
e$arrayminus <- arrayminus
e$symmetric <- FALSE
}
tr[[err.name]] <- e
tr
}

# Convert basic geoms to traces.
geom2trace <- list(
Expand Down Expand Up @@ -536,18 +566,10 @@ geom2trace <- list(
L
},
errorbar=function(data, params) {
list(x=data$x,
y=data$y,
error_y=list(arrayminus=data$y-data$ymin,
array=data$ymax-data$y,
color=toRGB(data$colour)))
make.errorbar(data, params, "y")
},
errorbarh=function(data, params) {
list(x=data$x,
y=data$y,
error_x=list(arrayminus=data$x-data$xmin,
array=data$xmax-data$x,
color=toRGB(data$colour)))
make.errorbar(data, params, "x")
},
area=function(data, params) {
list(x=c(data$x[1], data$x, tail(data$x, n=1)),
Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/test-ggplot-errorbar-horizontal.R
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use expect_true() directly! ;)


save_outputs(g, "errorbar-horizontal")
})
25 changes: 0 additions & 25 deletions tests/testthat/test-ggplot-errorbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,3 @@ test_that("geom_errorbar gives errorbars", {
save_outputs(g, "errorbar")
})

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$arrayminus, c(0.1, 0.3))
expect_equal(L[[1]]$error_x$array, L[[1]]$error_x$arrayminus)

save_outputs(g, "errorbar-horizontal")
})
6 changes: 4 additions & 2 deletions tests/testthat/test-ggplot-heatmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

ww <- reshape2::melt(workweek)
ww$day <- factor(ww$day, wdays)
Copy link
Contributor

Choose a reason for hiding this comment

The 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?
Because ww$day is already a factor...
These tests are hard-coded so I'm not sure that's necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Expand Down
7 changes: 4 additions & 3 deletions tests/testthat/test-ggplot-theme.R
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))+
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spacing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please clarify?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a space before the + operator.

theme_grey()

test_that("background translated correctly",{
ggiris <- iris.base + theme(panel.background=element_rect(fill="blue")) +
Expand Down Expand Up @@ -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"),
Expand Down
Loading