Re: [R] Plot_ly bar plots - bars span sevral x values when there are missing values.
Hi Rasmus, thanks for that suggestion. It does indeed fix the column widths in this example. the reason it does this is becuase it means that the additonal 0 values added result in values for two adjacent columns. so if adjacent columns are present the "correct" column width is maintained df[c(3,4),]%>%plot_ly( x = ~ x, y = ~ y, type = "bar")%>% layout(xaxis = list(range = c(0,20)), title = "adjcant x values get correct column width") however if there are not tow adjacent columns then the width increases df[c(3,5),]%>%plot_ly( x = ~ x, y = ~ y, type = "bar")%>% layout(xaxis = list(range = c(0,20)), title = "gap between x values column width changes") given this I'm still not sure why the single value example produces a correct column width. for now the generic work around is to pad with y=0 as you suggested. On Wed, 10 Mar 2021 at 05:58, Rasmus Liland wrote: > Dear Nevil, > > Although I am a bit unfamiliar with > plotly, it seems it is possible to plot > two bars side by side at least: > > h <- df[3:4,] > p <- plotly::plot_ly( > data = h, > x = ~ x, > y = ~ y, > type = "bar") > p <- plotly::layout(p=p, > xaxis = list(range = c(0,20)), > title = "example 4 two bars side by side") > plotly::orca(p=p, file="amos4.png") > > Thus, if you want to only plot x=3 and > x=11 you need to set y=0 when x=4:10: > > h <- df[c(3, 11),] > h <- rbind(h, cbind(x=4:10, y=0)) > p <- plotly::plot_ly( > data = h, > x = ~ x, > y = ~ y, > type = "bar") > p <- plotly::layout(p=p, > xaxis = list(range = c(0,20)), > title = "example 2 corrected") > plotly::orca(p=p, file="amos2.png") > > I think this has to do with the xaxis > option in plotly::layout there, and not > with the bar width. > > Best, > Rasmus > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Plot_ly bar plots - bars span sevral x values when there are missing values.
Dear Nevil, Although I am a bit unfamiliar with plotly, it seems it is possible to plot two bars side by side at least: h <- df[3:4,] p <- plotly::plot_ly( data = h, x = ~ x, y = ~ y, type = "bar") p <- plotly::layout(p=p, xaxis = list(range = c(0,20)), title = "example 4 two bars side by side") plotly::orca(p=p, file="amos4.png") Thus, if you want to only plot x=3 and x=11 you need to set y=0 when x=4:10: h <- df[c(3, 11),] h <- rbind(h, cbind(x=4:10, y=0)) p <- plotly::plot_ly( data = h, x = ~ x, y = ~ y, type = "bar") p <- plotly::layout(p=p, xaxis = list(range = c(0,20)), title = "example 2 corrected") plotly::orca(p=p, file="amos2.png") I think this has to do with the xaxis option in plotly::layout there, and not with the bar width. Best, Rasmus signature.asc Description: PGP signature __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Plot_ly bar plots - bars span sevral x values when there are missing values.
I need to plot a number of bar charts as part of a shiny app. I am using ploty ly x and y values are both numeric x being consecutive integer values. there are often missing values in the data I wish to plot - each plot needs to have a fixed x range. When there are more than a few missing values the bars for the remaining values are being plotted much wider spanning several x values ( see example 2) curiously when there is a single remaining value the bar width is correct (example 3) how can I maintain a constant bar width regardless of number of missing values? I cannot find a bar-width setting. df<-data.frame(x=1:20,y=rnorm(20)^2) #twenty bars each centred above a single x value: df%>%plot_ly( x = ~ x, y = ~ y, type = "bar")%>% layout(xaxis = list(range = c(0,20)), title = "example 1") # this produces wide bars first one spread x= 0-6 second x= 8-14 # expected two column same width as in example 1 centred above x=3 and x=11 df[c(3,11),]%>%plot_ly( x = ~ x, y = ~ y, type = "bar")%>% layout(xaxis = list(range = c(0,20)), title = "example 2 column width expands across multiple x values") # when only a single bar is present it is again the correct width - in this case centred above x=3 df[3,]%>%plot_ly( x = ~ x, y = ~ y, type = "bar")%>% layout(xaxis = list(range = c(0,20)), title = "example 3 correct columns again") [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.