Home > OS >  Setting bars and legend properly
Setting bars and legend properly

Time:12-07

I am working in R with Plotly. Below you can see my bar chart and data.

t_df3<-data.frame(structure(list(deciles = c(0.001, 0.002, 0.003, 0.004, 0.005, 
                           0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 
                           0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 
                           0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 
                           0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 
                           0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 
                           0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 
                           0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 
                           0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 
                           0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 
                           0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 
                           0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 
                           0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 
                           0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01), variable.x = structure(c(1L, 
                                                                                                     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                                                     2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
                                                                                                     4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
                                                                                                     5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 
                                                                                                     7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 
                                                                                                     9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 
                                                                                                     10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
                                                                                                     11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("Food", 
                                                                                                                                                                             "Alcoholic Beverages", "Clothing", "Housing Water", "Furnishings", 
                                                                                                                                                                             "Health", "Transport", "Communication", "Recreation", "Education", 
                                                                                                                                                                             "Restaurants", "Miscellaneous Goods"), class = "factor"), value.x = c(958.823102803738, 
                                                                                                                                                                                                                                                   1292.9, 1575.2, 1807, 1911.8, 
                                                                                                                                                                                                                                                   2041.2, 2376.83, 2723, 3161.9, 
                                                                                                                                                                                                                                                   4130.448, 120.0, 304, 246.4, 
                                                                                                                                                                                                                                                   249.8, 285, 382.1, 494.5, 
                                                                                                                                                                                                                                                   691.6, 787, 948.8, 11.9, 
                                                                                                                                                                                                                                                   22.9085309734513, 62, 77.3, 201.184778761062, 
                                                                                                                                                                                                                                                   239.961132743363, 330, 588.19178761062, 766.9, 
                                                                                                                                                                                                                                                   2117.2, 46, 91.0239292035398, 166.0, 
                                                                                                                                                                                                                                                   329.5, 525, 772.3, 1060.3112920354, 
                                                                                                                                                                                                                                                   1297.9, 1680, 3963.0, 106.8, 
                                                                                                                                                                                                                                                   175.2, 228, 295.961379310345, 300.2, 
                                                                                                                                                                                                                                                   404.8, 447, 496.528551724138, 731.2, 
                                                                                                                                                                                                                                                   1916.6995862069, 68, 71.9, 111.1, 
                                                                                                                                                                                                                                                   154.9, 201, 253.49, 248.205798165138, 
                                                                                                                                                                                                                                                   247.0, 421, 850.106642201835, 19.2, 
                                                                                                                                                                                                                                                   14.9, 40, 96.0269734513274, 200.4, 
                                                                                                                                                                                                                                                   354.0, 457, 745.2, 929.6, 
                                                                                                                                                                                                                                                   2654.9, 4.9, 12.6, 50.6, 
                                                                                                                                                                                                                                                   77.0251034482759, 168., 259.1, 364.2, 
                                                                                                                                                                                                                                                   571.067586206897, 828.672, 1452.7, 6.75, 
                                                                                                                                                                                                                                                   4.5, 5.41241379310345, 25.2, 46.1, 
                                                                                                                                                                                                                                                   68.2, 125.2, 104.550620689655, 258.9, 
                                                                                                                                                                                                                                                   951.36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15.5, 14.1, 
                                                                                                                                                                                                                                                   44.5, 79.2, 119.3, 216.31, 
                                                                                                                                                                                                                                                   316.8, 577.3, 901.7, 1897.1, 
                                                                                                                                                                                                                                                   70.5037168141593, 97.10, 136.4, 189.5, 
                                                                                                                                                                                                                                                   250.2, 333.9, 439.5, 583.7, 
                                                                                                                                                                                                                                                   725.7, 1283.5), color.x = structure(c(1L, 
                                                                                                                                                                                                                                                                                                              1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                                                                                                                                                                                                                                                              2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
                                                                                                                                                                                                                                                                                                              4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
                                                                                                                                                                                                                                                                                                              5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 
                                                                                                                                                                                                                                                                                                              7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 
                                                                                                                                                                                                                                                                                                              9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 
                                                                                                                                                                                                                                                                                                              10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
                                                                                                                                                                                                                                                                                                              11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("blue", 
                                                                                                                                                                                                                                                                                                                                                                                      "cyan", "darkgreen", "red", "brown", "chartreuse", "green", "purple", 
                                                                                                                                                                                                                                                                                                                                                                                      "gold", "tomato", "darkturquoise", "forestgreen"), class = "factor"), 
               deciles = c(0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 
                           0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 
                           0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 
                           0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 
                           0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 
                           0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 
                           0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 
                           0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 
                           0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 
                           0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 
                           0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 
                           0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 
                           0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 
                           0.006, 0.007, 0.008, 0.009, 0.01), variable = structure(c(1L, 
                                                                                     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                                     2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
                                                                                     4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 
                                                                                     5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 
                                                                                     7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 
                                                                                     8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 
                                                                                     10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 
                                                                                     11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 
                                                                                     12L, 12L, 12L, 12L, 12L), .Label = c("Food", "Alcoholic Beverages", 
                                                                                                                          "Clothing", "Housing Water", "Furnishings", "Health", "Transport", 
                                                                                                                          "Communication", "Recreation", "Education", "Restaurants", 
                                                                                                                          "Miscellaneous Goods"), class = "factor"), value = c(145.111841584158, 
                                                                                                                                                                               195.676277227723, 238.405544554455, 273.52, 289.35, 
                                                                                                                                                                               308.935841584158, 359.719128712871, 412.1, 478.5, 
                                                                                                                                                                               625.117306930693, 96.9022702702703, 245.5, 198.9, 
                                                                                                                                                                               201.624648648649, 230.688432432432, 308.3, 399.0, 
                                                                                                                                                                               558.103135135135, 635.512216216216, 765.6, 11.9, 
                                                                                                                                                                               22.9085309734513, 62.3668672566372, 77.3, 201.1, 
                                                                                                                                                                               239.961132743363, 330.529486725664, 588.1, 766.9, 
                                                                                                                                                                               2117.21543362832, 46.402407079646, 91.0, 166.0, 
                                                                                                                                                                               329.568637168142, 525.05182300885, 772.307681415929, 1060.3112920354, 
                                                                                                                                                                               1297.90619469027, 1680.52311504425, 3963.01847787611, 106.808275862069, 
                                                                                                                                                                               175.229793103448, 228.774620689655, 295.961379310345, 300.238344827586, 
                                                                                                                                                                               404.881655172414, 447.85324137931, 496.528551724138, 731.241931034483, 
                                                                                                                                                                               1916.9, 68.8187889908257, 71.9220550458716, 111.1, 
                                                                                                                                                                               154.5, 201.2, 253.4, 248.2, 
                                                                                                                                                                               247.1, 421.0, 850.106642201835, 19.2, 
                                                                                                                                                                               14.94, 40.8, 96.0269734513274, 200.4, 
                                                                                                                                                                               354.09, 457.8, 745.21614159292, 929.6, 
                                                                                                                                                                               2654.1, 4.97, 12.6554482758621, 50.6, 
                                                                                                                                                                               77.0, 168.119172413793, 259.120551724138, 364.27, 
                                                                                                                                                                               571.07, 828.672, 1452.78786206897, 6.75310344827586, 
                                                                                                                                                                               4.55834482758621, 5.41241379310345, 25.2513103448276, 46.1428965517241, 
                                                                                                                                                                               68.2262068965517, 125.256827586207, 104.550620689655, 258.918620689655, 
                                                                                                                                                                               951.36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15.5453793103448, 14.1020689655172, 
                                                                                                                                                                               44.5, 79.2, 119.3, 216.3, 
                                                                                                                                                                               316.8, 577.3, 901.7, 1897.1, 
                                                                                                                                                                               70.5, 97.10, 136.421309734513, 189.527575221239, 
                                                                                                                                                                               250.2, 333.986336283186, 439.591433628319, 583.765805309735, 
                                                                                                                                                                               725.7, 1283.5), color = structure(c(1L, 
                                                                                                                                                                                                                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                                                                                                                                                                                        2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
                                                                                                                                                                                                                                        4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 
                                                                                                                                                                                                                                        5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 
                                                                                                                                                                                                                                        7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 
                                                                                                                                                                                                                                        8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 
                                                                                                                                                                                                                                        10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 
                                                                                                                                                                                                                                        11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 
                                                                                                                                                                                                                                        12L, 12L, 12L, 12L, 12L), .Label = c("blue", "cyan", "darkgreen", 
                                                                                                                                                                                                                                                                             "red", "brown", "chartreuse", "green", "purple", "gold", 
                                                                                                                                                                                                                                                                             "tomato", "darkturquoise", "forestgreen"), class = "factor")), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                -120L)))

Below you can see code in Plotly that produce the chart shown below

library(plotly)
plt <- plot_ly(t_df3) %>%
  add_trace(x = ~deciles, y = ~value.x, type = 'bar',name = 'Left-scale',marker = list(color = ~color.x), name = ~variable.x) %>%
  add_trace(x = ~deciles, y = ~value, type = 'bar', name = 'Right-scale',marker = list(color = ~color), name = ~variable) %>%
  layout(
    xaxis = list(title = ''),
    yaxis = list(title = ''),
    legend = list(x = 0.01, y = 0.99),
    barmode = 'bar'
  )

enter image description here

Now I want to change the legend on this bar chart that is shown on the left side and to have bar chart as the bar chart shown below.

enter image description here

So can anybody help me how to solve this problem ?

CodePudding user response:

Here is a hack solution. A better one might exists.

library(plotly)
plt <- plot_ly(t_df3) %>%
  add_trace(x = ~deciles, y = ~value.x, type = 'bar', marker = list(color = ~color.x), name = ~variable.x) %>%
  add_trace(x = ~deciles   0.0004, y = ~value, type = 'bar', marker = list(color = ~color), name = ~variable) %>%
  layout(
    xaxis = list(title = ''),
    yaxis = list(title = ''),
    legend = list(x = 0.01, y = 0.99),
    barmode = 'bar'
  )
plt <- layout(p = plt, barmode = "stack")
plt

Second trace have an artificial x increase, traces are stacked in layout (info on hover has to be corrected).

  • Related