Skip to content

Commit ed8461e

Browse files
author
Toby Dylan Hocking
committed
Merge pull request #171 from ropensci/toby-ylim
Toby ylim
2 parents faaf4da + e949647 commit ed8461e

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
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.21
4+
Version: 0.5.22
55
Authors@R: c(person("Chris", "Parmer", role = c("aut", "cre"),
66
email = "[email protected]"),
77
person("Scott", "Chamberlain", role = "aut",

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
0.5.22 -- 2 March 2015.
2+
3+
Fixes for ylim() #171.
4+
15
0.5.21 -- 23 February 2015.
26

37
Fixes for error bars and tick marks.

R/ggplotly.R

+17-13
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,28 @@ gg2list <- function(p){
305305
ax.list$tickangle <- -tick.text$angle
306306
}
307307
ax.list$tickfont <- theme2font(tick.text)
308+
309+
## determine axis type first, since this information is used later
310+
## (trace.order.list is only used for type=category).
311+
title.text <- e(s("axis.title.%s"))
312+
ax.list$titlefont <- theme2font(title.text)
313+
ax.list$type <- if (misc$is.continuous[[xy]]){
314+
"linear"
315+
} else if (misc$is.discrete[[xy]]){
316+
"category"
317+
} else if (misc$is.date[[xy]] || misc$is.datetime[[xy]]){
318+
"date"
319+
} else {
320+
stop("unrecognized data type for ", xy, " axis")
321+
}
308322

309323
# Translate axes labels.
310324
scale.i <- which(p$scales$find(xy))
311325
ax.list$title <- if(length(scale.i)){
312326
sc <- p$scales$scales[[scale.i]]
313-
trace.order.list[[xy]] <- sc$limits
327+
if(ax.list$type == "category"){
328+
trace.order.list[[xy]] <- sc$limits
329+
}
314330
trace.name.map[sc$breaks] <- sc$labels
315331
if (is.null(sc$breaks)) {
316332
ax.list$showticklabels <- FALSE
@@ -346,18 +362,6 @@ gg2list <- function(p){
346362
p$labels[[xy]]
347363
}
348364

349-
title.text <- e(s("axis.title.%s"))
350-
ax.list$titlefont <- theme2font(title.text)
351-
ax.list$type <- if(misc$is.continuous[[xy]]){
352-
"linear"
353-
}else if(misc$is.discrete[[xy]]){
354-
"category"
355-
}else if(misc$is.date[[xy]] || misc$is.datetime[[xy]]){
356-
"date"
357-
}else{
358-
stop("unrecognized data type for ", xy, " axis")
359-
}
360-
361365
ax.list$zeroline <- FALSE # ggplot2 plots do not show zero lines
362366
# Lines drawn around the plot border.
363367
ax.list$showline <- !is.blank("panel.border", TRUE)

tests/testthat/test-ggplot-ylim.R

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
context("ggplot ylim")
2+
3+
## http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_%28ggplot2%29/
4+
5+
df <- data.frame(time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
6+
total_bill = c(14.89, 17.23))
7+
8+
gg.ylim <-
9+
ggplot(data=df, aes(x=time, y=total_bill, group=1)) +
10+
geom_line() +
11+
geom_point() +
12+
ylim(0, max(df$total_bill)) +
13+
xlab("Time of day") + ylab("Total bill") +
14+
ggtitle("Average bill for 2 people")
15+
16+
expect_traces <- function(gg, n.traces, name){
17+
stopifnot(is.ggplot(gg))
18+
stopifnot(is.numeric(n.traces))
19+
save_outputs(gg, paste0("ylim-", name))
20+
L <- gg2list(gg)
21+
is.trace <- names(L) == ""
22+
all.traces <- L[is.trace]
23+
no.data <- sapply(all.traces, function(tr) {
24+
is.null(tr[["x"]]) && is.null(tr[["y"]])
25+
})
26+
has.data <- all.traces[!no.data]
27+
expect_equal(length(has.data), n.traces)
28+
list(traces=has.data, kwargs=L$kwargs)
29+
}
30+
31+
test_that("ylim is respected for 1 trace", {
32+
info <- expect_traces(gg.ylim, 1, "one-trace")
33+
expected.ylim <- c(0, max(df$total_bill))
34+
expect_equal(info$kwargs$layout$yaxis$range, expected.ylim)
35+
})

0 commit comments

Comments
 (0)