Related
my title alignment is not working left in chart. how to align title left.
I am trying to align title left in my graph below but not working for me please help what am i missing here or which line i have to update
df <- data.frame("LOC" =c("CA","NY","WA","TX"),
"TAX" = c(3421.00,5640.00,7880.32,4569.00))
ttl ="Tax collection"
g <- plot_ly(df, x =~TAX, y = ~ LOC, type = 'bar',width = 1200, height =900, showlegend=F
, text = ~paste0(roundUp(TAX*100), "%"), textposition = "outside",
marker = list(color = "blue"))
tit <- list(
text = ttl,
font = cht_ttl,
xref = "paper",
yref = "paper",
xanchor = "right",
x = 0.1,
y = 1,
showarrow = FALSE
)
ax <- list(
title = "",
showgrid = FALSE,
range = c(0, 1)
)
chart <- g %>% layout(annotations = tit,
xaxis = ax,
yaxis = list (title = " "),font=chrt_title, showline = TRUE)
chart
Use xanchor = 'left' with x value less than 0.
library(plotly)
tit <- list(
text = ttl,
font = chrt_title,
xref = "paper",
yref = "paper",
xanchor = "left",
x = -0.04,
y = 1,
showarrow = FALSE
)
chart <- g %>% layout(annotations = tit,
xaxis = ax,
yaxis = list (title = " "),
font=chrt_title, showline = TRUE)
chart
I have an issue with plotly rendering in shiny app:
As you can see in the picture ; the lines (from layout/shapes in my plot construction) don't go to the right end of the plot (the lines' title (from add_annotation) are not well placed too.
There is my code for plot construction :
plotXbarD<-plot_ly(
data = triBOE(),
x=~titre
)
plotXbarD <- plotXbarD %>%
add_trace(y=triBOE()$Drying.Analysis.Crush.Strenght.Mean,type = 'scatter', mode = 'lines+markers',marker=list(color="red"))
plotXbarD<-plotXbarD %>%
layout(xaxis = list( title= "",tickangle = 45,tickfont=list(size=10)),
shapes=list(
list(type='line', x0=min(triBOE()$titre) , x1=max(triBOE()$titre) , y0=DlscM(),
y1=DlscM(), line=list(dash='dash', width=2,color='red')),
list(type='line', x0=min(triBOE()$titre) , x1=max(triBOE()$titre) , y0=DcibleM(),
y1=DcibleM(), line=list(dash='dash', width=2,color='green')),
list(type='line', x0=min(triBOE()$titre) , x1=max(triBOE()$titre) , y0=DlicM(),
y1=DlicM(), line=list(dash='dash', width=2,color='red'))
)
)
plotXbarD <- plotXbarD %>% add_annotations(
x=max(triBOE()$titre),
y=DlscM(),
xref = "x",
yref = "y",
text = "limite superieure",
xanchor = 'left',
showarrow = F
)
plotXbarD <- plotXbarD %>% add_annotations(
x=max(triBOE()$titre),
y=DcibleM(),
xref = "x",
yref = "y",
text = "Cible",
xanchor = 'left',
showarrow = F
)
plotXbarD <- plotXbarD %>% add_annotations(
x=max(triBOE()$titre),
y=DlicM(),
xref = "x",
yref = "y",
text = "limite inferieur",
xanchor = 'left',
showarrow = F
)
})
When I run this in rstudio there is no problem with render; but in shiny this going wrong!!
Is it a good thing to use plotly in shiny or is it better to use renderplotly with ggplot construction?
There is an data exemple
PO.Label ;Drying.Batch.Name ;Drying.Start.Time.1 ;Drying.Analysis.Crush.Strenght.Mean ;Drying.Analysis.Crush.Strenght.Std.Deviation ;Drying.Analysis.Elasticity.Mean ;Drying.Analysis.Elasticity.Std.Deviation
1236675;B37-40;2018/06/26 13:14:37;1.40;0.11;43.89;1.79
1245515;B41-44;2018/06/26 06:14:55;1.34;0.20;42.79;3.40
1245515;B41-44;2018/06/26 06:14:55;1.24;0.25;39.75;4.06
1245765;B05-08;2018/06/26 05:28:56;1.37;0.25;40.95;3.71
1245529;B01-06;2018/06/24 12:47:27;1.56;0.39;42.86;4.94
1240251;B33-36;2018/06/26 15:59:08;1.10;0.48;37.22;6.26
1236675;B41-44;2018/06/26 16:16:34;1.48;0.40;43.49;6.04
1240180;B43-48;2018/06/26 00:19:07;1.90;0.26;42.19;4.87
1245765;B13-16;2018/06/26 07:56:17;1.33;0.21;40.71;3.47
I adding an « titre » column by pasting ID/Drying start time/po label/driying batch name
I am trying to create an empty plotly with a message telling why the plot is empty as a title. I would like to center the message both horizontally and vertically.
I am missing the vertical center :
empty_plot <- function(title = NULL){
p <- plotly_empty(type = "scatter", mode = "markers") %>%
config(
displayModeBar = FALSE
) %>%
layout(
title = title
)
return(p)
}
empty_plot("Why it is empty")
Just found the option yref = "paper", with y = 0.5 for middle position:
empty_plot <- function(title = NULL){
p <- plotly_empty(type = "scatter", mode = "markers") %>%
config(
displayModeBar = FALSE
) %>%
layout(
title = list(
text = title,
yref = "paper",
y = 0.5
)
)
return(p)
}
empty_plot("Why it is empty")
I'm creating a 3D scatterplot in plotly and want to have a title that is a few sentences long (in order to describe the figure), with the title left-justified, not interfering with the figure itself, and not being cut off by the plot area.
The code below incorporates a stackoverflow answer on left-justified titles in plotly, but it does not help when you have a long title that needs to go over 3-4 lines.
Using \n makes the title's text go to a second line, but it still gets cut off by the plot area and does not left-justify.
library(plotly)
metric1 <- c(116,230,120,200,210,188,130,99,101,120)
metric2 <- c(79,109,120,95,130,98,118,130,140,88)
metric3 <- c(30,28,42,22,6,2,17,43,20,28)
class <- c(3,4,2,1,1,4,4,3,2,3)
df <- data.frame(metric1,metric2,metric3,class)
my_colors=c("red", "blue", "green", "#000000")[df$class]
p <- plot_ly(df,
x = ~metric1,
y = ~metric2,
z = ~metric3, text = class, type = "scatter3d",
mode = "markers", marker = list(color = my_colors)) %>%
add_annotations(
x=0, y=1.15,
text="Figure: The title of the figure will explain what information can be gleaned from the figure. Then the next sentence, which is still in this title, will elaborate on implications from the results. I want to be able to extend this as needed.",
font=list(size=17)
) %>%
layout(title = FALSE,
scene = list(xaxis = list(title = 'metric 1', range = c(0,300)),
yaxis = list(title = 'metric 2', range = c(0,150)),
zaxis = list(title = 'metric 3', range = c(0,100))), showlegend = FALSE)
p
The output I get shows only the end of the title and cuts it off:
Thanks for any help.
I think with the help of align = "left" and /n will give you what you want:
metric1 <- c(116,230,120,200,210,188,130,99,101,120)
metric2 <- c(79,109,120,95,130,98,118,130,140,88)
metric3 <- c(30,28,42,22,6,2,17,43,20,28)
class <- c(3,4,2,1,1,4,4,3,2,3)
df <- data.frame(metric1,metric2,metric3,class)
my_colors=c("red", "blue", "green", "#000000")[df$class]
p <- plot_ly(df,
x = ~metric1,
y = ~metric2,
z = ~metric3, text = class, type = "scatter3d",
mode = "markers", marker = list(color = my_colors)) %>%
add_annotations(
x=0.4, y=0.9,
align = "left",
# text="Figure: The title of the figure will explain what information can be gleaned from the figure. Then the next sentence, which is still in this title, will elaborate on implications from the results. I want to be able to extend this as needed.",
text=paste("Figure: The title of the figure will explain what information can be gleaned from the figure", "Then the next sentence, which is still in this title", "I want to be able to extend this as needed.", sep="\n") ,
font=list(size=17)
) %>%
layout(title = FALSE,
scene = list(xaxis = list(title = 'metric 1', range = c(0,300)),
yaxis = list(title = 'metric 2', range = c(0,150)),
zaxis = list(title = 'metric 3', range = c(0,100))), showlegend = FALSE)
p
I have two density plots created by R's plotly:
set.seed(1)
dens.1 <- density(runif(1000,0,100))
dens.2 <- density(runif(1000,100,10000))
df.1 <- data.frame(x=dens.1$x,y=dens.1$y)
df.2 <- data.frame(x=dens.2$x,y=dens.2$y)
library(plotly)
pl.1 <- plot_ly(x=~df.1$x,y=~df.1$y,type='scatter',mode='lines',line=list(color="#A9A9A9")) %>%
layout(xaxis=list(title="Count",zeroline=F),yaxis=list(title="Density",zeroline=F)) %>%
layout(title="Data1")
pl.2 <- plot_ly(x=~df.2$x,y=~df.2$y,type='scatter',mode='lines',line=list(color="#A9A9A9")) %>%
layout(xaxis=list(title="Count",zeroline=F),yaxis=list(title="Density",zeroline=F)) %>%
layout(title="Data2")
Now, I'd like to plot them together. So I used plotly's subplot:
subplot(list(pl.1,pl.2),nrows=1,shareX=F,shareY=F,titleX=T,titleY=T) %>% layout(showlegend=F)
but that only retains the plot title of pl.2:
How do I get both titles on that plot?
To get what you want and according to this (https://rpubs.com/bcd/subplot-titles) you could use:
a <- list(
text = "Data 1",
font = f,
xref = "paper",
yref = "paper",
yanchor = "bottom",
xanchor = "center",
align = "center",
x = 0.5,
y = 1,
showarrow = FALSE
)
b <- list(
text = "Data 2",
font = f,
xref = "paper",
yref = "paper",
yanchor = "bottom",
xanchor = "center",
align = "center",
x = 0.5,
y = 1,
showarrow = FALSE
)
pl.1 <- plot_ly(x=~df.1$x,y=~df.1$y,type='scatter',mode='lines',line=list(color="#A9A9A9")) %>%
layout(xaxis=list(title="Count",zeroline=F),yaxis=list(title="Density",zeroline=F)) %>%
layout(annotations = a)
pl.2 <- plot_ly(x=~df.2$x,y=~df.2$y,type='scatter',mode='lines',line=list(color="#A9A9A9")) %>%
layout(xaxis=list(title="Count",zeroline=F),yaxis=list(title="Density",zeroline=F)) %>%
layout(annotations = b)
subplot(list(pl.1,pl.2),nrows=1,shareX=F,shareY=F,titleX=T,titleY=T) %>% layout(showlegend=F)