在R图上叠加两个直方图

在R图上叠加两个直方图,r,histogram,plotly,R,Histogram,Plotly,我试图在R图上叠加两个直方图。然而,他们中只有一个出现了。下面是我对一些随机数据使用的代码: 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))

我试图在R图上叠加两个直方图。然而,他们中只有一个出现了。下面是我对一些随机数据使用的代码:

    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")
  )
myDF%
加上直方图(x=~收入,yaxis=“y1”)%>%
添加直方图(x=~AGEINTWOYEAR增量,yaxis=“y2”)%>%
布局(
title=“工资与年龄”,
yaxis=列表(
tickfont=list(color=“blue”),
叠加=“y”,
side=“left”,
title=“收入”
),
yaxis2=列表(
tickfont=list(color=“red”),
叠加=“y”,
side=“right”,
title=“年龄”
),
xaxis=列表(title=“count”)
)

任何帮助都将不胜感激

这是产生第一个yaxis
叠加的主要原因。因为
xaxis
count
Income
Age
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")
  )

[编辑:只需翻转]
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")
  )
plot\u ly(数据=myDF,alpha=0.6)%>%
加上直方图(x=~Income,xaxis=“x1”)%>%
添加直方图(x=~ageIntwoyear增量,xaxis=“x2”)%>%
布局(
余量=列表(t=60),
title=“工资与年龄”,
xaxis=列表(
tickfont=list(color=“blue”),
side=“left”,
title=“收入”
),
xaxis2=列表(
tickfont=list(color=“red”),
覆盖=“x”,
side=“top”,
位置=0.95,
title=“
年龄” ), yaxis=列表(title=“count”) )

这是产生第一个yaxis
重叠的主要原因。因为
xaxis
count
Income
Age
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")
  )

[编辑:只需翻转]
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")
  )
plot\u ly(数据=myDF,alpha=0.6)%>%
加上直方图(x=~Income,xaxis=“x1”)%>%
添加直方图(x=~ageIntwoyear增量,xaxis=“x2”)%>%
布局(
余量=列表(t=60),
title=“工资与年龄”,
xaxis=列表(
tickfont=list(color=“blue”),
side=“left”,
title=“收入”
),
xaxis2=列表(
tickfont=list(color=“red”),
覆盖=“x”,
side=“top”,
位置=0.95,
title=“
年龄” ), yaxis=列表(title=“count”) )

您可以混合直方图:

直方图通常在y轴上显示频率/计数,而不是在x轴上。我们可以制作一个你想要的图表,但我不确定它是否仍然是直方图

另外,正如你在我的照片中看到的,你的工资频率/计数(这里是蓝色)更高,变化性比年龄小。这使得制作一个好看的图表变得很困难。也许这只是你的样本数据的问题

因此,当你想使用直方图函数时,你必须反转频率的含义和x轴上的值

但无论如何,我认为一个散点图将是一个更好的解决方案,以显示工资和年龄之间的关系

编辑:

这是我运行代码时得到的结果:

像这样,我看不出情节的意义,也看不出你想要什么。第一个橙色列的含义是59岁在数据集中出现的次数在0到5次之间。第三列表示数据集中年龄在10到15倍之间的88岁。 在条形图中显示此信息无效。因为在计数的分类中可以有几个年龄值…我希望这是清楚的


无论如何,为了回答你的问题,我需要更多的澄清

您可以混合直方图:

直方图通常在y轴上显示频率/计数,而不是在x轴上。我们可以制作一个你想要的图表,但我不确定它是否仍然是直方图

另外,正如你在我的照片中看到的,你的工资频率/计数(这里是蓝色)更高,变化性比年龄小。这使得制作一个好看的图表变得很困难。也许这只是你的样本数据的问题

因此,当你想使用直方图函数时,你必须反转频率的含义和x轴上的值

但无论如何,我认为一个散点图将是一个更好的解决方案,以显示工资和年龄之间的关系

编辑:

这是我运行代码时得到的结果:

像这样,我看不出情节的意义,也看不出你想要什么。第一个橙色列的含义是59岁在数据集中出现的次数在0到5次之间。第三列表示数据集中年龄在10到15倍之间的88岁。 在条形图中显示此信息无效。因为在计数的分类中可以有几个年龄值…我希望这是清楚的

无论如何,为了回答你的问题,我需要更多的澄清

在回答之后,我想用一个其他人可以轻松使用的示例来回答这个问题,例如绘制两个重叠的直方图

# 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
#添加所需的软件包
图书馆(绘本)
#制作一些样本数据
a=rnorm(1000,4)
b=rnorm(1000,6)
#使用binsize设置自动绘制直方图
图在回答之后,我想用一个其他人可以轻松使用的例子来回答这个问题,例如绘制两个重叠的直方图

# 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
#添加所需的软件包
图书馆(绘本)
#制作一些样本数据
a=rnorm(1000,4)
b=rnorm(1000,6)
#使用binsize设置自动绘制直方图

图可以将图形翻转90度吗?这样直方图就向上了?非常感谢。另外,我不确定这个情节是否有意义。。。计数在x轴上,但方向是水平的?是否可以将图形翻转90度?这样直方图就向上了?非常感谢。另外,我不确定这个情节是否有意义。。。计数在x轴上,但方向是水平的?