I am going to create bullet graph and found a sample code below, but there is a issue about text display at the left side of the graph, only part of the title shows up at the left side. How to set up the margin so that the full title can be observed? Thank you.
library(dplyr)
# Read data
library(jsonlite)
df <- fromJSON(txt = url("https://cdn.rawgit.com/plotly/datasets/master/BulletData.json"))
# Convert to conventional format
df <- t(apply(df, 1, function(vec){
vec <- unlist(vec)
return(vec)
}))
df <- as.data.frame(df, stringsAsFactors = F)
p <- list()
# Set line widths
innerwidth <- 10
outerwidth <- 25
library(plotly)
for(i in 1:nrow(df)){
attach(df[i,])
p[[i]] <-
# Ranges3
plot_ly() %>%
add_segments(x = 0,
xend = as.numeric(ranges3),
y = title,
yend = title,
name = "Range3",
hoverinfo = "x",
line = list(color = "#eeeeee", width = outerwidth)) %>%
# Ranges2
add_segments(x = 0,
xend = as.numeric(ranges2),
y = title,
yend = title,
hoverinfo = "x",
line = list(color = "#dddddd", width = outerwidth)) %>%
# Ranges1
add_segments(x = 0,
xend = as.numeric(ranges1),
y = title,
yend = title,
hoverinfo = "x",
line = list(color = "#cccccc", width = outerwidth)) %>%
# Measure2
add_segments(x = 0,
xend = as.numeric(measures2),
y = title,
yend = title,
hoverinfo = "x",
line = list(color = "#b0c4de", width = innerwidth)) %>%
# Measure1
add_segments(x = 0,
xend = as.numeric(measures1),
y = title,
yend = title,
hoverinfo = "x",
line = list(color = "#4682b4", width = innerwidth)) %>%
# Marker
add_markers(x = as.numeric(markers),
y = title,
hoverinfo = "x",
marker = list(color = "black", symbol = "diamond-tall", size = 10)) %>%
layout(showlegend = F,
xaxis = list(title = "", showgrid = F, zeroline = F,
range = c(-(as.numeric(ranges3)/10), as.numeric(ranges3)),
ticklen = 7,
tickfont = list(family = "Arial", size = 10),
tickcolor = "#cccccc"),
yaxis = list(title = "",
showgrid = F,
zeroline = F,
showticklabels = F))
detach(df[i,])
}
pp <- subplot(p[[1]], p[[2]], p[[3]], p[[4]], p[[5]],
nrows = 5,
margin = c(0, 0, 0.1, 0))
# Add Y-Axis titles
pp <- layout(pp,
annotations = list(
list(xref = "paper", yref = "paper",
x = 0, y = 0.05, ax = 0, ay = 0, xanchor = "right",
text = paste0("<b>", df[1,1], "</b>","<br>",
'<span style = "color:grey; font-size:75%">',
df[1,2], "</span>"),
align = "right",
font = list(family = "arial",
size = 15)),
list(xref = "paper", yref = "paper",
x = 0, y = 0.25, ax = 0, ay = 0, xanchor = "right",
text = paste0("<b>", df[2,1], "</b>","<br>",
'<span style = "color:grey; font-size:75%">',
df[2,2], "</span>"),
align = "right",
font = list(family = "arial",
size = 15)),
list(xref = "paper", yref = "paper",
x = 0, y = 0.45, ax = 0, ay = 0, xanchor = "right",
text = paste0("<b>", df[3,1], "</b>","<br>",
'<span style = "color:grey; font-size:75%">',
df[3,2], "</span>"),
align = "right",
font = list(family = "arial",
size = 15)),
list(xref = "paper", yref = "paper",
x = 0, y = 0.65, ax = 0, ay = 0, xanchor = "right",
text = paste0("<b>", df[4,1], "</b>","<br>",
'<span style = "color:grey; font-size:75%">',
df[4,2], "</span>"),
align = "right",
font = list(family = "arial",
size = 15)),
list(xref = "paper", yref = "paper",
x = 0, y = 0.90, ax = 0, ay = 0, xanchor = "right",
text = paste0("<b>", df[5,1], "</b>","<br>",
'<span style = "color:grey; font-size:75%">',
df[5,2], "</span>"),
align = "right",
font = list(family = "arial",
size = 15))))
pp