2016-04-15 139 views
0

我有以下的數據幀:在Plotly重新排列軸標籤[R

structure(list(Var1 = structure(1:7, .Label = c("Friday", "Monday", 
"Saturday", "Sunday", "Thursday", "Tuesday", "Wednesday"), class = "factor"), 
    Freq = c(20938L, 43147L, 35027L, 24087L, 7148L, 6310L, 19027L 
    )), .Names = c("Var1", "Freq"), row.names = c(NA, -7L), class = "data.frame") 

它看起來像這樣:

 Var1 Freq 
1 Friday 20938 
2 Monday 43147 
3 Saturday 35027 
4 Sunday 24087 
5 Thursday 7148 
6 Tuesday 6310 
7 Wednesday 19027 

當我使用plotly庫繪製它,x軸從週三開始一直到星期五。但我想從週日到週六重新排列標籤。我使用以下代碼:

假設「T」與上面給出的數據的數據幀:

target <- c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") 
a <- t[match(target, t$Dayoftheweek), ] 
plot_ly(a, x = Dayoftheweek, y = Freq, type = "bar", color = Dayoftheweek) 

這不起作用。我已將rownames重置爲null仍會發生同樣的問題。

回答

0

另一種方法是ggplot2。你可以試試:

t <- factor(t$Var1, levels = c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")) 
ggplot(data=t, aes(x=Var1, y=Freq, fill=Var1)) + geom_bar(stat='identity') + theme_bw() 
ggplotly() 

enter image description here

+0

我想這前面...但我得到一個錯誤 「在gg2list錯誤(P):對象nann」未找到「。但是當我重新開始R會話時,它工作。無法理解....但是謝謝。我得到了證實。 – Apricot