【问题标题】:How to adjust plotly marker colors to match ordered categories in R?如何调整绘图标记颜色以匹配 R 中的有序类别?
【发布时间】:2016-09-18 20:27:52
【问题描述】:

在 R 中使用 plotly,我希望类别按照歌曲的数量顺序排列为不同的颜色(最好由我预先选择)。这是我尝试过的:

salesplot <-plot_ly(producersales, type="scatter", x=Producer, y=SalesPerSong, color=c('20+ songs', '11 songs','8-10 songs','5-7 songs', '3-4 songs', '2 songs'), size=SalesPerSong, mode="markers")
## Sample of my data
head(producersales)
               Producer NoOfSongs TotalSales SalesPerSong  SongRange
1             Timbaland        24    3446852       143619  20+ songs
2            Just Blaze        23    3134585       136286  20+ songs
3            Kanye West        20    3338410       166920  20+ songs
4 Jerome "J-Roc" Harmon        11    1165000       105909   11 songs
5          The Neptunes        11    1419877       129080   11 songs
6               No I.D.         9    1437008       159668 8-10 songs

问题是,当我打印 salesplot 时,所有标记都是一种颜色(2 首歌曲)。另外,如果我尝试使用 color=SongRange,则图例的顺序不是我需要的。

【问题讨论】:

  • 你可以试试加group = SongRange看看。不确定这是否适用于您想要的,因为我也是 plotly 的新手。 :)

标签: r plotly scatter-plot markers r-plotly


【解决方案1】:

你在寻找这样的东西吗?

#order factor like you want
producersales$SongRange  <- factor(producersales$SongRange , 
                                   levels = c("8-10songs", "11songs", "20+songs"))

#select colour you want
cols <- c("red", "blue", "black")
#plot
salesplot <- plot_ly(producersales, 
                     type = "scatter", 
                     x = Producer, 
                     y = SalesPerSong, 
                     color = SongRange,
                     colors = cols, 
                     mode = "markers")
salesplot

【讨论】:

  • 谢谢!我知道这是关卡功能,但想不出名字!!这是我最终得到的结果:producersales$SongRange &lt;- factor(producersales$SongRange, levels =c("2 songs", "3-4 songs", "5-7 songs", "8-10 songs","11 songs", "20+ songs")) cols &lt;- c("white", "yellow", "orange", "red", "blue", "green") salesplot &lt;-plot_ly(producersales, type="scatter", x=Producer, y=SalesPerSong, color=SongRange, colors=cols, mode="markers", size=SalesPerSong)我不知道如何使代码分开。
猜你喜欢
  • 2021-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-17
  • 1970-01-01
  • 1970-01-01
  • 2016-10-06
相关资源
最近更新 更多