2016-03-21 103 views
1

是否可以訂購R中的圖例條目?Plotly R訂單圖例條目

如果我例如指定一個這樣的餅圖:

plot_ly(df, labels = Product, values = Patients, type = "pie", 
      marker = list(colors = Color), textfont=list(color = "white")) %>% 
    layout(legend = list(x = 1, y = 0.5)) 

傳說根據哪個產品具有最高數量的患者進行排序。我希望圖例按照產品的字母順序排序。

這可能嗎?

回答

3

是的,這是可能的。圖表選項在這裏: https://plot.ly/r/reference/#pie

一個例子:

library(plotly) 
library(dplyr) 

# Dummy data 
df <- data.frame(Product = c('Kramer', 'George', 'Jerry', 'Elaine', 'Newman'), 
       Patients = c(3, 6, 4, 2, 7)) 

# Make alphabetical 
df <- df %>% 
    arrange(Product) 

# Sorts legend largest to smallest 
plot_ly(df, 
     labels = Product, 
     values = Patients, 
     type = "pie", 
     textfont = list(color = "white")) %>% 
    layout(legend = list(x = 1, y = 0.5)) 

# Set sort argument to FALSE and now orders like the data frame 
plot_ly(df, 
     labels = Product, 
     values = Patients, 
     type = "pie", 
     sort = FALSE, 
     textfont = list(color = "white")) %>% 
    layout(legend = list(x = 1, y = 0.5)) 

# I prefer clockwise 
plot_ly(df, 
     labels = Product, 
     values = Patients, 
     type = "pie", 
     sort = FALSE, 
     direction = "clockwise", 
     textfont = list(color = "white")) %>% 
    layout(legend = list(x = 1, y = 0.5)) 

會議信息:

R version 3.2.3 (2015-12-10) 
Platform: i386-w64-mingw32/i386 (32-bit) 
Running under: Windows >= 8 x64 (build 9200) 

... 

attached base packages: 
[1] stats  graphics grDevices utils  datasets 
[6] methods base  

other attached packages: 
[1] dplyr_0.4.3 plotly_3.4.3 ggplot2_2.1.0 

編輯: 此示例使用plotly 3.x.x.如果您使用plotly 4.x.x或更高版本,則此代碼可能無法正常工作。參見這裏瞭解更多詳情:https://www.r-bloggers.com/upgrading-to-plotly-4-0-and-above/

+0

工程就像一個魅力 - 謝謝你! – marcopah

+0

我很欣賞Seinfeld的參考資料:-) –