我有這個資料:
myData <- data.frame(Zupanija = c('GZ','ZGZ','KZ','VZ','KK','M','BB','VP','PS','BP','OB','VU','KA','SM','PG','LS','ZA','?K','SD','IS','DN'),
Inv = c(5205740135,2017069611,568884637,908563032,487561769,735161926,284291246,195329869,257799660,295494321,721957349,383617802,464253852,298576348,1182794616,277411269,677612459,405016102,3041655541,1039402830,642317513))
我想做簡單的 Plotly 點圖,我想使用以下代碼通過 Inv 變數(x 軸)對 y 軸上的資料進行排序:
myData %>%
plot_ly(x = ~Inv, y = ~ Zupanija) %>%
add_trace(type = 'scatter',
mode = 'markers',
stroke = I("black"),
span = I(1),
size= ~ sqrt(Inv),
color = ~ sqrt(Inv),
colors = inferno(50, alpha = 1, begin = 0, end = 1, direction = 1),
alpha = 1,
showlegend = FALSE)%>%
hide_colorbar()%>%
layout(xaxis = list(title = "HAMAG - ukupna ulaganja"),
yaxis = list(title=F, categoryorder = "total ascending"))

你可以看到,雖然我把這部分代碼放在了 layout(xaxis = list(title = "HAMAG - ukupna ulaganja"), yaxis = list(title=F, categoryorder = "total ascending")) - 我沒有得到想要的結果。有些變數是排序的,但 y 軸上的一些變數不是根據 x 值排序的:例如,ZGZ (2B) 高于 GZ (5B),BB 高于 KA,盡管它在 x 上的值較小。有沒有人看到我的錯誤?tnx。
uj5u.com熱心網友回復:
一種選擇是arrange按照您想要的順序手動設定資料集并將其設定categoryorder為"trace":
library(plotly)
library(viridis)
myData %>%
arrange(reorder(Zupanija, Inv)) %>%
plot_ly(x = ~Inv, y = ~Zupanija) %>%
add_trace(
type = "scatter",
mode = "markers",
stroke = I("black"),
span = I(1),
size = ~ sqrt(Inv),
color = ~ sqrt(Inv),
colors = inferno(50, alpha = 1, begin = 0, end = 1, direction = 1),
alpha = 1,
showlegend = FALSE
) %>%
hide_colorbar() %>%
layout(
xaxis = list(title = "HAMAG - ukupna ulaganja"),
yaxis = list(title = F, categoryorder = "trace")
)

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/397038.html
下一篇:從數值串列中,創建索引串列
