如何调整绘图标记颜色以匹配R中的有序类别?
在R中使用plotly,我希望类别是不同的颜色(最好是我预先选择的),以歌曲的顺序排列。以下是我尝试过的:如何调整绘图标记颜色以匹配R中的有序类别?,r,plotly,scatter-plot,markers,r-plotly,R,Plotly,Scatter Plot,Markers,R Plotly,在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="marker
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你在找这样的东西吗
#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您可以尝试添加group=SongRange
并查看。我不确定这是否符合您的要求,因为我对plotly也是新手谢谢您!我知道这是关卡功能,但我想不出它的名字!!以下是我的结论:producersales$SongRange