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
Related
actual output which is cluttered But When I click on the camera icon of plotly , I get the whole screenshot which I wanted . I have attached the screenshot of it . But when I use orca or save_image , I get image which is overlapped. I am trying to do this in R. Is there any suggestions? I have attached both the screenshots.
fig <- fig %>% layout(title = NULL,annotations = list(text = " ", showarrow = F,x = 0.5 , y = 1.05,
xref = "paper",
yref = "paper",
xanchor = "center",
yanchor = "top" ),showlegend = F)
dload$modelresult<-fig
# html <- "dtable.html"
# saveWidget(fig, html)
# working_direct<-getwd()
# final_dest_img<-paste0(work[enter image description here][1]ing_direct,"/d1.png")
# webshot(html, final_dest_img)
save_image(fig, file = "C:/Users/User/Downloads/image4.png")
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
Is there a way to add an arrow image onto the side of a barchart, based on some underlying information?
I have the following script:
library(tidyverse)
library(plotly)
data <- tibble(url = c("google.com","yahoo.com","yandex.com"), values = c(500,400,300), change = c(0.5,-0.9,0.1))
data
data %>%
plot_ly(x = data$values,
y = data$url,
type = "bar")
This produces a simple bar chart in plotly, I would like to add some arrows next to each bar chart to show that the values have either decreased or increased based on the data$change column. so if the number is positive a arrow turned up and green, if negative, then an arrow that is red and pointed down
Is this possible?
If none of this possible - is there a way to overlay just the text of the percentage change instead next to the bar charts?
Hopefully this would go onto a shiny app, so even if there is a way of embedding or overlaying a html element would be useful!
If there is an alternative in ggplot, I would also be interested.
Hopefully something that looks like this:
JUST TO UPDATE IN REGARDS TO BELOWS ANSWER THE CODE WOULD BE:
`library(tidyverse)
library(plotly)
data <- tibble(url = c("google.com","yahoo.com","yandex.com"), values = c(500,400,300), change = c(0.5,-0.9,0.1))
data
data %>%
plot_ly(x = data$values,
y = data$url,
type = "bar")
library(dplyr)
data <- data %>%
mutate(x.start = values + 50,
y.end = seq(0,2,1),
y.start = y.end + 0.5) %>%
mutate(y.start.new = case_when(sign(change) == -1 ~ y.end,
TRUE ~ y.start),
y.end.new = case_when(sign(change) == -1 ~ y.start,
TRUE ~ y.end)
)
data %>%
plot_ly(x = data$values,
y = data$url,
type = "bar") %>%
add_markers(~values, ~url) %>%
add_annotations(x = ~x.start[change == "up"],
y = ~y.start.new[change == "up"],
xref = "x", yref = "y",
axref = "x", ayref = "y",
text = "",
showarrow = T,
ax = ~x.start[change == "up"],
ay = ~y.end.new[change == "up"],
arrowcolor = "green") %>%
add_annotations(x = ~x.start[change == "down"],
y = ~y.start.new[change == "down"],
xref = "x", yref = "y",
axref = "x", ayref = "y",
text = "",
showarrow = T,
ax = ~x.start[change == "down"],
ay = ~y.end.new[change == "down"],
arrowcolor = "red")
`
But you do not produce the same output - only one arrow appears?
You can add annotations. First specify arrow start and end positions: Please note that in the data you provide you have yahoo decreasing, not yandex like in your plot.
library(dplyr)
data <- data %>%
mutate(x.start = values + 50,
y.end = seq(0,2,1),
y.start = y.end + 0.5) %>%
mutate(y.start.new = case_when(sign(change) == -1 ~ y.end,
TRUE ~ y.start),
y.end.new = case_when(sign(change) == -1 ~ y.start,
TRUE ~ y.end)
) %>%
mutate(change_dir = case_when(sign(change) == -1 ~ "down",
sign(change) == 1 ~ "up"))
Then plot using add_annotations
data %>%
plot_ly(x = data$values,
y = data$url,
type = "bar") %>%
add_markers(~values, ~url) %>%
add_annotations(x = ~x.start[change_dir == "up"],
y = ~y.start.new[change_dir == "up"],
xref = "x", yref = "y",
axref = "x", ayref = "y",
text = "",
showarrow = T,
ax = ~x.start[change_dir == "up"],
ay = ~y.end.new[change_dir == "up"],
arrowcolor = "green") %>%
add_annotations(x = ~x.start[change_dir == "down"],
y = ~y.start.new[change_dir == "down"],
xref = "x", yref = "y",
axref = "x", ayref = "y",
text = "",
showarrow = T,
ax = ~x.start[change_dir == "down"],
ay = ~y.end.new[change_dir == "down"],
arrowcolor = "red")
I'm trying to create an annotated Sankey diagram. I'd like the final version to look along the lines of this manually annotated diagram:
The easy part of getting the Sankey diagram:
sankey_diagram <- plot_ly(
type = "sankey",
orientation = "h",
node = list(
label = c("Node_A_1", "Node_A_2", "Node_B_2", "Node_B_3", "Node_C_1", "Node_C_2"),
color = c("blue", "blue", "blue", "blue", "blue", "blue"),
pad = 15,
thickness = 35,
line = list(
color = "black",
width = 0.5
)
),
link = list(
source = c(0,1,0,2,3,3),
target = c(2,3,3,4,4,5),
value = c(8,4,2,8,4,2)
)
) %>%
layout(
font = list(
size = 15
)
)
At first I thought that if I want to get annotated "columns" I should turn to the annotations section of the plotly's documentation. The problem with annotations is that they are spatially limited to (at least I think so) the area of the graph. This is the code in the annotation-based approach:
# properties that hide the axes
ax <- list(
title = "",
zeroline = FALSE,
showline = FALSE,
showticklabels = FALSE,
showgrid = FALSE
)
sankey_diagram %>%
add_annotations(
x=-1,
y=-5,
xref = "x",
yref = "y",
text = "Column A",
xanchor = 'right',
showarrow = F
) %>%
add_annotations(
x=0,
y=-5,
xref = "x",
yref = "y",
text = "Column B",
xanchor = 'right',
showarrow = F
) %>%
add_annotations(
x=1,
y=-5,
xref = "x",
yref = "y",
text = "Column C",
xanchor = 'right',
showarrow = F
) %>% add_annotations(
x=1,
y=1,
xref = "x",
yref = "y",
text = "",
xanchor = 'right',
showarrow = F
) %>%
layout(xaxis = ax, yaxis = ax)
The problem is that annotations land on the bottom of the graph but not under it.
The second approach is based on subplots. I create two subplots - the first one is Sankey, the other one is an empty one except for the annotations - and put them one in a row:
columns_plot <- plot_ly() %>% add_annotations(
x=-1,
y=-5,
xref = "x",
yref = "y",
text = "Column A",
xanchor = 'right',
showarrow = F
) %>%
add_annotations(
x=0,
y=-5,
xref = "x",
yref = "y",
text = "Column B",
xanchor = 'right',
showarrow = F
) %>%
add_annotations(
x=1,
y=-5,
xref = "x",
yref = "y",
text = "Column C",
xanchor = 'right',
showarrow = F
) %>% add_annotations(
x=1,
y=1,
xref = "x",
yref = "y",
text = "",
xanchor = 'right',
showarrow = F
) %>%
layout(xaxis = ax, yaxis = ax)
p <- subplot(sankey_diagram, columns_plot, nrows = 2, shareX = TRUE, margin = 0.1)
p %>%
layout(xaxis = ax, yaxis = ax)
For some strange reason plotly puts the columns_plot on top of the sankey_diagram. I suspect that the second approach is the correct one but still I can't get the result depicted in the first paragraph of this question.
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)