Siddharth Patel
Siddharth Patel

Reputation: 193

Group Bar Chart in R - Improper Y Axis

I'm trying to build a grouped bar chart in R. I have pasted the dataframe below. I have been using plotly to build the chart. The problem is, the numbers on Y axis are not proper, as in they do not increase in ascending order. I've also posted an image of graph formed.

Can someone please point out, where I'm going wrong?

Dataframe

chart.supp.part.defect.matrix


     Supplier PaintMarking45 Seal78 AirConditioning57 Engine34 CargoCompartment543 Insulation11
1     HJRU              8   <NA>              <NA>        1                <NA>         <NA>
2     DJDU           <NA>      1              <NA>     <NA>                <NA>         <NA>
3     DEF7           <NA>      3                54     <NA>                <NA>         <NA>
4      A23           <NA>   <NA>              <NA>        7                <NA>         <NA>
5      A52              3   <NA>              <NA>     <NA>                   2         <NA>
6     FJUE             65   <NA>                 1     <NA>                <NA>           11
7      A31           <NA>      1                 5     <NA>                <NA>         <NA>
8     DJHD           <NA>   <NA>              <NA>     <NA>                <NA>         <NA>
9      A38              4   <NA>                22     <NA>                <NA>         <NA>

Code to build chart

 title <- paste( "Supplier vs Defect")

p3 <- plot_ly(chart.supp.part.defect.matrix, x = ~Supplier, y = ~PaintMarking45, type = 'bar', name = 'Paint/Marking-45') %>%
  add_trace(y = ~Seal78,name = 'Seal-78') %>%
  add_trace(y = ~AirConditioning57,name = 'Air conditioning - 57') %>%
  add_trace(y = ~Engine34,name = 'Engine-34') %>%
  add_trace(y = ~CargoCompartment543,name = 'Cargo compartment-543') %>%
  add_trace(y = ~Insulation11 ,name = 'Insulation -11') %>%
  add_trace(y = ~Insulation6,name = 'Insulation-6') %>%
  add_trace(y = ~Engine11,name = 'Engine-11') %>%
  add_trace(y = ~Propulsion32,name = 'Propulsion-32') %>%
  layout(yaxis = list(title = 'Defect Count'), barmode = 'group') %>%
  layout(title = title)

ggplotly(p3)

Chart

enter image description here

Edit

dput(chart.supp.part.defect.matrix)


structure(list(Supplier = structure(c(9L, 6L, 5L, 1L, 4L, 8L, 
2L, 7L, 3L), .Label = c(" A23", " A31", " A38", " A52", " DEF7", 
"DJDU", "DJHD", "FJUE", "HJRU"), class = "factor"), PaintMarking45 = structure(c(4L, 
NA, NA, NA, 1L, 3L, NA, NA, 2L), .Label = c("3", "4", "65", "8"
), class = "factor"), Seal78 = structure(c(NA, 1L, 2L, NA, NA, 
NA, 1L, NA, NA), .Label = c("1", "3"), class = "factor"), AirConditioning57 = structure(c(NA, 
NA, 4L, NA, NA, 1L, 3L, NA, 2L), .Label = c("1", "22", "5", "54"
), class = "factor"), Engine34 = structure(c(1L, NA, NA, 2L, 
NA, NA, NA, NA, NA), .Label = c("1", "7"), class = "factor"), 
    CargoCompartment543 = structure(c(NA, NA, NA, NA, 1L, NA, 
    NA, NA, NA), .Label = "2", class = "factor"), Insulation11 = structure(c(NA, 
    NA, NA, NA, NA, 1L, NA, NA, NA), .Label = "11", class = "factor"), 
    Insulation6 = structure(c(NA, NA, NA, NA, NA, NA, 1L, NA, 
    NA), .Label = "7", class = "factor"), Engine11 = structure(c(NA, 
    NA, NA, NA, NA, NA, 2L, 1L, NA), .Label = c("54", "8"), class = "factor"), 
    Propulsion32 = structure(c(NA, NA, NA, NA, NA, NA, NA, NA, 
    1L), .Label = "2", class = "factor")), .Names = c("Supplier", 
"PaintMarking45", "Seal78", "AirConditioning57", "Engine34", 
"CargoCompartment543", "Insulation11", "Insulation6", "Engine11", 
"Propulsion32"), row.names = c(NA, -9L), class = "data.frame")

Upvotes: 0

Views: 240

Answers (2)

Maximilian Peters
Maximilian Peters

Reputation: 31679

In addition to Adam Spannbauer's approach you can also force Plotly to interpret the data as numbers by setting the yaxis type to linear

layout(yaxis=list(type='linear'))

enter image description here

Upvotes: 1

Adam Spannbauer
Adam Spannbauer

Reputation: 2757

as @neilfws mentioned in a comment the issue is that your y access data is being built off of factors. You can attempt to fix this on your data read (as @neilfws mentioned) or coerce your data to numeric before plotting. Below is how you can do the latter.

chart.supp.part.defect.matrix[,2:10] <- lapply(chart.supp.part.defect.matrix[,2:10], as.numeric)

p3 <- plot_ly(chart.supp.part.defect.matrix, x = ~Supplier, y = ~PaintMarking45, type = 'bar', name = 'Paint/Marking-45') %>%
  add_trace(y = ~Seal78,name = 'Seal-78') %>%
  add_trace(y = ~AirConditioning57,name = 'Air conditioning - 57') %>%
  add_trace(y = ~Engine34,name = 'Engine-34') %>%
  add_trace(y = ~CargoCompartment543,name = 'Cargo compartment-543') %>%
  add_trace(y = ~Insulation11 ,name = 'Insulation -11') %>%
  add_trace(y = ~Insulation6,name = 'Insulation-6') %>%
  add_trace(y = ~Engine11,name = 'Engine-11') %>%
  add_trace(y = ~Propulsion32,name = 'Propulsion-32') %>%
  layout(yaxis = list(title = 'Defect Count'), barmode = 'group') %>%
  layout(title = title)

p3

Additionally, you don't need to call ggplotly in this case. That function is only needed when you want to build your plot using ggplot2 and then add plotly's interactivity to the ggplot object.

Upvotes: 1

Related Questions