0

I am trying to create a Descending bars bar plot using ggplot2 and plotly in R. The data I am using is "a12" which is consumed by the ggplot under the line "#Final Data frame is consumed below".

If you see the snap shot below, the "na" value in the column "a1" should come at second spot in the plot, however it does not follow the descending order and is pushed at the last.

One way to cure this issue is to hardcode the value there, but I don't want to do that. Instead is there a way using which I can make "na" follow the order without hard coding any value?

Note: Please do not modify the data above the line "#Final Data frame is consumed below". The actual data I am working on is actually in the same format. Please help.

a1 = c("A","","B","C","D","F")
b1 = c(165,154,134,110,94,78)
a12 = data.frame(a1,b1)
a12[2, 1] = "NA"

#Final Data frame is consumed below
pp1 <<- ggplot(a12 , 
               aes(x = reorder(a1, -b1), 
                   y = b1,
                   text = paste("User: <br>", a1, "<br> Days: <br>", round(b1)))) + 
  geom_bar(stat = "identity", fill = "#3399ff" ) + 
  scale_y_continuous(name = "Time") + 
  scale_x_discrete(name = "Employee") 

ggplotly(pp1, tooltip="text", height = 392)

Addon Script below:

a1 = c("A",NA,"B","C","D","F")
b1 = c(165,154,134,110,94,78)
a12 = data.frame(a1,b1,stringsAsFactors = FALSE)
pp1 <<- ggplot(a12 , aes(x = reorder(a1,-b1), y = b1,text=paste("User: 
<br>",a1, "<br> Days: <br>", round(b1)))) + 
geom_bar(stat = "identity", fill = "#3399ff" ) + scale_y_continuous(name 
="Time") + scale_x_discrete(name ="Employee") 
ggplotly(pp1, tooltip="text",height = 392)

enter image description here

Robert J
  • 79
  • 6
  • 2
    Set `stringsAsFactors = FALSE` in your third line. – markus Dec 23 '17 at 08:37
  • @markus, it worked thanks, I also wish to know, say I have a 1000 row data with multiple columns having similar "NA" values, does this command take care of the entire data? – Robert J Dec 23 '17 at 08:49
  • 2
    please make this a new question giving a reproducible example as you did above. – markus Dec 23 '17 at 09:01
  • I'll figure this out, really appreciate your help, I also am kind of stuck with another issue in which I need your assistance, kindly check this link https://stackoverflow.com/questions/47951307/selection-of-activity-trace-in-a-chart-and-display-in-a-data-table-in-r-shiny – Robert J Dec 23 '17 at 09:31
  • @markus, if you replace, the second value in the vector a1 with a NA, the following solution does not work, I am attaching addition script below, kindly run and suggest a possible solution. – Robert J Dec 26 '17 at 11:12
  • I think you're mixing things up. Do you want to replace this value with `NA`, i.e. 'Not Available' or with `"NA"` ? The latter creates a string, which is what you've done in the first place. – markus Dec 26 '17 at 17:32
  • @markus, I'll be very clear now, I don't want to do any modification to the data, and the plot I create should have descending bars such that NA appears at the second position as in the vector. Please help me with the script under "Addon Script below:". And again, no replacement, no modification to the data, thanks for replying and please help. – Robert J Dec 26 '17 at 18:21
  • 1
    Possible duplicate of [Arranging bars of a bar plot with "null" in Descending order in R](https://stackoverflow.com/questions/47976994/arranging-bars-of-a-bar-plot-with-null-in-descending-order-in-r) – www Dec 27 '17 at 10:22

1 Answers1

3

In order to arrange the bars in the way you want, there should not be a missing value, i.e. NA, in df$a1. We somehow need to replace that missing value.

Since you asked for "no replacement, no modification to the data" in the comments above, I suggest you replace the missing values inside the call to ggplot using, e.g. tidyr::replace_na. This will leave your original data unchanged.

library(tidyr)
library(ggplot2)
pp1 <- ggplot(data = replace_na(a12, replace = list(a1 = "NA")),
                      aes(x = reorder(a1, -b1),
                          y = b1,
                          text = paste("User: <br>", a1, "<br> Days: <br>", round(b1)))) +
  geom_bar(stat = "identity", fill = "#3399ff") +
  scale_y_continuous(name = "Time") +
  scale_x_discrete(name = "Employee")

ggplotly(pp1, tooltip = "text",height = 392)

enter image description here

I hope this helps.

markus
  • 25,843
  • 5
  • 39
  • 58