From e32273af88f6e8925d9188ae53e886c6c0126720 Mon Sep 17 00:00:00 2001 From: cpsievert Date: Thu, 30 Apr 2015 14:10:20 -0500 Subject: [PATCH 1/6] Avoid layering multiple bars when there should be one --- R/trace_generation.R | 10 +- test-cookbook-bars-and-lines.R | 210 +++++++++++++++++++++++++++++++ tests/testthat/test-ggplot-bar.R | 13 ++ 3 files changed, 230 insertions(+), 3 deletions(-) create mode 100644 test-cookbook-bars-and-lines.R diff --git a/R/trace_generation.R b/R/trace_generation.R index 673e88d80b..de902b19d5 100644 --- a/R/trace_generation.R +++ b/R/trace_generation.R @@ -574,11 +574,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"), + plyr::summarise, count = max(y)) + 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)) diff --git a/test-cookbook-bars-and-lines.R b/test-cookbook-bars-and-lines.R new file mode 100644 index 0000000000..cc1f446046 --- /dev/null +++ b/test-cookbook-bars-and-lines.R @@ -0,0 +1,210 @@ +df <- data.frame(time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")), + total_bill = c(14.89, 17.23)) +# time total_bill +# Lunch 14.89 +# Dinner 17.23 + +# Very basic bar graph +g <- ggplot(data=df, aes(x=time, y=total_bill)) + geom_bar(stat="identity") +save_outputs(g, "bars-and-lines/basic-bar", file_prefix="") + +# Map the time of day to different fill colors. These both have the same result. +g <- ggplot(data=df, aes(x=time, y=total_bill, fill=time)) + geom_bar(stat="identity") +save_outputs(g, "bars-and-lines/basic-bar-fill-colors-1", file_prefix="") +g <- ggplot(data=df, aes(x=time, y=total_bill)) + geom_bar(aes(fill=time), stat="identity") +save_outputs(g, "bars-and-lines/basic-bar-fill-colors-2", file_prefix="") + +# Add a black outline +g <- ggplot(data=df, aes(x=time, y=total_bill, fill=time)) + geom_bar(colour="black", stat="identity") +save_outputs(g, "bars-and-lines/basic-bar-fill-colors-black-outline", file_prefix="") + +# No legend, since the information is redundant +g <- ggplot(data=df, aes(x=time, y=total_bill, fill=time)) + + geom_bar(colour="black", stat="identity") + + guides(fill=FALSE) +save_outputs(g, "bars-and-lines/basic-bar-fill-colors-black-outline-no-legend", file_prefix="") + +library(reshape2) +tips +# total_bill tip sex smoker day time size +# 16.99 1.01 Female No Sun Dinner 2 +# 10.34 1.66 Male No Sun Dinner 3 +# 21.01 3.50 Male No Sun Dinner 3 +# ... <244 total rows> ... +# 22.67 2.00 Male Yes Sat Dinner 2 +# 17.82 1.75 Male No Sat Dinner 2 +# 18.78 3.00 Female No Thur Dinner 2 + +# Bar graph of counts +g <- ggplot(data=tips, aes(x=day)) + geom_bar(stat="bin") +save_outputs(g, "bars-and-lines/bar-graph-of-counts", file_prefix="") + +# Equivalent to this, since stat="bin" is the default: +g <- ggplot(data=tips, aes(x=day)) + geom_bar() +save_outputs(g, "bars-and-lines/bar-graph-of-counts-2", file_prefix="") + +# Basic line graph. These both have the same result. +g <- ggplot(data=df, aes(x=time, y=total_bill, group=1)) + geom_line() +save_outputs(g, "bars-and-lines/basic-line", file_prefix="") +g <- ggplot(data=df, aes(x=time, y=total_bill)) + geom_line(aes(group=1)) +save_outputs(g, "bars-and-lines/basic-line-2", file_prefix="") + +# Add points +g <- ggplot(data=df, aes(x=time, y=total_bill, group=1)) + geom_line() + geom_point() +save_outputs(g, "bars-and-lines/basic-line-with-points", file_prefix="") + + + + + + + + + + + + + + + +# Change color of both line and points +# Change line type and point type, and use thicker line and larger points +# Change points to circles with white fill +g <- ggplot(data=df, aes(x=time, y=total_bill, group=1)) + + geom_line(colour="red", linetype="dotted", size=1.5) + + geom_point(colour="red", size=4, shape=21, fill="white") +save_outputs(g, "bars-and-lines/basic-dashed-line-with-colors", file_prefix="") + + + + + + + + +# Change the y-range to go from 0 to the maximum value in the total_bill column, +# and change axis labels +g <- 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") +save_outputs(g, "bars-and-lines/basic-line-fully-styled", file_prefix="") + +df1 <- data.frame(sex = factor(c("Female","Female","Male","Male")), + time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")), + total_bill = c(13.53, 16.81, 16.24, 17.42)) +# sex time total_bill +# Female Lunch 13.53 +# Female Dinner 16.81 +# Male Lunch 16.24 +# Male Dinner 17.42 + +# Stacked bar graph -- this is probably not what you want +g <- ggplot(data=df1, aes(x=time, y=total_bill, fill=sex)) + geom_bar(stat="identity") +save_outputs(g, "bars-and-lines/multi-var-stacked-bar", file_prefix="") + +# Bar graph, time on x-axis, color fill grouped by sex -- use position_dodge() +g <- ggplot(data=df1, aes(x=time, y=total_bill, fill=sex)) + geom_bar(stat="identity", position=position_dodge()) +save_outputs(g, "bars-and-lines/multi-var-grouped-bar", file_prefix="") +g <- ggplot(data=df1, aes(x=time, y=total_bill, fill=sex)) + geom_bar(stat="identity", position=position_dodge(), colour="black") +save_outputs(g, "bars-and-lines/multi-var-grouped-bar-black-outline", file_prefix="") + +# Change colors +g <- ggplot(data=df1, aes(x=time, y=total_bill, fill=sex)) + geom_bar(stat="identity", position=position_dodge(), colour="black") + + scale_fill_manual(values=c("#999999", "#E69F00")) +save_outputs(g, "bars-and-lines/multi-var-grouped-bar-colored", file_prefix="") + +# Bar graph, time on x-axis, color fill grouped by sex -- use position_dodge() +g <- ggplot(data=df1, aes(x=sex, y=total_bill, fill=time)) + geom_bar(stat="identity", position=position_dodge(), colour="black") +save_outputs(g, "bars-and-lines/multi-var-grouped-bar-reversed-vars", file_prefix="") + +# Basic line graph with points +g <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex)) + geom_line() + geom_point() +save_outputs(g, "bars-and-lines/basic-line-with-points", file_prefix="") + +# Map sex to color +g <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, colour=sex)) + geom_line() + geom_point() +save_outputs(g, "bars-and-lines/basic-line-with-mapped-colors", file_prefix="") + +# Map sex to different point shape, and use larger points +g <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex)) + geom_line() + geom_point() +save_outputs(g, "bars-and-lines/basic-line-with-symbols", file_prefix="") + +# Use thicker lines and larger points, and hollow white-filled points +g <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex)) + + geom_line(size=1.5) + + geom_point(size=3, fill="white") + + scale_shape_manual(values=c(22,21)) +save_outputs(g, "bars-and-lines/basic-line-with-points-fully-styled", file_prefix="") + +g <- ggplot(data=df1, aes(x=sex, y=total_bill, group=time, shape=time, color=time)) + geom_line() + geom_point() +save_outputs(g, "bars-and-lines/basic-line-swapped-vars", file_prefix="") + +# A bar graph +g <- ggplot(data=df1, aes(x=time, y=total_bill, fill=sex)) + + geom_bar(colour="black", stat="identity", + position=position_dodge(), + size=.3) + # Thinner lines + scale_fill_hue(name="Sex of payer") + # Set legend title + xlab("Time of day") + ylab("Total bill") + # Set axis labels + ggtitle("Average bill for 2 people") + # Set title + theme_bw() +save_outputs(g, "bars-and-lines/finished-bar-bw-theme", file_prefix="") + +# A line graph +g <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) + + geom_line(aes(linetype=sex), size=1) + # Set linetype by sex + geom_point(size=3, fill="white") + # Use larger points, fill with white + ylim(0, max(df1$total_bill)) + # Set y range + scale_colour_hue(name="Sex of payer", # Set legend title + l=30) + # Use darker colors (lightness=30) + scale_shape_manual(name="Sex of payer", + values=c(22,21)) + # Use points with a fill color + scale_linetype_discrete(name="Sex of payer") + + xlab("Time of day") + ylab("Total bill") + # Set axis labels + ggtitle("Average bill for 2 people") + # Set title + theme_bw() + + theme(legend.position=c(.7, .4)) # Position legend inside +# This must go after theme_bw +save_outputs(g, "bars-and-lines/finished-line", file_prefix="") + +dfn <- read.table(header=T, text=" + supp dose length + OJ 0.5 13.23 + OJ 1.0 22.70 + OJ 2.0 26.06 + VC 0.5 7.98 + VC 1.0 16.77 + VC 2.0 26.14 + ") + +g <- ggplot(data=dfn, aes(x=dose, y=length, group=supp, colour=supp)) + geom_line() + geom_point() +save_outputs(g, "bars-and-lines/line-continuous-numerical-x-axis", file_prefix="") + +# Copy the data frame and convert dose to a factor +dfn2 <- dfn +dfn2$dose <- factor(dfn2$dose) +g <- ggplot(data=dfn2, aes(x=dose, y=length, group=supp, colour=supp)) + geom_line() + geom_point() +save_outputs(g, "bars-and-lines/line-continuous-categorical-x-axis", file_prefix="") + +# Use the original data frame, but put factor() directly in the plot specification + +## TODO: Uncomment when Plotly supports this +## g <- ggplot(data=dfn, aes(x=factor(dose), y=length, group=supp, colour=supp)) + geom_line() + geom_point() +## save_outputs(g, "bars-and-lines/line-continuous-categorical-x-axis-with-factor", file_prefix="") + +# Use dfn2 from above +g <- ggplot(data=dfn2, aes(x=dose, y=length, fill=supp)) + geom_bar(stat="identity", position=position_dodge()) +save_outputs(g, "bars-and-lines/bar-categorical-numerical-labels", file_prefix="") + +# Use the original data frame, but put factor() directly in the plot specification +g <- ggplot(data=dfn, aes(x=factor(dose), y=length, fill=supp)) + geom_bar(stat="identity", position=position_dodge()) +save_outputs(g, "bars-and-lines/bar-categorical-numerical-labels-with-factor", file_prefix="") + +# Add title, narrower bars, gray fill, and change axis labels +g <- ggplot(data=df, aes(x=time, y=total_bill, fill=time)) + + geom_bar(colour="black", fill="#DD8888", width=.7, stat="identity") + + guides(fill=FALSE) + + xlab("Time of day") + ylab("Total bill") + + ggtitle("Average bill for 2 people") +save_outputs(g, "bars-and-lines/basic-bar-fully-styled", file_prefix="") diff --git a/tests/testthat/test-ggplot-bar.R b/tests/testthat/test-ggplot-bar.R index e4235a8097..07c90617b4 100644 --- a/tests/testthat/test-ggplot-bar.R +++ b/tests/testthat/test-ggplot-bar.R @@ -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)) +}) + + From 3362a6bdd9be74331f9cbe936a89e9773fd7c159 Mon Sep 17 00:00:00 2001 From: cpsievert Date: Thu, 30 Apr 2015 14:14:10 -0500 Subject: [PATCH 2/6] whoops; don't add cookbook tests --- test-cookbook-bars-and-lines.R | 210 ----------------------- tests/testthat/test-ggplot-categorical.R | 14 -- 2 files changed, 224 deletions(-) delete mode 100644 test-cookbook-bars-and-lines.R diff --git a/test-cookbook-bars-and-lines.R b/test-cookbook-bars-and-lines.R deleted file mode 100644 index cc1f446046..0000000000 --- a/test-cookbook-bars-and-lines.R +++ /dev/null @@ -1,210 +0,0 @@ -df <- data.frame(time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")), - total_bill = c(14.89, 17.23)) -# time total_bill -# Lunch 14.89 -# Dinner 17.23 - -# Very basic bar graph -g <- ggplot(data=df, aes(x=time, y=total_bill)) + geom_bar(stat="identity") -save_outputs(g, "bars-and-lines/basic-bar", file_prefix="") - -# Map the time of day to different fill colors. These both have the same result. -g <- ggplot(data=df, aes(x=time, y=total_bill, fill=time)) + geom_bar(stat="identity") -save_outputs(g, "bars-and-lines/basic-bar-fill-colors-1", file_prefix="") -g <- ggplot(data=df, aes(x=time, y=total_bill)) + geom_bar(aes(fill=time), stat="identity") -save_outputs(g, "bars-and-lines/basic-bar-fill-colors-2", file_prefix="") - -# Add a black outline -g <- ggplot(data=df, aes(x=time, y=total_bill, fill=time)) + geom_bar(colour="black", stat="identity") -save_outputs(g, "bars-and-lines/basic-bar-fill-colors-black-outline", file_prefix="") - -# No legend, since the information is redundant -g <- ggplot(data=df, aes(x=time, y=total_bill, fill=time)) + - geom_bar(colour="black", stat="identity") + - guides(fill=FALSE) -save_outputs(g, "bars-and-lines/basic-bar-fill-colors-black-outline-no-legend", file_prefix="") - -library(reshape2) -tips -# total_bill tip sex smoker day time size -# 16.99 1.01 Female No Sun Dinner 2 -# 10.34 1.66 Male No Sun Dinner 3 -# 21.01 3.50 Male No Sun Dinner 3 -# ... <244 total rows> ... -# 22.67 2.00 Male Yes Sat Dinner 2 -# 17.82 1.75 Male No Sat Dinner 2 -# 18.78 3.00 Female No Thur Dinner 2 - -# Bar graph of counts -g <- ggplot(data=tips, aes(x=day)) + geom_bar(stat="bin") -save_outputs(g, "bars-and-lines/bar-graph-of-counts", file_prefix="") - -# Equivalent to this, since stat="bin" is the default: -g <- ggplot(data=tips, aes(x=day)) + geom_bar() -save_outputs(g, "bars-and-lines/bar-graph-of-counts-2", file_prefix="") - -# Basic line graph. These both have the same result. -g <- ggplot(data=df, aes(x=time, y=total_bill, group=1)) + geom_line() -save_outputs(g, "bars-and-lines/basic-line", file_prefix="") -g <- ggplot(data=df, aes(x=time, y=total_bill)) + geom_line(aes(group=1)) -save_outputs(g, "bars-and-lines/basic-line-2", file_prefix="") - -# Add points -g <- ggplot(data=df, aes(x=time, y=total_bill, group=1)) + geom_line() + geom_point() -save_outputs(g, "bars-and-lines/basic-line-with-points", file_prefix="") - - - - - - - - - - - - - - - -# Change color of both line and points -# Change line type and point type, and use thicker line and larger points -# Change points to circles with white fill -g <- ggplot(data=df, aes(x=time, y=total_bill, group=1)) + - geom_line(colour="red", linetype="dotted", size=1.5) + - geom_point(colour="red", size=4, shape=21, fill="white") -save_outputs(g, "bars-and-lines/basic-dashed-line-with-colors", file_prefix="") - - - - - - - - -# Change the y-range to go from 0 to the maximum value in the total_bill column, -# and change axis labels -g <- 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") -save_outputs(g, "bars-and-lines/basic-line-fully-styled", file_prefix="") - -df1 <- data.frame(sex = factor(c("Female","Female","Male","Male")), - time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")), - total_bill = c(13.53, 16.81, 16.24, 17.42)) -# sex time total_bill -# Female Lunch 13.53 -# Female Dinner 16.81 -# Male Lunch 16.24 -# Male Dinner 17.42 - -# Stacked bar graph -- this is probably not what you want -g <- ggplot(data=df1, aes(x=time, y=total_bill, fill=sex)) + geom_bar(stat="identity") -save_outputs(g, "bars-and-lines/multi-var-stacked-bar", file_prefix="") - -# Bar graph, time on x-axis, color fill grouped by sex -- use position_dodge() -g <- ggplot(data=df1, aes(x=time, y=total_bill, fill=sex)) + geom_bar(stat="identity", position=position_dodge()) -save_outputs(g, "bars-and-lines/multi-var-grouped-bar", file_prefix="") -g <- ggplot(data=df1, aes(x=time, y=total_bill, fill=sex)) + geom_bar(stat="identity", position=position_dodge(), colour="black") -save_outputs(g, "bars-and-lines/multi-var-grouped-bar-black-outline", file_prefix="") - -# Change colors -g <- ggplot(data=df1, aes(x=time, y=total_bill, fill=sex)) + geom_bar(stat="identity", position=position_dodge(), colour="black") + - scale_fill_manual(values=c("#999999", "#E69F00")) -save_outputs(g, "bars-and-lines/multi-var-grouped-bar-colored", file_prefix="") - -# Bar graph, time on x-axis, color fill grouped by sex -- use position_dodge() -g <- ggplot(data=df1, aes(x=sex, y=total_bill, fill=time)) + geom_bar(stat="identity", position=position_dodge(), colour="black") -save_outputs(g, "bars-and-lines/multi-var-grouped-bar-reversed-vars", file_prefix="") - -# Basic line graph with points -g <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex)) + geom_line() + geom_point() -save_outputs(g, "bars-and-lines/basic-line-with-points", file_prefix="") - -# Map sex to color -g <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, colour=sex)) + geom_line() + geom_point() -save_outputs(g, "bars-and-lines/basic-line-with-mapped-colors", file_prefix="") - -# Map sex to different point shape, and use larger points -g <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex)) + geom_line() + geom_point() -save_outputs(g, "bars-and-lines/basic-line-with-symbols", file_prefix="") - -# Use thicker lines and larger points, and hollow white-filled points -g <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex)) + - geom_line(size=1.5) + - geom_point(size=3, fill="white") + - scale_shape_manual(values=c(22,21)) -save_outputs(g, "bars-and-lines/basic-line-with-points-fully-styled", file_prefix="") - -g <- ggplot(data=df1, aes(x=sex, y=total_bill, group=time, shape=time, color=time)) + geom_line() + geom_point() -save_outputs(g, "bars-and-lines/basic-line-swapped-vars", file_prefix="") - -# A bar graph -g <- ggplot(data=df1, aes(x=time, y=total_bill, fill=sex)) + - geom_bar(colour="black", stat="identity", - position=position_dodge(), - size=.3) + # Thinner lines - scale_fill_hue(name="Sex of payer") + # Set legend title - xlab("Time of day") + ylab("Total bill") + # Set axis labels - ggtitle("Average bill for 2 people") + # Set title - theme_bw() -save_outputs(g, "bars-and-lines/finished-bar-bw-theme", file_prefix="") - -# A line graph -g <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) + - geom_line(aes(linetype=sex), size=1) + # Set linetype by sex - geom_point(size=3, fill="white") + # Use larger points, fill with white - ylim(0, max(df1$total_bill)) + # Set y range - scale_colour_hue(name="Sex of payer", # Set legend title - l=30) + # Use darker colors (lightness=30) - scale_shape_manual(name="Sex of payer", - values=c(22,21)) + # Use points with a fill color - scale_linetype_discrete(name="Sex of payer") + - xlab("Time of day") + ylab("Total bill") + # Set axis labels - ggtitle("Average bill for 2 people") + # Set title - theme_bw() + - theme(legend.position=c(.7, .4)) # Position legend inside -# This must go after theme_bw -save_outputs(g, "bars-and-lines/finished-line", file_prefix="") - -dfn <- read.table(header=T, text=" - supp dose length - OJ 0.5 13.23 - OJ 1.0 22.70 - OJ 2.0 26.06 - VC 0.5 7.98 - VC 1.0 16.77 - VC 2.0 26.14 - ") - -g <- ggplot(data=dfn, aes(x=dose, y=length, group=supp, colour=supp)) + geom_line() + geom_point() -save_outputs(g, "bars-and-lines/line-continuous-numerical-x-axis", file_prefix="") - -# Copy the data frame and convert dose to a factor -dfn2 <- dfn -dfn2$dose <- factor(dfn2$dose) -g <- ggplot(data=dfn2, aes(x=dose, y=length, group=supp, colour=supp)) + geom_line() + geom_point() -save_outputs(g, "bars-and-lines/line-continuous-categorical-x-axis", file_prefix="") - -# Use the original data frame, but put factor() directly in the plot specification - -## TODO: Uncomment when Plotly supports this -## g <- ggplot(data=dfn, aes(x=factor(dose), y=length, group=supp, colour=supp)) + geom_line() + geom_point() -## save_outputs(g, "bars-and-lines/line-continuous-categorical-x-axis-with-factor", file_prefix="") - -# Use dfn2 from above -g <- ggplot(data=dfn2, aes(x=dose, y=length, fill=supp)) + geom_bar(stat="identity", position=position_dodge()) -save_outputs(g, "bars-and-lines/bar-categorical-numerical-labels", file_prefix="") - -# Use the original data frame, but put factor() directly in the plot specification -g <- ggplot(data=dfn, aes(x=factor(dose), y=length, fill=supp)) + geom_bar(stat="identity", position=position_dodge()) -save_outputs(g, "bars-and-lines/bar-categorical-numerical-labels-with-factor", file_prefix="") - -# Add title, narrower bars, gray fill, and change axis labels -g <- ggplot(data=df, aes(x=time, y=total_bill, fill=time)) + - geom_bar(colour="black", fill="#DD8888", width=.7, stat="identity") + - guides(fill=FALSE) + - xlab("Time of day") + ylab("Total bill") + - ggtitle("Average bill for 2 people") -save_outputs(g, "bars-and-lines/basic-bar-fully-styled", file_prefix="") diff --git a/tests/testthat/test-ggplot-categorical.R b/tests/testthat/test-ggplot-categorical.R index 01ca967c8d..284ac067b9 100644 --- a/tests/testthat/test-ggplot-categorical.R +++ b/tests/testthat/test-ggplot-categorical.R @@ -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), 2) # 1 trace + layout - expect_identical(info$kwargs$layout$xaxis$type, "category") - expect_identical(info$kwargs$layout$xaxis$title, "cut") - expect_identical(info$kwargs$layout$yaxis$type, "linear") - expect_true(all(c("Fair", "Good", "Very Good", "Premium", "Ideal") %in% - info[[1]]$x)) - - save_outputs(gbar, "bar-category-names") -}) From a026b9bb13c20467199c3b422705d9ac89165576 Mon Sep 17 00:00:00 2001 From: cpsievert Date: Tue, 19 May 2015 18:30:00 -0500 Subject: [PATCH 3/6] Map text if no data reduction occurred --- R/trace_generation.R | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/R/trace_generation.R b/R/trace_generation.R index 459c7d4169..d1056ac517 100644 --- a/R/trace_generation.R +++ b/R/trace_generation.R @@ -579,18 +579,20 @@ geom2trace <- list( # Convert days into milliseconds x <- as.numeric(x) * 24 * 60 * 60 * 1000 } - # if there is more than one y-value for a particular combination of + # 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"), + dat <- plyr::ddply(data, c("x", "PANEL", if ("group" %in% names(data)) "group"), plyr::summarise, count = max(y)) - L <- list(x=dat$x, - y=dat$count, - type="bar", - name=params$name, - marker=list(color=toRGB(params$fill))) + L <- list(x = dat$x, + y = dat$count, + type = "bar", + # text only makes sense if no dimension reduction occurred + text = if (nrow(dat) == nrow(data)) data$text else NULL, + name = params$name, + marker = list(color=toRGB(params$fill))) if (!is.null(params$colour)) { - L$marker$line <- list(color=toRGB(params$colour)) + L$marker$line <- list(color = toRGB(params$colour)) L$marker$line$width <- if (is.null(params$size)) 1 else params$size } if (!is.null(params$alpha)) L$opacity <- params$alpha From 4e003437a78346a9b812aef9b233860afc8978f1 Mon Sep 17 00:00:00 2001 From: cpsievert Date: Tue, 19 May 2015 18:34:01 -0500 Subject: [PATCH 4/6] moar space --- R/trace_generation.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/trace_generation.R b/R/trace_generation.R index d1056ac517..7f448dd66e 100644 --- a/R/trace_generation.R +++ b/R/trace_generation.R @@ -590,7 +590,7 @@ geom2trace <- list( # text only makes sense if no dimension reduction occurred text = if (nrow(dat) == nrow(data)) data$text else NULL, name = params$name, - marker = list(color=toRGB(params$fill))) + marker = list(color = toRGB(params$fill))) if (!is.null(params$colour)) { L$marker$line <- list(color = toRGB(params$colour)) L$marker$line$width <- if (is.null(params$size)) 1 else params$size From d861518869c0f9d778e84208fcfeca4b55f95c4f Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Tue, 19 May 2015 20:36:22 -0400 Subject: [PATCH 5/6] Remove 2 extra lines at end of file --- tests/testthat/test-ggplot-bar.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/testthat/test-ggplot-bar.R b/tests/testthat/test-ggplot-bar.R index 7ce80b57da..7c308375d0 100644 --- a/tests/testthat/test-ggplot-bar.R +++ b/tests/testthat/test-ggplot-bar.R @@ -202,5 +202,3 @@ test_that("For a given x value, if multiple y exist, sum them. ", { y <- y[info$traces[[1]]$x] expect_equal(info$traces[[1]]$y, as.numeric(y)) }) - - From edf055bc51a85f72d30a261115adfeff3d8ce4d3 Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Tue, 19 May 2015 20:41:44 -0400 Subject: [PATCH 6/6] Increment version number --- DESCRIPTION | 2 +- NEWS | 4 ++++ R/plotly-package.r | 2 +- R/plotly.R | 2 +- man/plotly-package.Rd | 2 +- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 041cd58a3f..6085e4fcf3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: plotly Type: Package Title: Interactive, publication-quality graphs online. -Version: 0.6.1 +Version: 0.6.2 Authors@R: c(person("Chris", "Parmer", role = c("aut", "cre"), email = "chris@plot.ly"), person("Scott", "Chamberlain", role = "aut", diff --git a/NEWS b/NEWS index 90e3149faf..b507652938 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +0.6.2 -- 19 May 2015 + +In geom_bar(stat = "identity"), sum y values if multiple for a given x. + 0.6.1 -- 5 May 2015 Add test-cookbook-lines.R and fix bugs that showed up in those tests. diff --git a/R/plotly-package.r b/R/plotly-package.r index bbd16326b3..8b9f042522 100644 --- a/R/plotly-package.r +++ b/R/plotly-package.r @@ -7,7 +7,7 @@ #' \itemize{ #' \item Package: plotly #' \item Type: Package -#' \item Version: 0.6.1 +#' \item Version: 0.6.2 #' \item Date: 2014-03-07 #' \item License: MIT #' } diff --git a/R/plotly.R b/R/plotly.R index 5728d656e8..d1ba1e3b60 100644 --- a/R/plotly.R +++ b/R/plotly.R @@ -82,7 +82,7 @@ For more help, see https://plot.ly/R or contact .") # public attributes/methods that the user has access to pub <- list(username=username, key=key, filename="from api", fileopt=NULL, - version="0.6.1") + version="0.6.2") priv <- list() pub$makecall <- function(args, kwargs, origin) { diff --git a/man/plotly-package.Rd b/man/plotly-package.Rd index 69f0c0cde6..ce7fff10e9 100644 --- a/man/plotly-package.Rd +++ b/man/plotly-package.Rd @@ -15,7 +15,7 @@ An example of an interactive graph made from the R API: https://plot.ly/~chris/4 \itemize{ \item Package: plotly \item Type: Package - \item Version: 0.6.1 + \item Version: 0.6.2 \item Date: 2014-03-07 \item License: MIT }