chart alignment in plotly bar chart - r

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

Related

How to have a single common colorbar for two faceted framed (time-varying) plotly sf maps

I have paired map display, with common frame slider, that currently looks as follows:
I would like instead for there to be a common colorbar for both sub-maps, going the full distance down the display. (Instead of, at present, two colorbars a and b, which both go down half the display height.)
Bonus kudos for a solution that allows the limits of the colorbars to be set too!
The code to reproduce the figure:
library(sf)
library(plotly)
library(tidyr)
library(dplyr)
fname <- system.file("shape/nc.shp", package="sf")
nc <- st_read(fname)
plot_ly(nc)
num_features <- dim(nc)[1]
# for each feature, imagine 10 years of data
years <- 2000:2010
# for each feature, and for each of the 10 years, imagine there are two variables a and b
nc_names <- nc[,"NAME"] %>% st_drop_geometry()
set.seed(12)
fake_dta <-
expand_grid(nc_names, year = years) %>%
mutate(a = runif(n()),
b = runif(n())
) %>%
mutate(
text_a = glue::glue("In {NAME} in {year}, a was {round(a, 2)}"),
text_b = glue::glue("In {NAME} in {year}, b was {round(b, 2)}")
)
# Join the data back
nc2 <- nc %>%
left_join(fake_dta)
# Question is how to plot both a and b as two maps, side by side, sharing the same
# time slider
# to map a
map_a <-
plot_ly(nc2) %>%
add_sf(
split = ~NAME,
color = ~a,
frame = ~year,
stroke = I("black"),
text = ~text_a,
hoveron = "fills", hoverinfo = "text",
showlegend = FALSE,
type = "scatter",
mode = "lines"#,
) %>%
add_annotations(
text = "A",
x = 0.5, y = 1,
yref = "paper",
xref = "paper",
yanchor = "bottom",
valign = "middle",
align = "center",
showarrow = FALSE,
font = list(size = 15)
) %>%
layout(
showlegend = FALSE,
shapes = list(
type = "rect",
x0 = 0,
x1 = 1,
xref = "paper",
y0 = 0,
y1 = 16,
yanchor = 1,
yref = "paper",
ysizemode = "pixel",
fillcolor = toRGB("gray80"),
line = list(color = "transparent")
)
)
map_a
# to map b
map_b <-
plot_ly(nc2) %>%
add_sf(
split = ~NAME,
color = ~b,
frame = ~year,
stroke = I("black"),
text = ~text_b,
hoveron = "fills", hoverinfo = "text",
showlegend = FALSE,
type = "scatter",
mode = "lines"#,
) %>%
add_annotations(
text = "B",
x = 0.5, y = 1,
yref = "paper",
xref = "paper",
yanchor = "bottom",
valign = "middle",
align = "center",
showarrow = FALSE,
font = list(size = 15)
) %>%
layout(
showlegend = FALSE,
shapes = list(
type = "rect",
x0 = 0,
x1 = 1,
xref = "paper",
y0 = 0,
y1 = 16,
yanchor = 1,
yref = "paper",
ysizemode = "pixel",
fillcolor = toRGB("gray80"),
line = list(color = "transparent")
)
)
map_b
# These produce warnings: line.color doesn't (yet) support data arrays
# Only one fillcolor per trace allowed
# and both together
subplot(map_a, map_b)

R Plotly add image watermark

I would like to have an image/watermark added to a double y-axis plot, somehow my code doesnt work..... How is this possible? It doesnt error but the image is not added
df <- data.frame(x = 1:50,
y1 = c(20:5,6:10,9:3,4:20, 19:15),
y2 = c(100:115,200:204,50:44,4:20, 66:62))
And my code
plot_ly(df) %>%
add_lines(type = 'scatter', mode = "lines",
name = "test",
x = ~x, y = ~y1,
line = list(color = '#999999'),
hoverinfo = "text",
text = ~paste(round(y1, 1), x)) %>%
add_lines(type = 'scatter', mode = "lines",
name = "test", yaxis = 'y2',
x = ~x, y = ~y2,
line = list(color = '#CC79A7'),
hoverinfo = "text",
text = ~paste(round(y2, 1), x)) %>%
#layout
layout(title = "test",
xaxis = list(titel = "Date",
rangeslider = list(thickness = 0.05)),
yaxis = list(side = 'left', title = 'test',
showgrid = F, zeroline = F, type = "log",
showline = T),
images = list(source = "https://rstudio.com/wp-content/uploads/2018/10/RStudio-Logo.png",
xref = "paper",
yref = "paper",
xanchor = "left",
yanchor = "bottom",
x= 4,
y=4,
sizex = 5,
sizey = 5,
opacity = 0.8),
yaxis2 = list(side = 'right', overlaying = "y",
title = 'test',
showgrid = F, zeroline = F,
showline = T))
The problem is that you have no access to the Rstduio-logo.png.
Create a www folder
Put a .png in this www folder let's say I call it test.png
Then simply:
layout(images = list(source = "test.png"))

R plotly Sankey diagram with annotated columns

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.

Two X-axis in Plotly for R

https://community.plot.ly/t/how-to-plot-multiple-x-axis-in-plotly-in-r/3014/3?u=him4u324
I have posted my question on Plotly community as well
I am trying to display two x-axis with common Y-axis on plotly for R. I was able to do so as well but starting point for each x-axis is separated from each other Whereas I wish them to be represented a common y-axis.
f1 <- list(
family = "Arial, sans-serif",
size = 18,
color = "grey"
)
f2 <- list(
family = "Old Standard TT, serif",
size = 14,
color = "#4477AA"
)
# bottom x-axis
ax <- list(
title = "Number of PBIs",
titlefont = f1,
anchor = "y",
side = "bottom",
showticklabels = TRUE,
tickangle = 0,
tickfont = f2
)
# top x-axis
ax2 <- list(
title = " ",
overlaying = "x",
anchor = "y",
side = "top",
titlefont = f1,
showticklabels = TRUE,
tickangle = 0,
tickfont = f2
)
# common y-axis
ay <- list(
title = "Process & Sub-Process Areas",
titlefont = f1,
showticklabels = TRUE,
tickangle = 0,
tickfont = f2
)
plot_ly(data = scrum %>%
group_by(Master.Project) %>%
summarise(Total_PBIs_Planned=sum(PBIs.Planned.in.Sprint, na.rm = TRUE),
Total_PBIs_Delivered = sum(Actual.PBI.Delivery,na.rm = TRUE)) %>% inner_join(scrum %>% count(Master.Project)), # creating the desired data frame
color = I("#149EF7")) %>%
# for bottom x-axis
add_segments(x = ~Total_PBIs_Planned, xend = ~Total_PBIs_Delivered,
y = ~Master.Project, yend = ~Master.Project, showlegend = FALSE) %>%
add_trace(x = ~Total_PBIs_Planned, y = ~Master.Project,
name = "Total_PBIs_Planned", type = 'scatter',mode = "markers",
marker = list(color = "#149EF7", size = 15,
line = list(color = '#FFFFFF', width = 1))) %>%
add_trace(x = ~Total_PBIs_Delivered, y = ~Master.Project,
name = "Total_PBIs_Delivered",type = 'scatter',mode = "markers",
marker = list(symbol ="circle-dot",color = "#F71430", size = 10,
line = list(color = '#FFFFFF', width = 1))) %>%
# for top x-axis
add_trace(x = ~n, y = ~Master.Project, xaxis = "x2",
name = "No._of_Sub_projects",type = 'bar',
marker = list(color = "#149EF7"),
opacity = 0.1,
hoverinfo = 'text',
text = ~paste(
Master.Project,
'<br> Total Sub Projects: ',n,
'<br> PBIs Planned: ',Total_PBIs_Planned,
'<br> PBIs Delivered: ',Total_PBIs_Delivered
)
) %>%
plotly::layout(
title = "Product Backlog Items - Planned Vs Delivered", titlefont = f1,
xaxis = ax,
yaxis = ay,
xaxis2 = ax2,
margin = list(l = 250)
)
You want to use rangemode = "tozero" in your axis layout.
ax <- list(
title = "Number of PBIs",
titlefont = f1,
anchor = "y",
side = "bottom",
showticklabels = TRUE,
tickangle = 0,
tickfont = f2,
rangemode = 'tozero')
Or you can specify your specific ranges using
range = c(0, 5)
See Plotly help here: https://plot.ly/r/axes/#rangemode

Multiple y-axes chart with Plotly in R

In Plotly for Python, we have this beautiful multiple y-axis example:
here is the link for the code.
I tried to do the same with Plotly in R using this code:
library(plotly)
x <- 1:4
y1 <- c(1,2,3,4)
y2 <- c(4,3,2,1)
y3 <- c(1,4,1,4)
y4 <- c(4,1,4,1)
test <- data.frame(x, y1, y2, y3, y4)
plot_ly(data = test, x = ~x, y = ~y1
,type = "scatter", mode = "lines", width = 800, color = I("red")
,name = "name01") %>%
add_trace(x = ~x, y = ~y2, yaxis = "y2", color = I("blue"), name = "name02") %>%
add_trace(x = ~x, y = ~y3, yaxis = "y3", color = I("purple"), name = "name03") %>%
add_trace(x = ~x, y = ~y4, yaxis = "y4", color = I("green"), name = "name04") %>%
layout(
yaxis = list(
showline = FALSE, side = "left"
,title = "Label 1"
,color = "red"
)
,yaxis2 = list(
showline = FALSE
,overlaying = "y"
,title = "Label 2", anchor = "free"
,color = "blue"
)
,yaxis3 = list(
showline = FALSE, side = "right", overlaying = "y"
,title = "Label 3"
,color = "purple"
)
,yaxis4 = list(
showline = FALSE, side = "right", overlaying = "y", position = 1
,title = "Label 4", anchor = "free"
,color = "green"
)
,xaxis = list(
showline = FALSE, zeroline = FALSE, dtick = 1, title = ""
)
,showlegend = FALSE
,margin = list(
pad = 30, b = 90, l = 150, r = 90
)
,legend = list(orientation = "h")
)
But I get these overlapping labels:
How can I fix it to get a non overlapping chart?
Try tuning the padding (pad) value for margin argument of layout().
Best I found for your other parameters is with pad = 49

Resources