2016-03-21 7 views
1

凡例をRで注文することはできますか?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 

EDIT: この例では、plotly 3.x.x.を使用していますplotly 4.x.x以上を使用する場合、このコードはそのまま動作しない可能性があります。詳細はこちらをご覧ください。https://www.r-bloggers.com/upgrading-to-plotly-4-0-and-above/

+0

ありがとうございます! – marcopah

+0

私はSeinfeldの参考に感謝します:-) –

関連する問題