如何在R中使用Plotly制作直方图,并使直方图条形具有不同的颜色。

3

我正在使用R中的plotly包制作直方图,下面是图表。

输入图片描述

这是我的代码:

plot_ly(test, x = ~count, type = 'histogram',  marker = list(color = 'blue'))

我想知道是否有一种方法可以从颜色角度使图表更加直观(例如,每个柱状图使用不同的颜色),如下所示的图表。

这是我在网上找到的一个例子:

enter image description here

提前致谢。

2个回答

2
plot_ly(test, x = ~ count, type = 'histogram',
        marker = list(color = viridis::viridis_pal(option = "C", direction = -1)(4)))

谢谢。在直方图上,如果我增加了条形的数量,那么x轴会跳过一些数字。例如,我将条形的数量从4个增加到11个。然而,x轴仅显示2、4、6等数字,但跳过了1、3、5等数字。谢谢。 - MMAASS

1
plot_ly(test, x = ~count, color = ~count) %>%
  add_histogram()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接