R: subplot with plots that have multiple y-axis and x-axis - r

I am trying to draw a subplot that contains 2 plots. Each inner plots have two y-axis and two x-axis. I also want to match each y label with his lines color.
First I want to start with a simple example. Consider following code
library(plotly)
library(dplyr)
p1 <- economics %>% plot_ly()%>%
add_trace( x = ~date, y = ~unemploy,yaxis='y1',type='scatter',mode="lines+markers")%>%
add_trace(x = ~date, y = ~uempmed,yaxis='y2',type='scatter',mode="lines+markers") %>%
layout( yaxis2 = list(overlaying = "y1",side = "right"))
p2<-p1
>p1
The output:
subplot(p1, p2 , nrows = 2 , shareX = TRUE)
The output:
So the subplot does not work fine. How to fix it?
EDIT:
With a little change to the code as shown below and it work!
R code
library(plotly)
library(dplyr)
p1 <- economics %>% plot_ly()%>%
add_trace( x = ~date, y =~unemploy,yaxis='y',type='scatter',mode="lines+markers")%>%
add_trace(x = ~date, y = ~uempmed,yaxis='y2',type='scatter',mode="lines+markers") %>%
layout( yaxis = list(side = "left"),yaxis2 = list(overlaying = "y",side = "right"))
p2<-economics %>% plot_ly()%>%
add_trace( x = ~date, y = ~unemploy,yaxis='y',type='scatter',mode="lines+markers")%>%
add_trace(x = ~date, y = ~uempmed,yaxis='y2',type='scatter',mode="lines+markers") %>%
layout(yaxis = list(side = "left"), yaxis2 = list(overlaying = "y3",side = "right"))
subplot(p1,p2,nrows=2)
The output now is as follows:
To extend for have 3 plots:
library(plotly)
library(dplyr)
p1 <- economics %>% plot_ly()%>%
add_trace( x = ~date, y = ~unemploy,yaxis='y',type='scatter',mode="lines+markers")%>%
add_trace(x = ~date, y = ~uempmed,yaxis='y2',type='scatter',mode="lines+markers") %>%
layout( yaxis = list(side = "left"),yaxis2 = list(overlaying = "y",side = "right"))
p2<-economics %>% plot_ly()%>%
add_trace( x = ~date, y = ~unemploy,yaxis='y',type='scatter',mode="lines+markers")%>%
add_trace(x = ~date, y = ~uempmed,yaxis='y2',type='scatter',mode="lines+markers") %>%
layout(yaxis = list(side = "left"), yaxis2 = list(overlaying = "y3",side = "right"))
p3<-economics %>% plot_ly()%>%
add_trace( x = ~date, y = ~unemploy,yaxis='y',type='scatter',mode="lines+markers")%>%
add_trace(x = ~date, y = ~uempmed,yaxis='y2',type='scatter',mode="lines+markers") %>%
layout(yaxis = list(side = "left"), yaxis2 = list(overlaying = "y5",side = "right"))
subplot(p1,p2,p3,nrows=3)
The output:

Related

Left-align a multi-line label using plotly

In the chart below, is there anyway to left-align all the text so that, for example, Total is located directly above Pct. Total, as shown below:
library(dplyr)
library(plotly)
library(scales)
dat <- data.frame(grp = c("A", "B", "C"),
val = c(100, 50, 50)) %>%
mutate(label = paste0("Total: ", val, "\nPct. Total: ", percent(val/sum(val))))
dat %>%
plot_ly(x = ~val,
y = ~grp,
type = "bar",
text = ~label,
textposition = "outside") %>%
layout(xaxis = list(range = c(0, 125)))
Edit: Solved
dat %>%
plot_ly(x = ~val,
y = ~grp,
type = "bar") %>%
layout(annotations = list(text = ~label,
y = ~grp,
x = ~val,
showarrow = F,
xanchor = "left",
align = "left"),
xaxis = list(range = c(0, 125)))
Solution:
dat %>%
plot_ly(x = ~val,
y = ~grp,
type = "bar") %>%
layout(annotations = list(text = ~label,
y = ~grp,
x = ~val,
showarrow = F,
xanchor = "left",
align = "left"),
xaxis = list(range = c(0, 125)))

show right y axis in plotly chart with only 1 trace

In R plotly, I want to show a single line chart that has the y axis labels on BOTH the left and the right sides. I understand how to do this with 2 or more traces, but I can't find anywhere that shows how to do it with only 1 trace on the chart. Here's a basic example - it only shows the y axis on the left but I want it to appear on both sides:
library(plotly)
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
fig <- plot_ly()
fig <- fig %>% add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10")
fig <- fig %>% layout(
title = "Double Y Axis", yaxis2 = ay,
xaxis = list(title="x")
)
fig
You can add the same values in a new axis like this:
library(plotly)
#Setup
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
#Plot
fig <- plot_ly()
fig <- fig %>% add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10", yaxis = "y2")
fig <- fig %>%
add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>%
layout(
title = "Double Y Axis", yaxis2 = ay,
xaxis = list(title="x")
)
Output:
If you want to remove the legend:
#Plot 2
fig <- plot_ly()
fig <- fig %>% add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10", yaxis = "y2")
fig <- fig %>%
add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>%
layout(
title = "Double Y Axis", yaxis2 = ay,
xaxis = list(title="x"),showlegend = FALSE
)
Output:

Plotly: How to set a minimum value on secondary y-axis?

I want to set a minimum value on the secondary y-axis. This is my code :
library(plotly)
# my data
value <- c(300000,400000,500000,600000,500000,600000)
x1 <- c(3,4,5,5,4,3)
x2 <-c(3,4,5,5,4,3)
name <- c("martin","john","marc","igor","thea","julia")
df <- data.frame(value, x1, x2, name)
# graph with plotly
graph=df %>%
plot_ly(x = ~name) %>%
add_bars(y = ~x1,
name = "bar1") %>%
add_bars(y = ~x2,
name = "bar2") %>%
add_lines(y = ~value,
name = "line",
yaxis = "y2") %>%
layout(barmode = "bar",
yaxis2 = list(overlaying = "y",
side = "right"),
barmode = "bar",
legend = list(x = 1.1, y =1))
# showing graph
graph
and i get this :
but i want the secondary y-axis start at 200k (or 100k) instead of 300k.
How can we fix it ? Some help would be appreciated
Generally, if you've already got a fig set up:
fig <- fig %>% layout(yaxis2 = list(range = c(<min>, <max>)))
And in your specific case:
graph <- graph %>% layout(yaxis2 = list(range = c(200000,600000)))
Plot
Complete code:
library(plotly)
# my data
value <- c(300000,400000,500000,600000,500000,600000)
x1 <- c(3,4,5,5,4,3)
x2 <-c(3,4,5,5,4,3)
name <- c("martin","john","marc","igor","thea","julia")
df <- data.frame(value, x1, x2, name)
# graph with plotly
graph=df %>%
plot_ly(x = ~name) %>%
add_bars(y = ~x1,
name = "bar1") %>%
add_bars(y = ~x2,
name = "bar2") %>%
add_lines(y = ~value,
name = "line",
yaxis = "y2") %>%
layout(barmode = "bar",
yaxis2 = list(overlaying = "y",
side = "right"),
barmode = "bar",
legend = list(x = 1.1, y =1))
# showing graph
#graph
graph <- graph %>% layout(yaxis2 = list(
#scaleanchor = "x",
#scaleratio = 0.2,
range = c(200000,600000)
#title = "1:5"
))
graph

Sharing axes and legends between subplots in plotly in R (faceting in ggplot2 and using ggplotly doesn't work)

I have the following data:
df <- data.frame(numbers = rep(1:3, 30),
letter = sample(c("A", "B", "C", "D"), 90, replace = TRUE),
status = sample(c("good", "bad", "ugly"), 90, replace = TRUE))
I am trying to replicate this ggplot2 plot, but make it interactive:
ggplot(df, aes(letter, fill = status)) + geom_bar() + facet_wrap(.~numbers)
If I use ggplotly, then I can select and deselect variables, but the bars do not readjust so I get something that looks like this:
So my idea was to cast the data, then create individual plotly plots and use subplot:
df_group <- df %>% group_by(numbers, letter, status) %>% tally()
df_group_cast <- dcast(df_group, numbers + letter ~ status)
p1 <- df_group_cast %>%
filter(numbers == 1) %>%
plot_ly(x = ~letter, y = ~good, type = 'bar', name = 'good') %>%
add_trace(y = ~bad, name = 'bad') %>%
add_trace(y = ~ugly, name = 'ugly') %>%
layout(yaxis = list(title = 'Count'), barmode = 'stack')
p2 <- df_group_cast %>%
filter(numbers == 2) %>%
plot_ly(x = ~letter, y = ~good, type = 'bar', name = 'good') %>%
add_trace(y = ~bad, name = 'bad') %>%
add_trace(y = ~ugly, name = 'ugly') %>%
layout(yaxis = list(title = 'Count'), barmode = 'stack')
p3 <- df_group_cast %>%
filter(numbers == 3) %>%
plot_ly(x = ~letter, y = ~good, type = 'bar', name = 'good') %>%
add_trace(y = ~bad, name = 'bad') %>%
add_trace(y = ~ugly, name = 'ugly') %>%
layout(yaxis = list(title = 'Count'), barmode = 'stack')
subplot(p1, p2, p3)
This is interactive, but also looks bad. I would like them to share a scale, have one legend, and have titles for each number group.
Is this possible?
(I am trying to embed an interactive graph like this into slidify, if there are better libraries I am open to using them. So far rCharts has failed me, so I'm trying plotly)
I figured it out! Didn't need to cast my data in the end. I have even added a step for adding subgroup titles.
df_group <- df %>% group_by(numbers, letter, status) %>% tally()
Put together annotation texts to add to the plots:
a <- list(
text = sprintf("<b>1</b>"),
xref = "paper",
yref = "paper",
yanchor = "bottom",
xanchor = "center",
align = "center",
x = 0.5,
y = 1,
showarrow = FALSE)
b <- list(
text = sprintf("<b>2</b>"),
xref = "paper",
yref = "paper",
yanchor = "bottom",
xanchor = "center",
align = "center",
x = 0.5,
y = 1,
showarrow = FALSE)
c <- list(
text = sprintf("<b>3</b>"),
xref = "paper",
yref = "paper",
yanchor = "bottom",
xanchor = "center",
align = "center",
x = 0.5,
y = 1,
showarrow = FALSE)
Put together the actual plots, note the "annotations" option under layout. I also didn't need to add all that trace nonsense, coloring by status did the work for me.
p1 <- df_group %>%
filter(numbers == 1) %>%
group_by(letter) %>%
plot_ly(x = ~letter, y= ~n, color = ~status, type = 'bar', legendgroup = ~status) %>%
layout(barmode = 'stack', annotations = a)
p2 <- df_group %>%
filter(numbers == 2) %>%
group_by(letter) %>%
plot_ly(x = ~letter, y= ~n, color = ~status, type = 'bar', legendgroup = ~status, showlegend = FALSE) %>%
layout(barmode = 'stack', annotations = b)
p3 <- df_group %>%
filter(numbers == 3) %>%
group_by(letter) %>%
plot_ly(x = ~letter, y= ~n, color = ~status, type = 'bar', legendgroup = ~status, showlegend = FALSE) %>%
layout(barmode = 'stack', annotations = c)
Plotting:
subplot(p1, p2, p3, shareY = TRUE)
Imgur can't show interactivity, so you will just have to trust that this is interactive and you can select the categories in all plots by clicking on their labels.

R: plotly graph with dual Y axis?

I've found this question but answer is not up to date to produce the correct result.
Second Y-Axis in a R plotly graph
How can I plot a dual y axis plot?
df <- data.frame(MediaDate = as.Date(c("2016-04-01","2016-05-01","2016-06-01"), format = "%Y-%m-%d"),
Spend = c(39654, 34446, 27402),
Visits = c(19970, 14450, 12419))
plot_ly(df, x = ~MediaDate, y = ~Spend, type = "bar", name = "Spend") %>%
add_trace(x = ~MediaDate, y = ~Visits, mode = "lines", yaxis = "y2", name = "Visits") %>%
layout(yaxis2 = list(overlaying = "y", side = "right"))
Produces:
What I need (but instead of a bar and a line, 2 lines):
Here's a way to do this:
df <- data.frame(MediaDate = as.Date(c("2016-04-01","2016-05-01","2016-06-01"),
format = "%Y-%m-%d"),
Spend = c(39654, 34446, 27402),
Visits = c(19970, 14450, 12419))
old.y <- list(
side = "left",
title = "Spend"
)
new.y <- list(
overlaying = "y",
side = "right",
title = "Visits"
)
plot_ly(df) %>%
add_lines(x = ~MediaDate, y = ~Spend, yaxis="y1") %>%
add_lines(x = ~MediaDate, y = ~Visits, yaxis = "y2") %>%
layout(yaxis2 = new.y, yaxis = old.y, xaxis = list(title="MediaDate"))

Resources