Related
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:
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
Is there a way to change the order of subplots in plotly for R? Is there a way to manually change the levels of a factor in this code?
I want a plot with Weight in the first plot followed by a,b,c in order above it. But what I get as output is Weight, c, a and b as shown in the image graph
Here is my code
df<-data.frame("time"= seq(0.01,10,length.out=100),"Weight"=1:100, "a"=rnorm(100),"b"=rnorm(100),"c"=rnorm(100))
q <- df%>%
tidyr::gather(variable, value, -time) %>%
transform(id = as.integer(factor(variable))) %>%
plot_ly(x = ~time, y = ~value, color = ~variable, colors = "Dark2",
yaxis = ~paste0("y", id)
) %>%
layout(
xaxis = list(title = "Time,s",tickfont = list(size = 17),titlefont = list(size = 20)),
yaxis = list(tickfont = list(size = 17), title="DP"),
hoverlabel = list(font=list(size=20))
) %>%
add_lines() %>%
subplot(nrows = length(df)-1, shareX = TRUE)
One way to do this is re-ordering factor levels as below:
# set.seed to keep the exact same results
set.seed(123)
df<-data.frame("time"= seq(0.01,10,length.out=100),"Weight"=1:100, "a"=rnorm(100),"b"=rnorm(100),"c"=rnorm(100))
DF <- df%>%
tidyr::gather(variable, value, -time) %>%
transform(id = as.integer(factor(variable)))
DF$variable <- factor(DF$variable, levels = c("Weight", "a", "b", "c")) #re-order
q <- DF %>%
plot_ly(x = ~time, y = ~value, color = ~variable, colors = "Dark2",
yaxis = ~paste0("y", sort(id, decreasing =F))) %>% #sort the order
layout(
xaxis = list(title = "Time,s",tickfont = list(size = 17),titlefont = list(size = 20)),
yaxis = list(tickfont = list(size = 17), title="DP"),
hoverlabel = list(font=list(size=20))
) %>%
add_lines() %>%
subplot(nrows = length(df)-1, shareX = TRUE)
q
You will need sort(id, decreasing =F) to get exact same order of what you set in factor(DF$variable, levels = c("Weight", "a", "b", "c")).
Exchange the data, the name, and the line features of the top and bottom subplots as following code,
#assign q$x$data to one template variable p
p = q$x$data
#exchange the data, name, and line features of q$x$data[[1]] and q$x$data[[4]]
q$x$data[[1]]$x = p[[4]]$x
q$x$data[[1]]$y = p[[4]]$y
q$x$data[[1]]$name = p[[4]]$name
q$x$data[[1]]$line = p[[4]]$line
q$x$data[[4]]$x = p[[1]]$x
q$x$data[[4]]$y = p[[1]]$y
q$x$data[[4]]$name = p[[1]]$name
q$x$data[[4]]$line = p[[1]]$line
#show
q
The problem might have been caused by yaxis = ~paste0("y", id). I replaced it with yaxis = ~paste0(id, "y") to get the correct order. You may need to change some code to get the right format.
library(plotly)
df<-data.frame("time"= seq(0.01,10,length.out=100),"Weight"=1:100, "a"=rnorm(100),"b"=rnorm(100),"c"=rnorm(100))
q <- df%>%
tidyr::gather(variable, value, -time) %>%
transform(id = as.integer(factor(variable))) %>%
plot_ly(x = ~time, y = ~value, color = ~variable, colors = "Dark2",
yaxis = ~paste0(id, "y")
) %>%
layout(
xaxis = list(title = "Time,s",tickfont = list(size = 17),titlefont = list(size = 20)),
yaxis = list(tickfont = list(size = 17), title="DP"),
hoverlabel = list(font=list(size=20))
) %>%
add_lines() %>%
subplot(nrows = length(df), shareX = TRUE)
q
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.
I am trying to create a plot in R similar to this:
with up to 6 variables and it has to be reactive.
I tried plotly, however in plotly I would get axis ticks on the plot, and it gets messy, and I managed to get only one y axis out.
Is there a way to recreate the plot in plotly or any other interactive library?
My plotly code:
library(dplyr)
library(plotly)
library(tidyr)
data <- cbind(
seq(from = 1, to = 30, by = 1),
sample(seq(from = 100, to = 300, by = 10), size = 30, replace = TRUE),
sample(seq(from = 1, to = 100, by = 9), size = 30, replace = TRUE),
sample(seq(from = 50, to = 60, by = 2), size = 30, replace = TRUE),
sample(seq(from = 100, to = 130, by = 1), size = 30, replace = TRUE)
) %>%
as.data.frame()
names(data) <- c("date", "a", "b", "x", "y")
plot_ly(x = ~data$date) %>%
add_lines(y = ~data[, 2], name = "a", line = list(color = "red")) %>%
add_lines(y = ~data[, 3], name = "b", line = list(color = "blue"), yaxis = "y2") %>%
add_lines(y = ~data[, 4], name = "x", line = list(color = "green"), yaxis = "y3") %>%
add_lines(y = ~data[, 5], name = "y", line = list(color = "pink"), yaxis = "y4") %>%
layout(
yaxis = list(
side = "left",
title = list("")
),
yaxis2 = list(
side = "left",
overlaying = "y",
anchor = "free"
),
yaxis3 = list(
side = "left",
overlaying = "y",
anchor = "free",
position = 0.04
),
yaxis4 = list(
side = "left",
overlaying = "y",
anchor = "free",
position = 0.08
),
margin = list(pad = 30)
)
With the highcharter package one can generate nice time series plots with multiple y-axes:
library(highcharter)
set.seed(1)
n <- 100
x1 <- cumsum(rnorm(n))
x2 <- cumsum(runif(n)-0.5)+10
x3 <- cumsum(rnorm(n,0,20))+100
x4 <- cumsum(rnorm(n,0,20))+1000
highchart() %>%
hc_add_series(data = x1) %>%
hc_add_series(data = x2, yAxis = 1) %>%
hc_add_series(data = x3, yAxis = 2) %>%
hc_add_series(data = x4, yAxis = 3) %>%
hc_yAxis_multiples(
list(lineWidth = 3, lineColor='#7cb5ec', title=list(text="First y-axis")),
list(lineWidth = 3, lineColor="#434348", title=list(text="Second y-axis")),
list(lineWidth = 3, lineColor="#90ed7d", title=list(text="Third y-axis")),
list(lineWidth = 3, lineColor="#f7a35c", title=list(text="Fourth y-axis"))
)
I just tweaked your code. Hope this helps!
plot_ly(x = ~data$date) %>%
add_lines(y = ~data[, 2], name = "a", line = list(color = "red")) %>%
add_lines(y = ~data[, 3], name = "b", line = list(color = "blue"), yaxis = "y2") %>%
add_lines(y = ~data[, 4], name = "x", line = list(color = "green"), yaxis = "y3") %>%
add_lines(y = ~data[, 5], name = "y", line = list(color = "pink"), yaxis = "y4") %>%
layout(
yaxis = list(
side = "left",
color="red",
title = ""
),
yaxis2 = list(
overlaying = "y",
anchor = "free",
color="blue",
title= ""
),
yaxis3 = list(
side = "right",
overlaying = "y",
color="green",
title= ""
),
yaxis4 = list(
side = "right",
overlaying = "y",
anchor = "free",
position = 1,
color="pink",
title= ""
),
xaxis = list(
title = ""
),
margin = list(pad = 25)
)
To add to and modify #Prem's answer, you can avoid overlap of the y-axes with the graph itself by tweaking the x-axis portion of the plot with the domain parameter of the x-axis.
plot_ly(data, x = ~time, type = 'scatter', mode = 'lines') %>%
add_lines(y = ~y1, name='y1', line = list(color = "red")) %>%
add_lines(y = ~y2, name='y2', yaxis='y2', line = list(color = "orange")) %>%
add_lines(y = ~y3, name='y3', yaxis='y3', line = list(color = "darkgreen")) %>%
add_lines(y = ~y4, name='y4', yaxis='y4', line = list(color = "purple")) %>%
add_lines(y = ~y5, name='y5', yaxis='y5', line = list(color = "brown")) %>%
layout(
xaxis = list(title = "time", domain = c(0.5,1)),
yaxis = list(title = 'Y-axis 1', side = "left", color = "red", position = 0,
anchor = 'free'),
yaxis2 = list(title = 'Y-axis 2', side = "left", color = "orange",
overlaying = "y", anchor = 'free', position = 0.1),
yaxis3 = list(title = 'Y-axis 3', side = "left",
overlaying = "y", anchor = 'free', color = "darkgreen", position = 0.2),
yaxis4 = list(title = 'Y-axis 4', side = "left",
overlaying = "y", anchor = 'free', color = "purple", position = 0.3),
yaxis5 = list(title = 'Y-axis 5',side = "left",
overlaying = "y", anchor = 'free', color = "brown", position = 0.4),
showlegend = T
)
The position of the single y-axes with respect to the plot size in percent can be set with the position parameter.
In the above example, each y-axis gets 10% of the total plot size while the graph is reserved for the right half.