首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何巧妙地调整标记颜色以匹配R中有序的类别?

如何巧妙地调整标记颜色以匹配R中有序的类别?
EN

Stack Overflow用户
提问于 2016-05-21 17:14:34
回答 1查看 2.8K关注 0票数 2

在R中巧妙地使用,我希望类别是不同的颜色(最好是由我预选的)按#的歌曲顺序。以下是我尝试过的:

代码语言:javascript
复制
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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-21 19:49:37

你在找这样的东西吗?

代码语言:javascript
复制
#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
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37365895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档