How to include hover in a plotly layout element - r

I am stuck with the following plotly example which I would like to exploit:
library(plotly)
p <- plot_ly(economics, x = ~date, y = ~uempmed, name = "unemployment")
# add shapes to the layout
p <- layout(p, title = 'Highlighting with Rectangles',
shapes = list(
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
x0 = "1980-01-01", x1 = "1985-01-01", xref = "x",
y0 = 4, y1 = 12.5, yref = "y"),
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.2,
x0 = "2000-01-01", x1 = "2005-01-01", xref = "x",
y0 = 4, y1 = 12.5, yref = "y")))
How can I include hover information for those rectangles? Neither of the following works:
layout(p, title = 'Highlighting with Rectangles',
shapes = list(
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
x0 = "1980-01-01", x1 = "1985-01-01", xref = "x",
# text - hoverinfo pair
text = "hello", hoverinfo = "text",
y0 = 4, y1 = 12.5, yref = "y"),
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.2,
x0 = "2000-01-01", x1 = "2005-01-01", xref = "x",
# hovertext
hovertext = "good bye",
y0 = 4, y1 = 12.5, yref = "y")))
Is there any way how I can plot rectangles in plotly and apply a hover to them. Unfortunately, at least as I am aware of, there is no type = "rect" or anything like that available in R.

You can do it by adding polygons instead of shapes. Look below;
library(plotly)
plot_ly() %>%
add_polygons(x=c(as.Date("1980-01-01"), as.Date("1980-01-01"),
as.Date("1985-01-01"), as.Date("1985-01-01")),
y=c(4, 12.5, 12.5, 4),
line=list(width=0),
fillcolor='rgba(255, 212, 96, 0.5)', inherit = FALSE,
name = 'Hello') %>%
add_polygons(x=c(as.Date("2000-01-01"), as.Date("2000-01-01"),
as.Date("2005-01-01"), as.Date("2005-01-01")),
y=c(4, 12.5, 12.5, 4),
line=list(width=0),
fillcolor='rgba(255, 212, 96, 0.5)', inherit = FALSE,
name = 'Bye') %>%
add_markers(data = economics, x = ~date, y = ~uempmed, name = "unemployment")

Related

Plotly: How to set style and color for the arrows using add_annotations?

I want to represent 2D vectors rooted at the origin with plotly in R. Moreover, I want to color the vector based on a categorical variable. The problem is that I can either create the lines color-coded but without the arrow head:
library(plotly)
library(dplyr)
v <- c(1, 1)
b1 <- c(1, 0)
b2 <- c(0, 1)
df <- data.frame(
x = c(v[1], b1[1], b2[1]),
y = c(v[2], b1[2], b2[2]),
is_basis = c(FALSE, TRUE, TRUE)
)
df %>%
plot_ly(x = ~x, y = ~y, color = ~is_basis) %>%
add_segments(xend = ~x, yend = ~y, x = 0, y = 0, colors = c("red","black"))
Or with the arrow-head but not color-coded:
df %>%
plot_ly(x = ~x, y = ~y, color = ~is_basis) %>%
add_annotations(x = ~x, y = ~y, showarrow = TRUE, text = "", ax = 0, ay = 0,
axref = "x", ayref = "y", xref = "x", yref = "y")
So, my question is: Can I add an arrow head with add_segments? Or, can I style and color the arrows generated with add_annotations?
You can chose arrowhead and set the color of the arrow through the arguments arrowhead and arrowcolor in the function add_annotations(). If you set axref = "x" and ayref = "y" you also make sure that the lines start at origo if you set ax and ay to 0.
Here's an example using a subset of the built-in dataset mtcars:
Complete code:
library(plotly)
library(dplyr)
data <- mtcars[which(mtcars$am == 1 & mtcars$gear == 4),]
#data <- data %>% filter(row.names(data) %in% c("Honda Civic", "Fiat X1-9", "Datsun 710"))
#row.names(data) <- c("Honda Civic", "Fiat X1-9", "Datsun 710")
p <- plot_ly(data, x = ~wt, y = ~mpg, type = 'scatter', mode = 'markers',
marker = list(size = 2)) #%>%
p <- p %>%
add_annotations(x = data["Honda Civic","wt"],
y = data["Honda Civic","mpg"],
text = "",
xref = "x",
yref = "y",
showarrow = TRUE,
arrowcolor='blue',
arrowhead = 1,
arrowsize = 2,
ax = 0,
ay = 0,
axref="x",
ayref='y',
font = list(color = '#264E86',
family = 'sans serif',
size = 14))
p <- p %>%
add_annotations(x = data["Fiat X1-9","wt"],
y = data["Fiat X1-9","mpg"],
text = "",
xref = "x",
yref = "y",
showarrow = TRUE,
arrowcolor='green',
arrowhead = 2,
arrowsize = 2,
ax = 0,
ay = 0,
axref="x",
ayref='y',
font = list(color = '#264E86',
family = 'sans serif',
size = 14))
p <- p %>%
add_annotations(x = data["Datsun 710","wt"],
y = data["Datsun 710","mpg"],
text = "",
xref = "x",
yref = "y",
showarrow = TRUE,
arrowcolor='red',
arrowhead = 3,
arrowsize = 2,
ax = 0,
ay = 0,
axref="x",
ayref='y',
font = list(color = '#264E86',
family = 'sans serif',
size = 14))
p <- p %>%
add_annotations(x = data["Fiat 128","wt"],
y = data["Fiat 128","mpg"],
text = "",
xref = "x",
yref = "y",
showarrow = TRUE,
arrowcolor='black',
arrowhead = 4,
arrowsize = 2,
ax = 0,
ay = 0,
axref="x",
ayref='y',
font = list(color = '#264E86',
family = 'sans serif',
size = 14))
p <- p %>%
add_annotations(x = data["Mazda RX4 Wag","wt"],
y = data["Mazda RX4 Wag","mpg"],
text = "",
xref = "x",
yref = "y",
showarrow = TRUE,
arrowcolor='purple',
arrowhead = 5,
arrowsize = 2,
ax = 0,
ay = 0,
axref="x",
ayref='y',
font = list(color = '#264E86',
family = 'sans serif',
size = 14))
p <- p %>%
add_annotations(x = data["Volvo 142E","wt"],
y = data["Volvo 142E","mpg"],
text = "",
xref = "x",
yref = "y",
showarrow = TRUE,
arrowcolor='grey',
arrowhead = 6,
arrowsize = 2,
ax = 0,
ay = 0,
axref="x",
ayref='y',
font = list(color = '#264E86',
family = 'sans serif',
size = 14))
p <- p %>%
add_annotations(x = data["Toyota Corolla","wt"],
y = data["Toyota Corolla","mpg"],
text = "",
xref = "x",
yref = "y",
showarrow = TRUE,
arrowcolor='aquamarine',
arrowhead = 7,
arrowsize = 2,
ax = 0,
ay = 0,
axref="x",
ayref='y',
font = list(color = '#264E86',
family = 'sans serif',
size = 14))
p

How to change x-axis layout using plotly in r

I'm trying to customise the x-axis. I currently have the "Site" variable on the x-axis. The range is from 15 to 24 with site a and b for each number i.e. for 15, there is 15a and b (and so on to 24). When I label all bars, it looks messy and I'm trying to customise it so for example, the number 15 is below but the individual bars are labelled a and b. This way, the individual bars will be identifiable but it won't look so crowded.
Here is my code so far:
#Stacked bar chart exp2
BarChartData2 <- read.csv("Bar chart data exp 2.csv")
Site <- BarChartData2[,4]
Card <- BarChartData2[,2]
Pen <- BarChartData2[,3]
data2 <- data.frame(Site, Card, Pen)
pstacked2 <- plot_ly(data2, x = ~Site, y = ~Card, type = 'bar', name = 'Card', marker = list(color = 'Black')) %>%
add_trace(y = ~Pen, name = 'Pen', marker = list(color = 'red')) %>%
layout(yaxis = list(title = 'Number of Bee Visits'), barmode = 'stack', font = list(family = 'Times New Roman', size =14, color ="black"), xaxis = list(autotick = F, dtick = 2))
pstacked2
Any help/other ideas of how to do this will be much appreciated!
Here is something to help you. You can play with shapes and annotations to get exatly what you want.
plot_ly(data2, x = ~Site, y = ~Card, type = 'bar', name = 'Card', marker = list(color = 'Black')) %>%
add_trace(y = ~Pen, name = 'Pen', marker = list(color = 'red')) %>%
layout(yaxis = list(title = 'Number of Bee Visits'),
barmode = 'stack',
font = list(family = 'Times New Roman', size =14, color ="black"),
xaxis = list(autotick = F, dtick = 2),
margin = list(
r = 10,
t = 25,
b = 100,
l = 110
),
shapes = list(
list(
line = list(
color = "rgba(68, 68, 68, 0.5)",
width = 1
),
type = "line",
x0 = 0,
x1 = 1,
xref = "paper",
y0 = -0.05,
y1 = -0.05,
yref = "paper"
)
),
annotations = list(
list(
x = 0.04,
y = -0.1,
showarrow = FALSE,
text = "15",
xref = "paper",
yref = "paper"
),
list(
x = 0.14,
y = -0.1,
showarrow = FALSE,
text = "16",
xref = "paper",
yref = "paper"
),
list(
x = 0.24,
y = -0.1,
showarrow = FALSE,
text = "17",
xref = "paper",
yref = "paper"
),
list(
x = .35,
y = -0.1,
showarrow = FALSE,
text = "18",
xref = "paper",
yref = "paper"
),
list(
x = .45,
y = -0.1,
showarrow = FALSE,
text = "19",
xref = "paper",
yref = "paper"
),
list(
x = .55,
y = -0.1,
showarrow = FALSE,
text = "20",
xref = "paper",
yref = "paper"
),
list(
x = .65,
y = -0.1,
showarrow = FALSE,
text = "21",
xref = "paper",
yref = "paper"
),
list(
x = .75,
y = -0.1,
showarrow = FALSE,
text = "22",
xref = "paper",
yref = "paper"
),
list(
x = .85,
y = -0.1,
showarrow = FALSE,
text = "23",
xref = "paper",
yref = "paper"
),
list(
x = .95,
y = -0.1,
showarrow = FALSE,
text = "24",
xref = "paper",
yref = "paper"
)
)
)

R Plotly-plot layout (margind and x axis on the top)

I encountered a problem while using plotly and R shiny to visualize data. My sample codes are:
y <- c("q1", "q2", "q3", "q4", "q5", "q6", "q7")
x1 <- c(20, 10, 15, 15, 20, 10, 15)
x2 <- c(10, 20, 20, 10, 10, 30, 10)
x3 <- c(10, 10, 5, 10, 10, 5, 5)
x4 <- c(20, 25, 25, 35, 55, 40, 35)
x5 <- c(40, 35, 35, 30, 5, 15, 35)
num <- c(1,3,5,6,7,2,4)
df <- data.frame(y, x1, x2, x3, x4, x5, num)
plot_ly(df[order(-xtfrm(df$num)),]) %>%
add_trace(x = ~x1, y = ~y, marker = list(color = 'rgb(202,0,32)'), type = 'bar', orientation = 'h') %>%
add_trace(x = ~x2, y = ~y, marker = list(color = 'rgb(244,165,130)'), type = 'bar', orientation = 'h') %>%
add_trace(x = ~x3, y = ~y, marker = list(color = 'rgb(223,223,223)'), type = 'bar', orientation = 'h') %>%
add_trace(x = ~x4, y = ~y, marker = list(color = 'rgb(146,197,222)'), type = 'bar', orientation = 'h') %>%
add_trace(x = ~x5, y = ~y, marker = list(color = 'rgb(5,113,176)'), type = 'bar', orientation = 'h') %>%
layout(title="mytitle",
xaxis = list(title = "",
showticklabels = TRUE,
zeroline = FALSE,
domain = c(0.15, 1)),
yaxis = list(title = "",
showticklabels = FALSE,
zeroline = FALSE,
categoryorder = 'array',
categoryarray = ~qt),
barmode = 'relative',
paper_bgcolor = 'rgb(248, 248, 255)', plot_bgcolor = 'rgb(248, 248, 255)',
autosize=T,
margin = list(l = 150, r = 10, t = 100, b = 50, pad=4),
showlegend=F) %>%
# labeling the y-axis
add_annotations(xref = 'paper', yref = 'y', x = 0.14, y = df$y,
xanchor = 'right',
text = df$y,
font = list(family = 'Arial', size = 5,
color = 'rgb(67, 67, 67)'),
showarrow = FALSE, align = 'right')
The problem is that the margins does not work very well with large amount of questions. In my data, one of the categories includes more than 40 questions. The plot with 5 questions looks fine. (length=500)
But the plot with over 40 questions have big top and bottom margins. (length=4000)
Please help me get rid of these weird margins.
Also, I'd like to know how to put the x-axis on both the top and bottom of the plot. Thank you!

Line appearing below the chart in plotly (Using Layout - Shapes)

I'm trying to make a horizontal donut chart with a line indicating my value.
Something like this:
But in my code the line is below the chart. Like this:
Heres the code:
a <- list(
showticklabels = F,
autotick = F,
showgrid = F,
zeroline = F)
b <- list(
xref = 'paper',
yref = 'paper',
x = 0.5,
y = 0.5,
showarrow = FALSE,
text = '')
base_plot <- plot_ly(
type = "pie",
values = c(50, 7.14, 7.14, 7.14, 7.14, 7.14, 7.14, 7.14),
labels = c("-", "0", "20", "40", "60", "80", "100", "150"),
rotation = 90,
direction = "clockwise",
hole = 0.4,
textinfo = "none",
textposition = "outside",
hoverinfo = "none",
domain = list(x = c(0, 1), y = c(0, 1)),
marker = list(colors = c('#FFFFFF', '#440832', '#80180e', '#a52223', '#c0291b', '#f5c142', '#6aca3c', '#3980de')),
showlegend = F
) %>%
layout(
shapes = list(
list(
type = 'lines',
x0 = 0.5,
x1 = 0.5,
y0 = 0.5,
y1 = 1,
xref = 'paper',
yref = 'paper',
fillcolor = '#000000',
layer = "above"
)
),
xaxis = a,
yaxis = a,
annotations = b
)
Looking at the Plotly documentation the parameter [layer = "above"] should resolve my problem, but it isn't working. How do I make the line appear above the chart?
As Marco Sandri told
Your code works correctly in my R 3.5.0. with plotly_4.7.1.9000. I get the line above the chart, as expected. Try to install the development version of plotly: devtools::install_github("ropensci/plotly").

plotly-Sort yaxis alphabetically

I'm looking for a way to sort my plotly bar plot by yaxis alphabetically. I tried several ways but the order of y is still from z to a, instead of a to z. Please help me out!
qt <- c("A", "C", "B","B","A", "C", "C")
y <- c("q1", "q2", "q3", "q4", "q5", "q6", "q7")
x1 <- c(20, 10, 15, 15, 20, 10, 15)
x2 <- c(10, 20, 20, 10, 10, 30, 10)
x3 <- c(10, 10, 5, 10, 10, 5, 5)
x4 <- c(20, 25, 25, 35, 55, 40, 35)
x5 <- c(40, 35, 35, 30, 5, 15, 35)
df <- data.frame(qt, y, x1, x2, x3, x4, x5)
df$qt <- factor(df$qt, levels = c("A", "B", "C"))
plot_ly(df) %>%
add_trace(x = ~x1, y = ~y, marker = list(color = 'rgb(202,0,32)'), type = 'bar', orientation = 'h') %>%
add_trace(x = ~x2, y = ~y, marker = list(color = 'rgb(244,165,130)'), type = 'bar', orientation = 'h') %>%
add_trace(x = ~x3, y = ~y, marker = list(color = 'rgb(223,223,223)'), type = 'bar', orientation = 'h') %>%
add_trace(x = ~x4, y = ~y, marker = list(color = 'rgb(146,197,222)'), type = 'bar', orientation = 'h') %>%
add_trace(x = ~x5, y = ~y, marker = list(color = 'rgb(5,113,176)'), type = 'bar', orientation = 'h') %>%
layout(title="mytitle",
xaxis = list(title = "",
showticklabels = TRUE,
zeroline = FALSE,
domain = c(0.15, 1)),
yaxis = list(title = "",
showticklabels = FALSE,
zeroline = FALSE),
barmode = 'relative',
paper_bgcolor = 'rgb(248, 248, 255)', plot_bgcolor = 'rgb(248, 248, 255)',
autosize=T,
margin = list(l = 150, r = 50, t = 100, b = 50),
showlegend=F) %>%
# labeling the y-axis
add_annotations(xref = 'paper', yref = 'y', x = 0.14, y = df$y,
xanchor = 'right',
text = df$y,
font = list(family = 'Arial', size = 15,
color = 'rgb(67, 67, 67)'),
showarrow = FALSE, align = 'right')%>%
#labeling the y-axis (category)
add_annotations(xref = 'paper', yref = 'qt', x = 0.01, y = df$y,
xanchor = 'right',
text = df$qt,
font = list(family = 'Arial', size = 15,
color = 'rgb(67, 67, 67)'),
showarrow = FALSE, align = 'right')
The primary goal I would like to accomplish is to order this by the variable qt (from A to C). But if this is impossible, ordering the plot by y is also desirable (from q1 to q7). My plot looks like this:
Thank you in advance!

Resources