Set X Axis Range on Flat Frequency Plot

Consider the following graph:

library(dplyr)
library(plotly)

x <- c(1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 4, 5, 6, 8, 8, 8, 10, 10)
y <- as.data.frame(table(x))

y %>%
plot_ly(x = ~ x,
        y = ~ Freq,
        type = 'bar')

      

Frequency graph

I would like to take this graph and create a similar graph in which the values ​​7 and 9 are listed with a frequency of zero. Is there a way to get the frequency number of a sequence such as seq(0, 10, 1)

where 7 and 9 will display as frequency 0 or is there a way that I can set the x-axis in my graph plot to be from 0 to 10 even though I don't have all the digits in mine data?

I tried

y %>%
plot_ly(x = ~ x,
        y = ~ Freq,
        type = 'bar') %>%
layout(xaxis = list(autotick = FALSE, tick0 = 0, tickd = seq(0, 10, 1))

      

and

layout(xaxis = list(autotick = FALSE, tick0 = 0, tickd = c(0,10))

      

but none of them will change anything.

I would like my desired output to look like this:

Desired result

Please note, this is just a small sample and my actual data will be much larger. Because of this, something like scrolling through the data and counting each number would be too slow.

+3


source to share


1 answer


An easy solution is to convert x

as a factor with levels from 1 to 10.



library(dplyr)
library(plotly)

x <- c(1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 4, 5, 6, 8, 8, 8, 10, 10)
x <- factor(x, levels=1:10)

y <- as.data.frame(table(x))

y %>%
plot_ly(x = ~ x,
        y = ~ Freq,
        type = 'bar')

      

+3


source







All Articles