Skip to content

Avoid layering multiple bars when there should be one #213

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 7 commits into from
May 21, 2015
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 7 additions & 3 deletions R/trace_generation.R
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,15 @@ geom2trace <- list(
# Convert days into milliseconds
x <- as.numeric(x) * 24 * 60 * 60 * 1000
}
L <- list(x=x,
y=data$y,
# if there is more than one y-value for a particular combination of
# x, PANEL, and group; then take the _max_ y.
data$x <- x
dat <- plyr::ddply(data, c("x", "PANEL", if("group"%in%names(data))"group"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Whitespaces in this conditional statement, please.

plyr::summarise, count = max(y))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not? I know this is not native R/ggplot2, as it won't render in the static ggplot version, but let's make use of this field for interesting hover info in the interactive plotly version! (Remember this email where I replied to a user with a link to your https://cpsievert.shinyapps.io/ggtree/?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was being overly paranoid since it won't make sense unless stat="identity", but it looks as though data$text is NULL whenever the stat is not the identity (my bad).

However, even when stat="identity", a text aesthetic still might not make much sense (for example, the bar-category-names test), so in a026b9b, I added an extra check.

(BTW, this link should be more stable -- https://gallery.shinyapps.io/ggtree/ )

L <- list(x=dat$x,
y=dat$count,
type="bar",
name=params$name,
text=data$text,
marker=list(color=toRGB(params$fill)))
if (!is.null(params$colour)) {
L$marker$line <- list(color=toRGB(params$colour))
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-ggplot-bar.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,16 @@ test_that("geom_bar(position = 'fill') stacks proportions", {
expect_identical(prop, 1)
})

d <- head(diamonds, 50)
gbar <- ggplot(d, aes(cut, price)) + geom_bar(stat = "identity")

test_that("For a given x value, if multiple y exist, sum them. ", {
info <- expect_traces(gbar, 1, "category-names")
expect_identical(info$traces[[1]]$type, "bar")
y <- with(d, tapply(price, cut, sum))
# make sure order of counts match
y <- y[info$traces[[1]]$x]
expect_equal(info$traces[[1]]$y, as.numeric(y))
})


14 changes: 0 additions & 14 deletions tests/testthat/test-ggplot-categorical.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,3 @@ test_that("axis type=category when we plot factors", {

save_outputs(gg, "bar-factor-category")
})

test_that("Bar charts of type=category show category names", {
gbar <- ggplot(d, aes(cut, price)) + geom_bar(stat="identity")
info <- gg2list(gbar)

expect_equal(length(info$data), 1) # 1 trace
expect_identical(info$layout$xaxis$type, "category")
expect_identical(info$layout$xaxis$title, "cut")
expect_identical(info$layout$yaxis$type, "linear")
expect_true(all(c("Fair", "Good", "Very Good", "Premium", "Ideal") %in%
info$data[[1]]$x))

save_outputs(gbar, "bar-category-names")
})