R 在绘图仪中重新排列轴标签
我有以下数据框:R 在绘图仪中重新排列轴标签,r,plotly,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",
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 library绘制它时,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)
target另一种选择是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()
t我之前尝试过这个……但是我得到了一个错误“gg2list(p)中的错误:找不到对象'nann'。但是当我重新启动R会话时,它工作了。我不明白……不过非常感谢。我得到了确认。