Skip to content

Toby ylim #171

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 5 commits into from
Mar 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: plotly
Type: Package
Title: Interactive, publication-quality graphs online.
Version: 0.5.21
Version: 0.5.22
Authors@R: c(person("Chris", "Parmer", role = c("aut", "cre"),
email = "[email protected]"),
person("Scott", "Chamberlain", role = "aut",
Expand Down
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.5.22 -- 2 March 2015.

Fixes for ylim() #171.

0.5.21 -- 23 February 2015.

Fixes for error bars and tick marks.
Expand Down
30 changes: 17 additions & 13 deletions R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,28 @@ gg2list <- function(p){
ax.list$tickangle <- -tick.text$angle
}
ax.list$tickfont <- theme2font(tick.text)

## determine axis type first, since this information is used later
## (trace.order.list is only used for type=category).
title.text <- e(s("axis.title.%s"))
ax.list$titlefont <- theme2font(title.text)
ax.list$type <- if (misc$is.continuous[[xy]]){
"linear"
} else if (misc$is.discrete[[xy]]){
"category"
} else if (misc$is.date[[xy]] || misc$is.datetime[[xy]]){
"date"
} else {
stop("unrecognized data type for ", xy, " axis")
}
Copy link
Member

Choose a reason for hiding this comment

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

💄 - spaces around if:
image, space between } and else


# Translate axes labels.
scale.i <- which(p$scales$find(xy))
ax.list$title <- if(length(scale.i)){
sc <- p$scales$scales[[scale.i]]
trace.order.list[[xy]] <- sc$limits
if(ax.list$type == "category"){
trace.order.list[[xy]] <- sc$limits
}
trace.name.map[sc$breaks] <- sc$labels
if (is.null(sc$breaks)) {
ax.list$showticklabels <- FALSE
Expand Down Expand Up @@ -346,18 +362,6 @@ gg2list <- function(p){
p$labels[[xy]]
}

title.text <- e(s("axis.title.%s"))
ax.list$titlefont <- theme2font(title.text)
ax.list$type <- if(misc$is.continuous[[xy]]){
"linear"
}else if(misc$is.discrete[[xy]]){
"category"
}else if(misc$is.date[[xy]] || misc$is.datetime[[xy]]){
"date"
}else{
stop("unrecognized data type for ", xy, " axis")
}

ax.list$zeroline <- FALSE # ggplot2 plots do not show zero lines
# Lines drawn around the plot border.
ax.list$showline <- !is.blank("panel.border", TRUE)
Expand Down
35 changes: 35 additions & 0 deletions tests/testthat/test-ggplot-ylim.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
context("ggplot ylim")

## http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_%28ggplot2%29/

df <- data.frame(time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23))

gg.ylim <-
ggplot(data=df, aes(x=time, y=total_bill, group=1)) +
geom_line() +
geom_point() +
ylim(0, max(df$total_bill)) +
xlab("Time of day") + ylab("Total bill") +
ggtitle("Average bill for 2 people")

expect_traces <- function(gg, n.traces, name){
stopifnot(is.ggplot(gg))
stopifnot(is.numeric(n.traces))
save_outputs(gg, paste0("ylim-", name))
L <- gg2list(gg)
is.trace <- names(L) == ""
all.traces <- L[is.trace]
no.data <- sapply(all.traces, function(tr) {
is.null(tr[["x"]]) && is.null(tr[["y"]])
})
has.data <- all.traces[!no.data]
expect_equal(length(has.data), n.traces)
list(traces=has.data, kwargs=L$kwargs)
}

test_that("ylim is respected for 1 trace", {
info <- expect_traces(gg.ylim, 1, "one-trace")
expected.ylim <- c(0, max(df$total_bill))
expect_equal(info$kwargs$layout$yaxis$range, expected.ylim)
})