-
Notifications
You must be signed in to change notification settings - Fork 633
subplot: problems with stacked histograms bins and legends when few datapoints #1456
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
Comments
UPDATE 2:I found a solution, which is to add the # Using cut I get a factor with all the bins:
intervals <- cut(log10(di$price), breaks = h$breaks)
# This table is used to join with the data, latter:
mid_int <- tibble(mids = h$mids,
ints = levels(intervals) %>% factor)
plot_data <- di %>%
mutate(ints = intervals) %>%
count(cut, color, ints) %>%
complete(cut, color, ints, fill = list(n = 0)) %>%
left_join(mid_int) %>% # Join to include mids into plot_data
select(cut, color, ints, mids, n) # Reorder columns Now Also, when using the hovermode = "compare" option, it shows data for bars that are not even there (ie: height == 0; this is also the default behaviour of the ("Conteo" means the count for each bar.) |
Interesting, thanks for the thorough report! I think this could be considered a plotly.js bug, but the problem can be fixed by explicitly setting https://plot.ly/r/reference/#bar-width to 1/2 the bin width you desire. In other words, change add_bars() %>% to add_bars(width = 0.025) %>% and believe the problem goes away (plus, you won't need the |
Hi! I've tried your suggestion and it works for the anomalous bin width, but it messes up the legend: Seems like it has to do with the data split that I made to do the multiple plots: it just uses the legend for the first plot/chunk of data, which has only a sample of the whole set of categories. Edit: sorry I clicked the 'close issue' button by mistake :s |
Closing since the legend ordering is a separate issue |
What I want to do
Hello! I'm currently creating a shiny app using
plotly
to show histograms of my data. My intention is to emulate theggplot2
functionfacet_grid
with stacked histograms. With enough data, the code seems to work correctly, but when the data is scarce, some strange things start to happen. Specifically, the size of the bins gets distorted and the legend has fewer categories than what it should.Here's a quick comparison of 3 outputs:
ggplot
andplotly
respectively, using the same 50 rows of data in both cases. The latter has the aforementioned problems.plotly
(using the same code as the middle figure), using 1000 rows of data. This one seems OK.Full-res image here.
Reprex
(not made with reprex package)
The following code sets up the R session and the data, including the amount of datapoints needed to recreate the figures (change to size = 1000 to get the one on the right):
My goal is to reproduce plot on the left (created with the code below), w/o using the
ggplotly
function (mainly because of this).Manipulating the data for
plot_ly
First I need to manually set the breaks and counts for the histogram bins (the
type = "log"
option for the xaxis has some problems). To do this I use thehist
andcut
functions:Finally I calculate de counts for all the bins, using all combinations of the columns cut, color and midpoints (note that I group by
intervals
too, which coincide with the grouping made withmidpoints
; the goal is to have theintervals
for hovermode info):Setting up the plotting parameters
Here I set up the
plot_ly
parameters. First I set a common range for the y axis (yrange
):Then I set up both axis, which are lists. The first part involves calculating the breaks for x axis, using functions from the
scales
package (log10_trans
andnumber
):Creating the plot objects
In this part I split the data by the column
color
and then I create a list to store the plots. I also made theplotFun
as a wrapper for theplotly
commands for the sake of cleanlyness.Finally I excecute the
plot_ly
commands using a loop, with an iteration for each "color" category in the dataset:The (sub)plot:
print(sp)
UPDATE
Found a hacky solution to this, check my following comment.
The text was updated successfully, but these errors were encountered: