在R中以Plotly绘制条形图

时间:2019-07-04 09:16:05

标签: r plotly

我有一个数据帧(矩阵13x11)。您可以在下面看到图片 enter image description here

还有数据:

 # Data

        test_data_set<-structure(list(Name = c("Cars_sale_1", "Cars_sale_2", "Cars_sale_3", 
        "Cars_sale_4", "Cars_sale_5", "Cars_sale_6", "Cars_sale_7", "Cars_sale_8", 
        "Cars_sale_9", "Cars_sale_10", "Cars_sale_11", "Cars_sale_12", 
        "Cars_sale_13"), First = c(156300.824706096, 10006.2467099491, 
        3212.0722933848, 3319.03842779435, 9658.39620986138, 8434.32181084401, 
        1367.81891559923, 717.880329882435, 260.817687313564, 196.525706264257, 
        1042.98999824531, 7036.46253728724, 14974.7002155131), Second = c(227324.372696964, 
        16086.4713107563, 6318.58220740481, 21832.8829619231, 15740.5860677312, 
        10538.8313739252, 4399.92981224776, 2872.64432356554, 1391.68275135989, 
        0, 1979.57536409896, 12618.0733462011, 20694.7337436906), Third = c(277421.301982804, 
        18264.5376381821, 10922.6180031584, 30805.9659589402, 23327.3205825583, 
        14162.2038954203, 9179.99649061239, 5272.22319705212, 3019.19635023688, 
        0, 3587.71714335848, 17227.7241621337, 21867.2276106995), Fourth = c(307141.042288121, 
        27274.1182663625, 15141.1826636252, 51266.257238112, 25035.1289699947, 
        18876.8555886998, 13549.8859449026, 12045.9027899632, 4577.92595192139, 
        0, 9101.66695911564, 19369.2928583962, 30971.5263285415), Fifth = c(345904.895595719, 
        35406.3519915775, 21022.9163011055, 70233.5146516933, 28311.4932444288, 
        22832.3565537814, 21108.8261098438, 14801.7546938059, 4776.69766625724, 
        56.1502017897877, 11680.6457273206, 24203.544481488, 25989.4022630561
        ), Sixth = c(375676.013335673, 38199.2630286015, 34954.3428671697, 
        96511.528338305, 33332.4442884717, 27694.4025267591, 27706.1940691349, 
        26899.0349184067, 8709.73855062292, 224.600807159151, 16098.5436041411, 
        31910.4404281453, 32467.4847713049), Seventh = c(433176.346727496, 
        47455.623793648, 51832.251272153, 121340.024565713, 41695.1745920337, 
        31331.5318476926, 44969.8543604141, 24795.9291103702, 10157.0100017547, 
        828.215476399368, 27548.4120021056, 41680.0140375504, 35955.6910763933
        ), Eight = c(501520.687839972, 55052.4653447973, 74202.4916652044, 
        162651.693279523, 45550.4474469205, 40385.1903842779, 54554.132303913, 
        43609.6157220565, 16360.2035444815, 4171.95999298123, 45789.3665555361, 
        53713.5637831198, 29226.7897579876), Ninth = c(567436.251974031, 
        65858.0101772241, 104945.288647131, 238514.82716266, 60495.6659062993, 
        52381.4002456571, 100849.973679593, 61956.6941568696, 27927.4258641867, 
        4159.60694858747, 77211.5809791192, 69056.0449201614, 29472.1253015506
        ), Tenth = c(755730.057904896, 89047.2012633796, 208602.210914195, 
        544052.500438673, 195334.760484295, 129515.213195297, 220957.50131602, 
        119074.083172486, 115559.080540446, 36932.7952272328, 156449.622740832, 
        120385.751886296, 33197.0639513509)), row.names = c(NA, -13L), class = c("tbl_df", 
        "tbl", "data.frame"))

我的意图是在R中制作两个图形条形图,如下面的图片所示,并使用Plotly软件包在Excel中绘制。

enter image description here enter image description here

如何使用R中的Plotly包绘制堆叠的条形图?

1 个答案:

答案 0 :(得分:1)

问题是您的行和列必须交换。完成后,您必须考虑“第一”,“第二”等的顺序,以便它们以正确的顺序打印

library(plotly)
t_df <- data.frame(t(test_data_set[,-1]))
colnames(t_df) <- test_data_set$Name
t_df$Number <- factor(row.names(t_df),levels=row.names(t_df),ordered=TRUE)

p <- plot_ly(t_df, x = ~Number, y = ~Cars_sale_1, type = 'bar', name = 'Cars_sale_1') %>%
  add_trace(y = ~Cars_sale_2, name = 'Cars_sale_2') %>%
  add_trace(y = ~Cars_sale_3, name = 'Cars_sale_3') %>%
  add_trace(y = ~Cars_sale_4, name = 'Cars_sale_4') %>%
  add_trace(y = ~Cars_sale_5, name = 'Cars_sale_5') %>%
  add_trace(y = ~Cars_sale_6, name = 'Cars_sale_6') %>%
  add_trace(y = ~Cars_sale_7, name = 'Cars_sale_7') %>%
  add_trace(y = ~Cars_sale_8, name = 'Cars_sale_8') %>%
  add_trace(y = ~Cars_sale_9, name = 'Cars_sale_9') %>%
  add_trace(y = ~Cars_sale_10, name = 'Cars_sale_10') %>%
  add_trace(y = ~Cars_sale_11, name = 'Cars_sale_11') %>%
  add_trace(y = ~Cars_sale_12, name = 'Cars_sale_12') %>%
  add_trace(y = ~Cars_sale_13, name = 'Cars_sale_13') %>%
  layout(yaxis = list(title = 'Count'), barmode = 'stack')
p

enter image description here 对于第二张图,我创建了一个新的数据框,其中确保每一行的总和为1

t_df_pct <- t_df
t_df_pct[,1:(ncol(t_df)-1)] <- t_df_pct[,1:(ncol(t_df)-1)]/rowSums(t_df_pct[,1:(ncol(t_df)-1)])

p2 <- plot_ly(t_df_pct, x = ~Number, y = ~Cars_sale_1, type = 'bar', name = 'Cars_sale_1') %>%
  add_trace(y = ~Cars_sale_2, name = 'Cars_sale_2') %>%
  add_trace(y = ~Cars_sale_3, name = 'Cars_sale_2') %>%
  add_trace(y = ~Cars_sale_4, name = 'Cars_sale_4') %>%
  add_trace(y = ~Cars_sale_5, name = 'Cars_sale_5') %>%
  add_trace(y = ~Cars_sale_6, name = 'Cars_sale_6') %>%
  add_trace(y = ~Cars_sale_7, name = 'Cars_sale_7') %>%
  add_trace(y = ~Cars_sale_8, name = 'Cars_sale_8') %>%
  add_trace(y = ~Cars_sale_9, name = 'Cars_sale_9') %>%
  add_trace(y = ~Cars_sale_10, name = 'Cars_sale_10') %>%
  add_trace(y = ~Cars_sale_11, name = 'Cars_sale_11') %>%
  add_trace(y = ~Cars_sale_12, name = 'Cars_sale_12') %>%
  add_trace(y = ~Cars_sale_13, name = 'Cars_sale_13') %>%
  layout(yaxis = list(title = 'Percentage'), barmode = 'stack')
p2

second barplot