I am quite new to plotly and am currently trying to create three different plotly bars for three indicators: Gini and Palma coefficients and S80/S20. I have the following two problems:
Thank you very much for any help. Complete code here:
library(plotly)
gini<-as.data.frame(c(0.4, 0.3, 0.2))
palma<-as.data.frame(c(1.2, 1.1, 1.0))
S<-as.data.frame(c(5.2, 5.1, 5.0))
p <- plot_ly(gini, x = "Coeff. di Gini", y = ~gini$`c(0.4, 0.3, 0.2)`, alpha = 0.7)
q<- plot_ly(palma, x="Coeff. di Palma", y=~palma$`c(1.2, 1.1, 1)`, alpha=0.7)
r<-plot_ly(S, x="S80/S20", y=~S$`c(5.2, 5.1, 5)`, alpha=0.7)
subplot(
add_trace(p, name = "Coeff. di Gini", x = "Coeff. di Gini", y = ~gini$`c(0.4, 0.3, 0.2)`, symbol = ~gini$`c(0.4, 0.3, 0.2)`, symbols = c('circle','x','o'),
marker = list(size = 10),type = 'scatter', mode = 'lines+markers'),
add_trace(q, name="Coeff. di Palma", x = "Coeff. di Palma", y = ~palma$`c(1.2, 1.1, 1)`, symbol = ~palma$`c(1.2, 1.1, 1)`, symbols = c('circle','x','o'),
marker = list(size = 10), type = 'scatter', mode = 'lines+markers'),
add_trace(r, name="S80/S20", x = "S80/S20", y = ~S$`c(5.2, 5.1, 5)`, symbol = ~S$`c(5.2, 5.1, 5)`, symbols = c('circle','x','o'),
marker = list(size = 10), type = 'scatter', mode = 'lines+markers')
)
the legend shows the name of the indicators however I would like it to show for the three different symbols three different years, let's say: circle=2018, x=2019, o=2020. Is it possible to adjust it?
You could add traces which only serve to provide a legend but no points are shown.
visible = 'legendonly'
and hide the legend of your 'real' traces.
showLegend = F
can the three plots appear with some distance between each other (say 1-2 cm of distance between graphs)?
If you add margin
to your subplot call you specify the distance between the subplots. Needs a bit of fiddling but you could try
margin = 0.1
library(plotly)
gini <- as.data.frame(c(0.4, 0.3, 0.2))
palma <- as.data.frame(c(1.2, 1.1, 1.0))
S <- as.data.frame(c(5.2, 5.1, 5.0))
p <- plot_ly(x = "Coeff. di Gini",
y = gini$`c(0.4, 0.3, 0.2)`,
alpha = 0.7,
name = "Coeff. di Gini",
symbol = gini$`c(0.4, 0.3, 0.2)`,
symbols = c('circle','x','o'),
marker = list(size = 10),
type = 'scatter',
mode = 'lines+markers',
showlegend=F) %>%
add_markers(name = "2018",
marker = list(size = 10, symbol = 'circle'),
showlegend=T, visible='legendonly') %>%
add_markers(name = "2019",
marker = list(size = 10, symbol = 'x'),
showlegend=T, visible='legendonly') %>%
add_markers(name = "2020",
marker = list(size = 10, symbol = 'circle-open'),
showlegend=T, visible='legendonly')
q <- plot_ly(x="Coeff. di Palma",
y=palma$`c(1.2, 1.1, 1)`,
alpha=0.7,
name="Coeff. di Palma",
symbol = palma$`c(1.2, 1.1, 1)`,
symbols = c('circle','x', 'o'),
marker = list(size = 10),
type = 'scatter',
mode = 'lines+markers',
showlegend=F)
r <- plot_ly(x="S80/S20",
y=S$`c(5.2, 5.1, 5)`,
alpha=0.7,
name="S80/S20",
symbol = ~S$`c(5.2, 5.1, 5)`,
symbols = c('circle','x','o'),
marker = list(size = 10),
type = 'scatter',
mode = 'lines+markers',
showlegend=F)
subplot(
add_trace(p),
add_trace(q),
add_trace(r),
margin = 0.1
) %>% layout(showlegend=T)