简体   繁体   English

在Plotly R中重新排列轴标签

[英]rearranging axis labels in Plotly R

I have the following data frame: 我有以下数据框:

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")

It looks like this: 看起来像这样:

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

when I plot it using plotly library, the x-axis starts from Wednesday and goes all the way up to Friday. 当我使用plotly库对其进行绘制时,x轴从星期三开始一直一直到星期五。 But I want to reorder the labels starting from Sunday to Saturday. 但是我想从星期日到星期六重新排列标签。 I use the following codes: 我使用以下代码:

Assuming 't' is the data frame with data given above: 假设“ 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) 

This doesn't work. 这行不通。 I have reset the rownames to null still the same problem occurs. 我将行名重置为null仍然发生相同的问题。

An alternative would be ggplot2 . 另一个选择是ggplot2 You can try: 你可以试试:

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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM