I am having trouble formatting an axis using Plot.ly Double Axis graph. I am trying to get one of the y-axis to contain the '$' symbol in front of the number but I wasn't able to find any code for this. Hope someone can help me figure this out.
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right"
)
p <- plot_ly(data = df, x = days, y = sales, name = "Sales",type="bar") %>%
add_trace(x = days, y = Sales/Day, name = "Sales/Day", yaxis = "y2") %>%
layout(title = "Double Y Axis", yaxis2 = ay)
So in the above code I want to add a '$' symbol to both y axes (Sales and Sales/day).
Since you did not provide a reproducible example, I will use an example from the plotly website. You could try:
library(plotly)
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right"
)
plot_ly(x = 1:3, y = 10*(1:3), name = "slope of 10") %>%
add_trace(x = 2:4, y = 1:3, name = "slope of 1", yaxis = "y2") %>%
layout(title = "Double Y Axis", yaxis2 = ay, yaxis = list(showtickprefix = "all", tickprefix = "$"))
Related
Since ggplotly does not supportggplot2's sec.axis (Adding second Y axis on ggplotly), I want to add a second axis to the plotly-object instead. However, I do not wish to add any new trace.
Example:
library(plotly)
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
p <- plot_ly() %>%
add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>%
add_lines(x = ~2:4, y = ~1:3, name = "slope of 1", yaxis = "y2") %>%
layout(
title = "Double Y Axis", yaxis2 = ay,
xaxis = list(title="x")
)
p
How do I accomplish showing yaxis = "y2" without add_lines or adding any other trace?
One way to achieve this is to do what you have done and change the color of whatever you add for the second axis to "transparent", and turn off the hoverinfo and legend entry for the line :
library(plotly)
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
p <- plot_ly() %>%
add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>%
add_lines(x = ~2:4, y = ~1:3, color = I("transparent"), name = "", yaxis = "y2", hoverinfo='skip', showlegend=FALSE) %>%
layout(
title = "Double Y Axis", yaxis2 = ay,
xaxis = list(title="x")
)
p
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"))
I am trying to make a dual axis plot using plotly in R. However, I am unable to name the first y-axis using the following code whicI i found here: https://plot.ly/r/multiple-axes/
library(plotly)
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
p <- plot_ly() %>%
add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>%
add_lines(x = ~2:4, y = ~1:3, name = "slope of 1", yaxis = "y2") %>%
layout(
title = "Double Y Axis", yaxis2 = ay,
xaxis = list(title="x")
)
I would really appreciate it if someone could offer some advice.
Are you looking for this:
library(plotly)
library(dplyr)
ay2 <- list(
tickfont = list(color = "red"),
side = "left",
title = "first y axis"
)
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
plot_ly() %>%
add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10", yaxis = "y1") %>%
add_lines(x = ~2:4, y = ~1:3, name = "slope of 1", yaxis = "y2") %>%
layout(
title = "Double Y Axis", yaxis2 = ay, yaxis = ay2,
xaxis = list(title="x")
)
I have the following bar in plotly and I want to :
get the x-axis title away from the axis label so they dont overlap
make the Y-axis labels bigger
bring the bar values to the top of bars
My code is :
library(plotly)
plot_ly(x = c('100-200','200-400', '400-600','600-800','800- 1000'),
y = c(12261,29637,17469,11233,17043),
name = "dd",
type = "bar",
xaxis = list(title ="tr"),
yaxis = list(title = "cc") ,
text = c(12261,29637,17469,11233,17043),textposition = 'auto') %>%
layout(
xaxis = list(tickangle=15, title = " "),
yaxis = list(title = " "))
Thanks for your comments :)
Question 1: get the x-axis title away from the axis label so they dont overlap
This problem can be solved setting proper margins with margin = list(b=100, l=100) in layout.
Question 2: make the Y-axis labels bigger.
Use xaxis = list(titlefont=list(size=30)) in layout
Question 3: bring the bar values to the top of bars.
Use add_text with textposition = 'top'
library(plotly)
x <- c('100-200','200-400', '400-600','600-800','800-1000')
y <- c(12261,29637,17469,11233,17043)
labs <- c(12261,29637,17469,11233,17043)
plot_ly(x = x, y = y,
name = "dd",
type = "bar",
xaxis = list(title ="tr"),
yaxis = list(title = "cc")) %>%
add_text(x=x, y=y, text=labs, hoverinfo='none', textposition = 'top', showlegend = FALSE,
textfont=list(size=20, color="black")) %>%
layout(margin = list(b=100, l=100),
xaxis = list(tickangle=15, title = "Lenght of exon", titlefont=list(size=30)),
yaxis = list(title = "Number of genes", titlefont=list(size=30)))
Note that your graph is not exactly reproducible with the given code at the moment, so let me know if I have made unnecessary assumptions.
TLDR: Look at the bottom if you prefer all the changes described in the question.
I would recommend not using tickangle as it screws things up a bit. Try the following. Note the use of insidetextfont and tickfont for the yaxis.
library(plotly)
x = c('100-200','200-400', '400-600','600-800','800- 1000')
y = c(12261,29637,17469,11233,17043)
plot_ly(
x = x, y = y, type = "bar", text = y, textposition = 'auto',
insidetextfont = list(color = 'white')
) %>% layout(
xaxis = list(title = "Length of exon"),
yaxis = list(title = "Number of genes", tickfont = list(size = 15))
)
If you want to make the labels lie outside the bars, then use textposition = 'outside' instead of textposition = 'auto', and get rid of the insidetextfont for the default black colour. That may end up messing with the range of the y axis and you would need to manually define the range which may be cumbersome.
plot_ly(
x = x, y = y, type = "bar", text = y, textposition = 'outside'
) %>% layout(
xaxis = list(title = "Length of exon"),
yaxis = list(
title = "Number of genes", tickfont = list(size = 15),
range = c(0, max(y) + 2000)
)
)
This gives us .
I do not recommend tickangle, but if you must, use it with margin in layout.
plot_ly(
x = x, y = y, type = "bar", text = y, textposition = 'outside'
) %>% layout(
xaxis = list(title = "Length of exon", tickangle = 15),
yaxis = list(
title = "Number of genes", tickfont = list(size = 15),
range = c(0, max(y) + 2000)
), margin = list(b=100)
)
Hope this helps.
To label the line by the value of y axis. I have plotted two graph in one ,in the first i am getting my result as of my need but in second it is showing values at lower axis
how to display all value of x axis on the x plane .In my case i want to display all values from 1:18 in the x plane
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "Modal price"
)
p <- plot_ly(M, x = ~Week, y = ~`Arrival(Tonnes)`,
type= "bar",mode= 'marker',
marker = list(size = 10),name ="Weekly Arrival") %>%
add_annotations(x = seq(1:18),
y = M$`Arrival(Tonnes)`,
text = M$`Arrival(Tonnes)`,
xref = "x",
yref = "y",
showarrow = TRUE,
arrowsize = .01,
ax = 0,
ay = -20) %>%
#2nd graph addition
add_lines(x = ~Week, y = ~`Modal price`, name = "Weekly Modal prices",
marker = list(size = 10), yaxis = "y2",mode="marker") %>%
layout(
title = "Model price vs Arrivals in Weeks", yaxis2 = ay,
xaxis = list(title="Weeks") ) %>%
##Problem here the values are not attaching with the graph continuity
add_annotations(x=M$Week,y=M$`Modal price`,text=M$`Modal
price`,xref="x",yref="y",
showarrow=TRUE,arrowsize=0.1,ax=20,ay=-20)