Drawing a Bar Chart with Plotly in R - r

I have one data frame (matrix 13x11).You can see pic below
There are also data :
# 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"))
My intention is to make two graph bar plots in R like pics below with package Plotly, which are drawn in Excel.
How do you draw a stacked bar plot with the Plotly package in R?

The problem is that your rows and columns have to be exchanged. Once it is done, you have to factor the order of First, Second etc in order for them to be printed in the right order
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
For the second graph I created a new dataframe where I make sure that every row has sum 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

Related

Adding Images to animations in plotly (R)

I am trying to create an animation where an image has to move together with the dot that you can see in this image:
I have a dataset about Formula 1 and I want to show the image of the car instead of the dot in the image.
Here you have a summary of my dataset:
And the code of the graph:
prep = data[data$year == 2021,] %>% split(.$date) %>% accumulate(., ~bind_rows(.x,.y))%>%
bind_rows(.id = "frame")
prep2 = data[data$year == 2021,] %>% split(.$date) %>%
bind_rows(.id = "frame")
prep%>%
plot_ly(x = ~name, y = ~points, color = ~factor(name)) %>%
add_lines(frame = ~as.Date(frame, format = '%Y-%m-%d'))%>%
add_markers(data = prep2, frame = ~as.Date(frame, format = '%Y-%m-%d'))%>%
layout(yaxis = list(title = 'Puntos'),showlegend = FALSE,xaxis = list(title = 'Fecha de la carrera',range = c(as.Date(min(data$date[data$year == 2021]), format="%d/%m/%Y"),as.Date(max(data$date[data$year == 2021]), format="%d/%m/%Y"))))%>%
animation_slider(currentvalue = list(prefix = "Carrera "))

Change xAxis format in highcharts using R

I´m trying to change the xAxis format. I have two data tables with values per month.
These are my tables:
data <- structure(list(DATA = c("2022/09", "2022/10", "2022/11"), AUM = c(31057073.67,
32045391.81, 32690375.86)), row.names = c(NA, -3L), class = c("data.table",
"data.frame"))
data2 <- structure(list(DATA = c("2022/09", "2022/10", "2022/11"), CUM_SUM = c(1047515038.76,
978350213.95, 879488195.72)), row.names = c(NA, -3L), class = c("data.table",
"data.frame"))
This is my graph:
highchart() %>%
hc_add_series(name = "AUM", id = "aum_line", data = data, hcaes(x = DATA, y = AUM), type = 'line') %>%
hc_add_series(name = 'PL_CUM', id = 'pl_cum_line', data = data2, hcaes(x = DATA, y = CUM_SUM), type = 'line') %>%
hc_plotOptions(column = list(dataLabels = list(enabled = F),
enableMouseTracking = T)) %>%
hc_chart(zoomType = 'xy') %>%
hc_exporting(enabled = TRUE)
My xAxis should be year and month.
I think this is what you're looking for? Your date data is still a string so I converted to date before adding an additional argument to handle the formatting
data$DATA <- as.Date(paste(data$DATA, "/01", sep=""))
data2$DATA <- as.Date(paste(data2$DATA, "/01", sep=""))
Gives:
> data$DATA
[1] "2022-09-01" "2022-10-01" "2022-11-01"
> data2$DATA
[1] "2022-09-01" "2022-10-01" "2022-11-01"
Then adding hc_xAxis(dateTimeLabelFormats = list(day = '%m %Y'), type = "datetime")
highchart() %>%
hc_add_series(
name = "AUM",
id = "aum_line",
data = data,
hcaes(x = DATA, y = AUM),
type = 'line'
) %>%
hc_add_series(
name = 'PL_CUM',
id = 'pl_cum_line',
data = data2,
hcaes(x = DATA, y = CUM_SUM),
type = 'line'
) %>%
hc_plotOptions(column = list(
dataLabels = list(enabled = F),
enableMouseTracking = T
)) %>%
hc_chart(zoomType = 'xy') %>%
hc_exporting(enabled = TRUE) %>%
hc_xAxis(dateTimeLabelFormats = list(day = '%m %Y'), type = "datetime")
Plot:
If you want to add your dates as a string, you should pass it as data.names and change xAxis.type to category.
Demo:
https://jsfiddle.net/BlackLabel/x8fm4njt/
In the case of dates, the more relevant xAxis type is datetime. In that case, x should be given as a timestamp.
Demo:
https://jsfiddle.net/BlackLabel/bts82m6u/
API Reference:
https://api.highcharts.com/highcharts/series.line.data
https://api.highcharts.com/highcharts/xAxis.type

Plotly subplots with shared legend in R

I have made twoplots using plotly, which are working fine individually, but when combined using subplot I can't seem to figure out how to combine the legends. I have tried to use showlegend = F in plot_ly in one of the plots, but this just removes it completely - what I want is to control both subplots with the same legend.
My code is as follows:
coronavirus_not_china <- coronavirus %>%
filter(!(country == "China"))
cases_not_china_plot <- coronavirus_not_china %>%
group_by(type, date) %>%
summarise(total_cases = sum(cases)) %>%
pivot_wider(names_from = type, values_from = total_cases) %>%
arrange(date) %>%
mutate(active = confirmed - death - recovered) %>%
mutate(active_total = cumsum(active),
recovered_total = cumsum(recovered),
death_total = cumsum(death)) %>%
plot_ly(x = ~ date,
y = ~ active_total,
name = 'Active',
fillcolor = '#1f77b4',
type = 'scatter',
mode = 'none',
stackgroup = 'one',
showlegend = F) %>%
add_trace(y = ~ death_total,
name = "Death",
fillcolor = '#E41317') %>%
add_trace(y = ~recovered_total,
name = 'Recovered',
fillcolor = 'forestgreen') %>%
layout(title = "Distribution of Covid19 Cases outside China",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Number of Cases", showgrid = T))
coronavirus_china <- coronavirus %>%
filter((country == "China"))
cases_china_plot <- coronavirus_china %>%
group_by(type, date) %>%
summarise(total_cases = sum(cases)) %>%
pivot_wider(names_from = type, values_from = total_cases) %>%
arrange(date) %>%
mutate(active = confirmed - death - recovered) %>%
mutate(active_total = cumsum(active),
recovered_total = cumsum(recovered),
death_total = cumsum(death)) %>%
plot_ly(x = ~ date,
y = ~ active_total,
name = 'Active',
fillcolor = '#1f77b4',
type = 'scatter',
mode = 'none',
stackgroup = 'one',
showlegend = T) %>%
add_trace(y = ~ death_total,
name = "Death",
fillcolor = '#E41317') %>%
add_trace(y = ~recovered_total,
name = 'Recovered',
fillcolor = 'forestgreen') %>%
layout(title = "Distribution of Covid19 Cases inside China",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Number of Cases", showgrid = F))
And I create the subplots as:
subplot(cases_not_china_plot, cases_china_plot, nrows = 2, margin = 0.05, shareX = T) %>%
layout(title="Coronavirus cases outside China and in China", ylab("Number of cases"))
I am quite new to R, so if there is a smarter way to do what I desire, please let me know.
With the above code, my output is:

Second yaxis plotly does not apply

Hey I am using the package plotly to create diagrams and i am trying to set up 2 different yaxis, but it does not work. Here is my code:
Schuldenquote_Argentinien <- c(80.3,70.8,62.1,53.8,55.4,43.5,38.9,40.4,43.5,44.7,52.6,53.1,57.1)
Schuldenquote_Brasilien <- c(68.7,65.9,63.8,61.9,65,63.1,61.2,62.2,60.2,62.3,72.6,78.3,84.1)
Schuldenquote_Chile <- c(7,5,3.9,4.9,5.8,8.6,11.1,11.9,12.7,15,17.3,21,23.5)
Schuldenquote_Mexico <- c(38.5,37.4,37.2,42.5,43.7,42,42.9,42.7,45.9,48.9,52.8,56.8,54)
Schuldenquote_Peru <- c(40,34.8,31.9,27.9,28.4,25.4,23,21.2,20,20.7,24,24.5,25.4)
Schulden_Argentinien <- c(159783,0,0,0,0,184396,0,0,0,0,337657,0,367360)*1000000
Schulden_Brasilien <- c(610776,0,0,0,0,1391222,0,0,0,0,1306199,0,1726603)*1000000
Schulden_Chile <- c(8.6,0,0,0,0,18.67,0,0,0,0,42.131,0,65.395)*1000000000
Schulden_Mexico <- c(337621,0,0,0,0,443485,0,0,0,0,618382,0,625687)*1000000
Schulden_Peru <- c(29923,0,0,0,0,37706,0,0,0,0,46037,0,54448)*1000000
Einwohner_Argentinien <- c(39.1455,0,0,0,0,41.2239,0,0,0,0,43.4178,0,44.271)*1000000
Einwohner_Brasilien <- c(186.1974,0,0,0,0,196.7963,0,0,0,0,205.9621,0,209.2883)*1000000
Einwohner_Chile <- c(16.1471,0,0,0,0,16.9934,0,0,0,0,17.7627,0,18.0547)*1000000
Einwohner_Mexico <- c(108.4722,0,0,0,0,117.3189,0,0,0,0,125.8909,0,129.1633)*1000000
Einwohner_Peru <- c(27.6104,0,0,0,0,29.3736,0,0,0,0,31.3767,0,32.1655)*1000000
Schuldenkopf_Argentinien <- Schulden_Argentinien/Einwohner_Argentinien
Schuldenkopf_Brasilien <- Schulden_Brasilien/Einwohner_Brasilien
Schuldenkopf_Chile <- Schulden_Chile/Einwohner_Chile
Schuldenkopf_Mexico <- Schulden_Mexico/Einwohner_Mexico
Schuldenkopf_Peru <- Schulden_Peru/Einwohner_Peru
library(lubridate)
Jahr <- c("01/01/2005","01/01/2006","01/01/2007","01/01/2008","01/01/2009","01/01/2010","01/01/2011","01/01/2012","01/01/2013","01/01/2014","01/01/2015","01/01/2016","01/01/2017")
Jahr <- as.Date(Jahr, format = "%d/%m/%Y", origin = "1970-01-01")
Schuldenquote_Lateinamerika <- data.frame(Jahr,Schuldenquote_Argentinien,Schuldenquote_Brasilien,Schuldenquote_Chile,Schuldenquote_Mexico,Schuldenquote_Peru)
Schuldenkopf_Lateinamerika <- data.frame(Jahr, Schuldenkopf_Argentinien, Schuldenkopf_Brasilien, Schuldenkopf_Chile, Schuldenkopf_Mexico, Schuldenkopf_Peru)
names(Schuldenquote_Lateinamerika) <- c("Jahr","ARG","BRA","CHL","MEX","PER")
names(Schuldenkopf_Lateinamerika) <- c("Jahr","ARG","BRA","CHL","MEX","PER")
library(plotly)
Schulden_Plot <- plot_ly(data = Schuldenkopf_Lateinamerika, y = ~ARG, x = ~Jahr, name = "ARG", type = "bar", color = toRGB("cornflowerblue")) %>%
add_trace(y = ~BRA, name = "BRA", marker = list(color = toRGB("forestgreen"))) %>%
add_trace(y = ~CHL, name = "CHL", marker = list(color = toRGB("firebrick1"))) %>%
add_trace(y = ~MEX, name = "MEX", marker = list(color = toRGB("gold2"))) %>%
add_trace(y = ~PER, name = "PER", marker = list(color = toRGB("darkorchid"))) %>%
add_lines(y = Schuldenquote_Lateinamerika$ARG, name = " ", mode = "lines", line = list(color = toRGB("cornflowerblue"), width = 2, yaxis = "y2")) %>%
add_lines(y = Schuldenquote_Lateinamerika$BRA, name = " ", mode = "lines", line = list(color = toRGB("forestgreen"), width = 2, yaxis = "y2")) %>%
add_lines(y = Schuldenquote_Lateinamerika$CHL, name = " ", mode = "lines", line = list(color = toRGB("firebrick1"), width = 2, yaxis = "y2")) %>%
add_lines(y = Schuldenquote_Lateinamerika$MEX, name = " ", mode = "lines", line = list(color = toRGB("gold2"), width = 2, yaxis = "y2")) %>%
add_lines(y = Schuldenquote_Lateinamerika$PER, name = " ", mode = "lines", line = list(color = toRGB("darkorchid"), width = 2, yaxis = "y2")) %>%
layout(yaxis = list(title = "Schulden pro Kopf in USD", linewidth = 3), yaxis2 = list(side = "right", title = "Schulden in Relation zum GDP" , linewidth = 3, overlaying = "y"), xaxis = list(zerolinewidth = 2), barmode = "group", showlegend = FALSE, autosize = TRUE)
Schulden_Plot
I tried several different ways to implement it in the code, but I am probably missing something?
I think you used wrong argument position yaxis should be outside the line = list.
Additionally, I adjust the margin to looks clear.
Full code is shown below :
Schulden_Plot <- plot_ly(data = Schuldenkopf_Lateinamerika, y = ~ARG, x = ~Jahr, name = "ARG", type = "bar", color = toRGB("cornflowerblue")) %>%
add_trace(y = ~BRA, name = "BRA", marker = list(color = toRGB("forestgreen"))) %>%
add_trace(y = ~CHL, name = "CHL", marker = list(color = toRGB("firebrick1"))) %>%
add_trace(y = ~MEX, name = "MEX", marker = list(color = toRGB("gold2"))) %>%
add_trace(y = ~PER, name = "PER", marker = list(color = toRGB("darkorchid"))) %>%
add_lines(y = Schuldenquote_Lateinamerika$ARG, name = " ", mode = "lines", line = list(color = toRGB("cornflowerblue"), width = 2), yaxis = "y2") %>%
add_lines(y = Schuldenquote_Lateinamerika$BRA, name = " ", mode = "lines", line = list(color = toRGB("forestgreen"), width = 2), yaxis = "y2") %>%
add_lines(y = Schuldenquote_Lateinamerika$CHL, name = " ", mode = "lines", line = list(color = toRGB("firebrick1"), width = 2), yaxis = "y2") %>%
add_lines(y = Schuldenquote_Lateinamerika$MEX, name = " ", mode = "lines", line = list(color = toRGB("gold2"), width = 2), yaxis = "y2") %>%
add_lines(y = Schuldenquote_Lateinamerika$PER, name = " ", mode = "lines", line = list(color = toRGB("darkorchid"), width = 2), yaxis = "y2") %>%
layout(margin = list(l=50, r= 50, b= 50, t= 50),yaxis = list(title = "Schulden pro Kopf in USD", linewidth = 3),
yaxis2 = list(side = "right", title = "Schulden in Relation zum GDP" , linewidth = 3, overlaying = "y"),
xaxis = list(zerolinewidth = 2), barmode = "group", showlegend = FALSE, autosize = TRUE)

Change color in barplot with plotly

I have a bar chart like below, which is drawn by the plotly package.
You can see the data and code below:
#CODE
#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"))
# Draw bar chart
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
The chart shows 13 different categories but my problem are the colors.
Car_sale_1 and Car_sale_11 have the same color, as do Car_sale_2 and Car_sale_12 etc.
My intention is to make a graph with unique colors per category. Can anybody help me fix this problem ?
I have reedited the code to make it much easier for you to see a possible way to manually select the colors. Also, do not add as many traces manually since you would get crazy if you had 100. Do it the following way:
library(plotly)
library(reshape2)
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)
t_df<-melt(t_df, id.vars = c("Number"))
t_df$color <- factor(t_df$variable, labels = c("blue", "red", "green", "yellow", "brown",
"black","orange","purple", "grey","white",
"chocolate", "coral", "cornflowerblue"))
p <- plot_ly(t_df, x = ~Number, y = ~value,
type = 'bar',
marker = list(color = ~color), name = ~variable) %>%
layout(yaxis = list(title = 'Count'), barmode = 'stack')
p

Resources