Anuj Sharma Anuj Sharma - 12 days ago 6
R Question

Plots not showing labels in order

I have following data frame:

BeginTime APName
20:04:33 wls-wilkeson17-46
20:20:06 wls-wilkeson17-46
21:28:01 wls-wilkeson17-46
21:33:10 wls-wilkeson17-46
21:48:56 wls-wilkeson17-46
21:59:18 wls-wilkeson17-46
21:07:19 wls-wilkeson17-47


I am trying to plot it in r like this:

plot(x = user_sig_all$BeginTime, y = factor(user_sig_all$APName), yaxt = "n")


Then I am drawing labels on y axis using:

axis(2, at=1:7, labels=user_sig_all$APName)


And graph I am getting is

enter image description here

As you can see it is just displaying label no 1 and 2 which are wls-wilkeson17-46, but it should show wls-wilkeson17-47 for topmost point as per data.

Can anyone suggest what I am doing wrong ?

Answer

After plotting, draw your axis one by one by putting in a loop like below:

plot(x = user_sig_all$BeginTime, y = factor(user_sig_all$APName), yaxt = "n")
countt <- 1
last_lab <- user_sig_all$APName[1]

for (AP in user_sig_all$APName){
  if(countt == 1) {
    axis(2, at=countt:countt, labels=AP)
    last_lab <- AP
    countt <- countt + 1
  } else {
    if(last_lab != AP){
      axis(2, at=countt:countt, labels=AP)
      last_lab <- AP
      countt <- countt + 1
    }
  }
}

Hope it helps. Good Luck :)