Grouping by third attribute fails in bar chart.
Closed this issue · 10 comments
Could you please provide a reproducible example?
For the ggvis graph, if I remove the line with group_by then the graph will show. For the ggplot2 graph:
BarPlot <- ggplot(Plot_Data, aes(x=month, y=nrow, fill=Product)) +
geom_bar(position="dodge", stat="identity") +
geom_bar(position="dodge", stat="identity", colour='black', show_guide=FALSE) +
scale_x_datetime(breaks="1 month", labels=date_format("%B")) +
scale_y_continuous(name="# of Reviews", breaks= c(seq(0, (nrow(Plot_Data)+5), 5)),
labels=c(seq(0, (nrow(Plot_Data)+5), 5))) + theme_bw() +
Can you please reproduce the problem with a built-in dataset?
You can also use dput
to output your data set so that it can be used by others.
Plot_Data <- structure(list(month = structure(c(1391230800, 1391230800, 1391230800,
1391230800, 1391230800, 1393650000, 1393650000, 1393650000, 1393650000,
1396324800, 1396324800, 1396324800, 1396324800, 1398916800, 1398916800,
NA, NA, NA, NA, NA, NA, NA, NA, NA), class = c("POSIXct", "POSIXt"
), tzone = ""), Product = c("Jolly Roger", "Red Mo", "Schwarzbier",
"Slab Cabin", "Spruce Creek", "Golden", "Maibock", "Red Mo",
"Slab Cabin", "Hefeweizen", "Maibock", "Red Mo", "Tailgater",
"Maibock", "Pallet Jack", NA, NA, NA, NA, NA, NA, NA, NA, NA),
nrow = c(1L, 6L, 7L, 8L, 3L, 8L, 11L, 6L, 7L, 6L, 9L, 12L,
1L, 3L, 9L, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("month",
"Product", "nrow"), row.names = c("20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "NA",
"NA.1", "NA.2", "NA.3", "NA.4", "NA.5", "NA.6", "NA.7", "NA.8"
), class = "data.frame")
BarPlot <- ggplot(Plot_Data, aes(x=month, y=nrow, fill=Product)) +
geom_bar(position="dodge", stat="identity") +
geom_bar(position="dodge", stat="identity", colour='black', show_guide=FALSE) +
scale_x_datetime(breaks="1 month", labels=date_format("%B")) +
scale_y_continuous(name="# of Reviews", breaks= c(seq(0, (nrow(Plot_Data)+5), 5)),
labels=c(seq(0, (nrow(Plot_Data)+5), 5))) + theme_bw() +
ggtitle("Reviews per Month") +
theme(legend.position="right",
plot.title = element_text(face="bold", size=15),
legend.background = element_rect(fill="transparent"),
legend.title=element_text(size=20, vjust=1, face='plain'),
legend.title.align=.5,
legend.text=element_text(size=15, vjust=1))
print(BarPlot)
ggvis doesn't support grouped (side-by-side) bar graphs yet, though it's coming soon.
There are a few issues with your ggvis code:
- You're trying to use a nonexistent column for
y
. - It should be
group_by(Product)
, notgroup_by(~Product)
@wch Is this still on the roadmap ?
+1. Are there milestone(s) for "dodged and stacked" bars?
A little sad that this conversation is so old and it hasn't been incorporated yet. I really wanted to make a grouped horizontal bar chart, so I came up with a somewhat hacky solution.
The code in my shiny app looks like this:
bar <- data %>%
ggvis(y = ~factor(y_var, levels = sort_order), stroke := "#none", fill = ~fill_var) %>%
group_by(fill_var) %>%
layer_rects(x = 0, x2 = ~x_var, height = band()) %>%
scale_nominal("y", padding = 0.6, points = FALSE) %>%
add_tooltip(bar_tooltip, "hover") %>%
set_options(width = 1150, height = 500) %>%
bind_shiny('bar')
That will layer the rects on top of each other. To offset them, I inspected in the developer tools to find the group id for each bar group, then added the following to my css code:
#g39 {
transform: translate(0, 7.5px);
}
#g40 {
transform: translate(0, -7.5px);
}
In the R viewer the bars are still overlapping, but opening in a browser, the results look like this:
I need this in a big way. This is blocking adoption at several large clients.