我试图在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")
)
非常感谢任何帮助!
答案 0 :(得分:4)
这是给第一个yaxis overlaying
的主要原因。由于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")
)
答案 1 :(得分:2)
您可以混合直方图:
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")
)
直方图通常在y轴上具有频率/计数而不在x轴上。我们可以生成你想要的图表,但我不确定它是否仍然是直方图。
另外,就像你在我的照片中看到的那样,工资的频率/数量(这里是蓝色)更高,变异性小于年龄。这使得图表看起来很难。也许这只是您的样本数据的问题......
所以当你想要使用直方图函数时,你必须反转频率的含义和x轴上的值。
但无论如何,我认为一个scaternplot将是一个更好的解决方案,以显示薪水和年龄之间的关系。
<强> 编辑: 强>
这是我运行代码时得到的结果:
像这样,我没有看到情节中的感觉和你想要的东西。第一个橙色柱的含义是,您的数据集中的年龄为59到0到5次。第三列表示数据集中的年龄为88 ocours,介于10到15次之间。 在条形图中显示此信息不起作用。因为你可以在计数类别中有几个年龄值...我希望这很清楚。
无论如何,要回答你的问题,我需要进一步澄清。
答案 2 :(得分:0)
在回答here之后,我想举一个例子,其他人在绘制两个重叠的直方图时可以很容易地使用它。
# 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