Reputation: 1580
I have created one line graph using plotly in Shiny. Now, in the line graph if I wish to add labels to they are overlapping with the line which makes it difficult to understand.
I am using the following code:
a = paste("$",prettyNum(de$Amount, big.mark = ",", scientific = FALSE), sep = "")
f <- list(
size = 14,
color = "Black",
fontface="bold"
)
xQuartAxis <- list(
title = "Month")
yQuartAxis <- list(
title = "Amount in $")
plot_ly(
x = as.vector(de$Month),
y = de$Amount,
text = paste(a), hoverinfo = "text", textinfo="text",showlegend = FALSE,
name = "Amount Paid",
mode = "lines+text"
)%>%
layout(title=paste("Monthly Amount paid by", clientName,"for the year",selectedYear, sep = " ") ,titlefont =f,t = 150, xaxis = xQuartAxis, yaxis = yQuartAxis)
})
I am getting this output:
Kindly tell me how can I separate the text from line graph for better visibility?
Thanks :)
Upvotes: 1
Views: 1286
Reputation: 93851
I'd suggest making the numbers stand out relative to the line. First, make the line transparent (say, alpha=0.2). The line will still be visible to guide your eye, but the numbers will be highlighted.
Second, truncate the numbers to thousands to match the y-scale. You don't need to display so many digits for the point markers.
Here's a ggplotly
version, as I'm not sure how to separately set the opacity for the text markers and the line with plot_ly
. You can add, say, opacity=0.3
inside plot_ly()
, but that makes the text markers transparent as well.
library(scales)
library(ggplot2)
library(plotly)
# Fake data
set.seed(395875)
de = data.frame(Month=factor(month.abb, levels=month.abb), Amount=rnorm(12, 12000, 1000))
ggplotly(
ggplot(de, aes(Month,Amount)) +
geom_line(aes(group=1), alpha=0.2, color="blue") +
geom_text(aes(label=paste0("$", sprintf("%1.1f", Amount/1000))), size=3.5) +
theme_bw() +
scale_y_continuous(limits=c(0, max(de$Amount)), breaks=seq(0,15000,5000),
labels=paste0("$",seq(0,15000,5000)/1000,"k")) +
labs(y="Amount ($000)")
)
Upvotes: 1