R语言 如何在Plotly中使直方图条具有不同的颜色
在这篇文章中,我们将讨论在R语言的Plotly中使直方图条具有不同的颜色。直方图被定义为沿x轴的数据的条形图表示。plotly包包含plot_ly()函数,用于在R语言中可视化不同的图,如散点图、柱状图、饼状图等。
语法: plot_ly(df,type,marker,layout)
其中。
- df -dataframe
- type – 用来指定我们想要可视化的图的类型
- marker – 用颜色属性来标记不同颜色的绘图
- layout – 包含用于修改布局属性的属性,如标题等。
创建一个具有不同颜色的直方图
首先,我们需要安装并加载所需的软件包,然后创建包含数字矢量的样本数据框。使用plot_ly()函数,我们将绘制直方图,并使用标记属性,我们可以指定我们要绘制的不同条形图的颜色列表。
# install required packages
install.packages("plotly")
# Load the installed package
library(plotly)
# creation of sample dataframe (vector of numeric values)
df <- c(1,2,3,4,5,6,7,8)
# Plotting the scatter plot using plot_ly() function
plot_ly(x=df, type="histogram",
marker=list(color=c('green','red',
'blue','yellow'))) %>%
layout(title="Histogram using Plotly")
输出

极客教程