1
votes

I'm trying to overlay two histogram plots in R plotly. However only one of them shows up. Here's the code I'm using with some random data:

    myDF <- cbind.data.frame(Income = sample(1:9, size = 1000, replace= TRUE),
                           AgeInTwoYearIncrements = sample(seq(from = 2, to = 70, by = 2), size = 1000, replace = TRUE))


plot_ly(data = myDF, alpha = 0.6) %>% 
  add_histogram(x = ~Income, yaxis = "y1") %>% 
  add_histogram(x = ~AgeInTwoYearIncrements, yaxis = "y2") %>% 
  layout(
    title = "Salary vs Age",
    yaxis = list(
      tickfont = list(color = "blue"),
      overlaying = "y",
      side = "left",
      title = "Income"
    ),
    yaxis2 = list(
      tickfont = list(color = "red"),
      overlaying = "y",
      side = "right",
      title = "Age"
    ),
    xaxis = list(title = "count")
  )

Any help would be much appreciated!

3

3 Answers

4
votes

It is the main cause to give the 1st yaxis overlaying. And because xaxis is count, Income and Age is y.

plot_ly(data = myDF, alpha = 0.6) %>% 
  add_histogram(y = ~Income, yaxis = "y1") %>%    # not `x =`
  add_histogram(y = ~AgeInTwoYearIncrements, yaxis = "y2") %>% 
  layout(
    title = "Salary vs Age",
    yaxis = list(
      tickfont = list(color = "blue"),
      # overlaying = "y",     # the main cause is this line.
      side = "left",
      title = "Income"
    ),
    yaxis2 = list(
      tickfont = list(color = "red"),
      overlaying = "y",
      side = "right",
      title = "Age"
    ),
    xaxis = list(title = "count")
  )

enter image description here

plot_ly(data = myDF, alpha = 0.6) %>% 
  add_histogram(x = ~ Income, xaxis = "x1") %>% 
  add_histogram(x = ~ AgeInTwoYearIncrements, xaxis = "x2") %>% 
  layout(
    margin = list(t = 60),
    title = "Salary vs Age",
    xaxis = list(
      tickfont = list(color = "blue"),
      side = "left",
      title = "Income"
    ),
    xaxis2 = list(
      tickfont = list(color = "red"),
      overlaying = "x",
      side = "top",
      position = 0.95,
      title = "<br>Age"
    ),
    yaxis = list(title = "count")
  )

enter image description here

2
votes

You can mix histograms:

enter image description here

plot_ly(data = myDF, alpha = 0.6) %>% 
  add_histogram(x = ~Income) %>%
  add_histogram(x = ~AgeInTwoYearIncrements) %>%
layout(
  title = "Salary and Age",
  yaxis = list(
    tickfont = list(color = "blue"),
    overlaying = "y",
    side = "left",
    title = "count"
  ),
  xaxis = list(title = "Salary and Age value")
)

A histogram has normally on the y-axis the frequency / count and not on the x-axis. We can produce a diagram like you want but I'm not sure if it is still a histogram.

Also, like you see in my picture you the frequency/count for salary (here blue) is more high and the variability is less then age. That make it difficult for a good looking diagram. Maybe this is just a problem of your sample data...

So When you like to go with the histogram function, you have to invert the meaning of the frequency and the value on the x-axis.

But anyway, I think a scaternplot would be a better solution to show the relation between salary and age.

edit:

This is the result I get when I run your code:

enter image description here

Like this I don't see the sense in the plot and what you want. The meaning of the first orange colum is that a age of 59 occurs between 0 and 5 times in your dataset. The third colum means a age of 88 ocours between 10 and 15 times in your dataset. To present this information in a barplot don't work. Because you can have several Age-values in on categorie of counts...I hope this is clear.

Anyway, to answer your question I need more clarification.

0
votes

Following the responses here, I wanted to answer this with an example that others can easily use when for instance plotting two overlapping histograms.

# Add required packages
library(plotly)    

# Make some sample data
a = rnorm(1000,4)
b = rnorm(1000,6)

# Make your histogram plot with binsize set automatically 
fig <- plot_ly(alpha = 0.6) # don't need "nbinsx = 30" 
fig <- fig %>% add_histogram(a, name = "first")
fig <- fig %>% add_histogram(b, name = "second")
fig <- fig %>% layout(barmode = "overlay", 
                      yaxis = list(title = "Frequency"),
                      xaxis = list(title = "Values"))

# Print your histogram 
fig

And here is the result of the code: Finished histogram