R中的组条形图 - 不正确的Y轴

时间:2017-05-09 11:40:54

标签: r charts bar-chart plotly

我正在尝试在R中构建一个分组条形图。我已粘贴下面的数据框。我一直在使用 plotly 来构建图表。问题是,Y轴上的数字不正确,因为它们不会按升序增加。我还发布了一张图形图像。

有人可以指出,我哪里出错了?

数据帧

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>

构建图表的代码

 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)

图表

enter image description here

修改

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

2 个答案:

答案 0 :(得分:1)

正如@neilfws在评论中提到的那样,问题是您的y访问数据是基于因素构建的。在绘图之前,您可以尝试在数据读取上修复此问题(如@neilfws所述)或将数据强制转换为数字。以下是你如何做到后者。

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

此外,在这种情况下,您无需致电ggplotly。只有在想要使用ggplot2构建绘图然后将plotly的交互性添加到ggplot对象时才需要该函数。

答案 1 :(得分:1)

除了Adam Spannbauer的方法,您还可以强制Plotly将数据解释为数字,方法是将yaxis type设置为linear

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

enter image description here