小数据集上的线图
Line plotting on small dataset
我有这个小数据集,我需要用 plotly 绘制。我正在为此苦苦挣扎:/
我希望有 3 条彩色线(每行(1,2,3)的每一行。x 轴需要是列名,Y 轴代表每个数值。
我的代码到目前为止看起来是错误的
plot_ly (x = colnames(a), y = a[1], type = "scatter" ,mode = "lines" )
我不确定这是否是您想要的情节,但听起来最接近您的描述。我改编了几列你的数据来描述。
如果使用 pivot_longer
的数据格式较长,绘图会更容易。如果您将行号添加到数据中也会更容易,这样您就可以为每个行号绘制一条线。
由于 plotly
将按字母顺序绘制您的 x 轴类别,因此您需要将 name
因子(名称是您的列名)重新调整为列的顺序。
在您的 plot_ly
语句中,使用 split
按行号绘制。
library(plotly)
library(tidyverse)
a %>%
mutate(rn = row_number()) %>%
pivot_longer(cols = -rn, names_to = "name", values_to = "value") %>%
mutate(name = factor(name, levels = colnames(a))) %>%
plot_ly(x = ~name, y = ~value, split = ~rn, type = "scatter", mode = "lines")
输出
数据
a <- data.frame(
N_of_Brands = c(-.4, .8, -.4),
Brand_Runs = c(-.26, .70, -.75),
Total_Volume = c(-.69, .15, -.015),
No_of_Trans = c(-.81, .45, -.35)
)
我有这个小数据集,我需要用 plotly 绘制。我正在为此苦苦挣扎:/
我的代码到目前为止看起来是错误的
plot_ly (x = colnames(a), y = a[1], type = "scatter" ,mode = "lines" )
我不确定这是否是您想要的情节,但听起来最接近您的描述。我改编了几列你的数据来描述。
如果使用 pivot_longer
的数据格式较长,绘图会更容易。如果您将行号添加到数据中也会更容易,这样您就可以为每个行号绘制一条线。
由于 plotly
将按字母顺序绘制您的 x 轴类别,因此您需要将 name
因子(名称是您的列名)重新调整为列的顺序。
在您的 plot_ly
语句中,使用 split
按行号绘制。
library(plotly)
library(tidyverse)
a %>%
mutate(rn = row_number()) %>%
pivot_longer(cols = -rn, names_to = "name", values_to = "value") %>%
mutate(name = factor(name, levels = colnames(a))) %>%
plot_ly(x = ~name, y = ~value, split = ~rn, type = "scatter", mode = "lines")
输出
数据
a <- data.frame(
N_of_Brands = c(-.4, .8, -.4),
Brand_Runs = c(-.26, .70, -.75),
Total_Volume = c(-.69, .15, -.015),
No_of_Trans = c(-.81, .45, -.35)
)