I have spent way too long trawling through google/stack overflow trying to find an answer but no luck!
Essentially, I have created a stacked bar chart, but the text labels overlaid do not align with the correct bars. I have tried using text = ~value, the annotations parameter and many more... but no luck.
Here's my latest version of the code and a picture example. I'm assuming it must be a simple solution but I can't figure it out!
t <- list(
size = 12,
color = 'black')
fig <- df4 %>% plot_ly(x = ~variable, y = ~value, type = 'bar',
name = ~Country, color = ~Country) %>%
layout(yaxis = list(title = '% of Value'),
xaxis = list(title = 'Metric'),
barmode = 'stack',
title = "Proportion of value across countries") %>%
add_text(text=~value, textposition="middle center",
showlegend = F, textfont = t)
fig
Managed to solve it:
fig <- df4 %>% plot_ly(x = ~variable, y = ~value, type = 'bar',
text = paste(df4$value,"%"),
textposition = "outside",
color = ~Country,
textfont = t)%>%
layout(yaxis = list(title = '% of Value'),
xaxis = list(title = 'Country'),
title = "Proportion of value across countries",
barmode = 'stack')
fig
Related
I was wanting to know if there is a way of not showing the add_trace values on the plotly graph output? The code below produces a series of cumulative annual line graphs. The add_trace addition was inserted to add a dashed vertical line to the graph. However, when I hover over the graphic it shows the values for the vertical line - can this be turned off, but allow hover values for the cumulative annual lines? I was hoping to do this with a tweak of the code below.
Thanks.
df %>%
filter(op_area_ocode == 'ALL') %>% mutate(Year = as.character(R.Year), DoY = as.Date(DoY.N, origin = "1972-01-01") - 1) %>%
plot_ly(., x = ~ DoY, y = ~cni, color = ~Year, colors = c("#D3D3D3","#D3D3D3","#D3D3D3","#D3D3D3","black","#E69F00","#56B4E9", "#009E73", "#AA4499"), type = 'scatter', mode = 'lines') %>%
layout(title = paste("Reports"), xaxis = list(title = "", type = "date", tickformat = "%d %b"), yaxis = list (title = "Number of reports")) %>%
add_trace(x = as.Date("1972-08-15"),type = 'scatter', mode = 'lines', line = list(color = 'red', width=0.5, dash ="dot"),name = '', showlegend = FALSE)
You should be able to use hoverinfo='none' in your add_trace() call
df %>%
filter(op_area_ocode == 'ALL') %>% mutate(Year = as.character(R.Year), DoY = as.Date(DoY.N, origin = "1972-01-01") - 1) %>%
plot_ly(., x = ~ DoY, y = ~cni, color = ~Year, colors = c("#D3D3D3","#D3D3D3","#D3D3D3","#D3D3D3","black","#E69F00","#56B4E9", "#009E73", "#AA4499"), type = 'scatter', mode = 'lines') %>%
layout(title = paste("Reports"), xaxis = list(title = "", type = "date", tickformat = "%d %b"), yaxis = list (title = "Number of reports")) %>%
add_trace(x = as.Date("1972-08-15"),
type = 'scatter',
mode = 'lines',
line = list(color = 'red', width=0.5, dash ="dot"),
name = '',
showlegend = FALSE,
hoverinfo="none"
)
I'd like to do plotly chart and plot filled area shape and bars on one plot. However area shape overlaying bars. I couldn't change order of elements. Is it possible to bring bars in fron?
data <- data.frame(years = c(2005,2006,2007), values1 = c(1,2,3), values2 = c(3,3,2))
plot_ly(data, x = data$years, y=data$values1, type = 'bar') %>%
add_trace(x=data$years, y=data$values2, type = 'scatter', mode = 'lines', fill = 'tozeroy')
This is adapted from the answer by #Maximilian Peters. This code
data <- data.frame(years = c(2005,2006,2007), values1 = c(1,2,3), values2 = c(3,3,2))
plot_ly(data) %>%
add_trace(x=~years, y=~values1, type = 'bar') %>%
add_trace( x = ~years, y=~values2, type = 'scatter', mode = 'lines', fill = 'tozeroy', yaxis='y2'
) %>%
layout(title = 'Trace order Plotly R',
xaxis = list(title = ""),
yaxis = list(side = 'left', title = 'Y - Axis', overlaying = "y2"),
yaxis2 = list(side = 'right', title = "" )
)
generates this output:
I would like to add the labels just above the 3 line plots (4 lables for each in total) with the library plotly.
Here is the code for the dataframe and the plot I want.
In commentary you can find what I've tried and didn't work : "add_text(textfont = t, textposition = "top right")"
Thank you
library(plotly)
df <- data.frame("Type" = c('A','B','R','M'),
`Nr.of.Delivered`= c(3000081, 300028, 100073, 100016),
`Nr.of.openviewed`=c(900001, 100009, 60006, 60005),
`Nr.of.unique.clicks`= c(107361,21531,14191,14427),
`open.rate`= c(25.9,32.8,39.4,43.54),
`click.rate`= c(4,6.4,8.90,10.61),
"cto"= c(11.57, 17.89,21.52,23.29))
df %>%
plot_ly () %>%
add_trace(x=~Type,
y=~`Nr.of.Delivered`,
type='bar',
name="Nr.of.Delivered",
marker = list(color = tier_pal, size=0.5)) %>%
add_trace(x=~Type,
y=~`open.rate`,
type="scatter",
mode="markers+lines",
yaxis = "y2",
name = "% of open rate",
marker = list(color = " #a67c39"),
line = list(color = " #a67c39")) %>%
#add_text(textfont = t, textposition = "top right") %>%
add_trace(df ,
x=~Type,
y=~`click.rate`,
type="scatter",
mode="markers+lines",
yaxis = "y2",
name="% of click rate",
marker = list(color = " #804638"),
line = list(color = " #804638") ) %>%
#add_text(textfont = t, textposition = "top right") %>%
add_trace(df ,
x=~Type,
y=~cto,
type="scatter",
mode="markers+lines",
yaxis = "y2",
color="red",
name="% of CTO",
marker = list(color = toRGB( 'dark blue')),
line = list(color = toRGB( 'dark blue'))) %>%
#add_text(textfont = t,textposition = "top right") %>%
layout (title = plot_name,
titlefont=list(size=14),
yaxis2 = list(overlaying = "y",
side = "right",
name = "Percentages",
ticksuffix="%",
range=c(0,60),
showgrid = F),
yaxis = list(showgrid=F),
legend = list(x = 100, y = 1.2))
I'm not sure why exactly this doesn't work, but it seems like you can combine the text labels directly into each plotly trace.
add_trace(x=~Type,
y=~open.rate,
type="scatter",
mode="markers+lines",
yaxis = "y2",
name = "% of open rate",
marker = list(color = " #a67c39"),
line = list(color = " #a67c39"),
text =~open.rate, textfont = t, textposition= "top right")
By the way, what is your variable t in the textfont = t parameter?
I'm trying to plot overlaid line and bar charts using plotly, grouped by the same feature grp.
I need to use fixed colors for both lines and bars.
It works well when I first add the bar-trace :
library(plotly)
df <- data.frame(year = rep((2000:2017), each=2),
grp = rep(c("Grp1", "Grp2"), 18),
y1 = rnorm(36, 100, 40),
y2 = rnorm(36, 50, 10)
)
plot_ly(df) %>%
add_trace( x = ~year, y = ~y1,
type = 'bar',
yaxis = "y2",
opacity = .4,
color = ~grp,
colors = colorRamp(c("grey", "black"))) %>%
add_trace(x = ~year, y = ~y2,
type = 'scatter',
mode = 'lines+markers',
linetype = ~grp,
line = list(color = "red")) %>%
layout(yaxis2 = list(overlaying = "y",
side = "right"))
But If I switch bar trace and line trace, my color selection for bars disappears.
plot_ly(df) %>%
add_trace(x = ~year, y = ~y2,
type = 'scatter',
mode = 'lines+markers',
linetype = ~grp,
line = list(color = "red")) %>%
add_trace( x = ~year, y = ~y1,
type = 'bar',
yaxis = "y2",
opacity = .4,
color = ~grp,
colors = colorRamp(c("grey", "black"))) %>%
layout(yaxis2 = list(overlaying = "y",
side = "right"))
Obviously my syntax is incorrect : does someone know how to write this code properly to ensure colors are stable whatever the order is ?
Many thanks !
Try to specify colors inside plot_ly:
plot_ly(df, colors = colorRamp(c("grey", "black"))) %>%
add_trace(x = ~year, y = ~y2,
type = 'scatter',
mode = 'lines+markers',
linetype = ~grp,
line = list(color = "red")) %>%
add_trace( x = ~year, y = ~y1,
type = 'bar',
yaxis = "y2",
opacity = .4,
color = ~grp) %>%
layout(yaxis2 = list(overlaying = "y",
side = "right"))
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.