Reputation: 8404
In the plotly chart I create with below the legend always covers the labels of second y axis. How can I set the distance between them?
Week<-structure(c(18323, 18330, 18337, 18344, 18351, 18358, 18365,
18372, 18379, 18386, 18393, 18400, 18407, 18414, 18421, 18428,
18435, 18442, 18449, 18456, 18463, 18470, 18477, 18484, 18491,
18498, 18505, 18512, 18519, 18526, 18533, 18540, 18547, 18554,
18561, 18568, 18575, 18582, 18589, 18596, 18603, 18610, 18617,
18624, 18631, 18638, 18645, 18652, 18659, 18666, 18673, 18680,
18687, 18694, 18701, 18708, 18715, 18722, NA), class = "Date")
Bob<-c(NA, 12, 28, 89, 205, 311, 367, 419, 536, 673, 787, 996, 1501,
2091, 2836, 3971, 5429, 7422, 9653, 12205, 15096, 19962, 23567,
28432, 33051, 37347, 43390, 49897, 54851, 60913, 67073, 72769,
79629, 84063, 88398, 89579, 88464, 85595, 81697, 74943, 67632,
58226, 53371, 49759, 51508, 55515, 58813, 62240, 62627, 62646,
61285, 54438, 49614, 46721, 44554, 48151, 54014, 68891, 47176
)
Anna<-c(NA, 12, 28, 89, 205, 311, 367, 419, 536, 673, 787, 996, 1501,
2091, 2836, 3971, 5429, 7422, 9653, 12205, 15096, 19962, 23567,
28432, 33051, 37347, 43390, 49897, 54851, 60913, 67073, 72769,
79629, 84063, 88398, 89579, 88464, 85595, 81697, 74943, 67632,
58226, 53371, 49759, 51508, 55515, 58813, 62240, 62627, 62646,
61285, 54438, 49614, 46721, 44554, 48151, 54014, 68891, 47176
)*50
re<-data.frame(Week,Bob,Anna)
re<-re %>% group_by(month_year = format(Week, '%Y-%b')) %>% summarise(across(c(Bob,Anna), sum, na.rm =TRUE))
colnames(re)[1]<-"Week"
ay <- list(
overlaying = "y",
side = "right",
title = "Second"
)
tempNames <- c("Bob", "Anna")
tempNamesV2 <- tempNames[order(tempNames)]
# plotlyObjList <-
p <- plot_ly(re)
for(i in seq_along(tempNamesV2)){
if(i == 1){
p <- add_bars(p, x = ~Week, y = re[[tempNamesV2[i]]], name = tempNamesV2[i],
marker = list(color = "#3E5B84"), yaxis = "y", offsetgroup = i,
text = ~ paste("<b>Country:</b>", "kjj", "<br><b>Date:</b>",Week ),
hovertemplate = paste('%{text}<extra></extra>'))
} else if (i == 2){
p <- add_bars(p, x = ~Week, y = re[[tempNamesV2[i]]], name = tempNamesV2[i],
marker = list(color = "#6BBABF"), yaxis = "y2", offsetgroup = i,
text = ~ paste("<b>Country:</b>", "kjj", "<br><b>Date:</b>",Week ),
hovertemplate = paste('%{text}<extra></extra>'))
}
}
p <- p %>% layout(yaxis2 = ay,
xaxis = list(title = "Date"),
yaxis = list(title = "i"),
margin = list(b = 100, t=50, r=50),
barmode = 'group',
legend=list(title=list(text='<b> Country </b>')))
p
Upvotes: 2
Views: 1288
Reputation: 506
I think this would do the trick:
(...)
p <- p %>%
layout(yaxis2 = ay,
xaxis = list(title = "Date"),
yaxis = list(title = "i"),
margin = list(b = 100, t=50, r=50),
barmode = 'group',
legend=list(title=list(text='<b> Country </b>')))%>%
layout(legend = list(x = 1.05, y = 1))
p
Upvotes: 3