重新排列Plotly R中的轴标签

时间:2016-04-15 13:30:41

标签: r plotly

我有以下数据框:

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

当我使用绘图库绘制它时,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仍然会出现同样的问题。

1 个答案:

答案 0 :(得分: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