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?
Related
I'm trying to set the colour in R for "points" or markers in plotly with a custom palette. It doesn't work. I'm also trying to add a title to the graph, but it won't appear. In addition, I'd like to remove some elements of the legend (like the extra "z") and add a title to the legend elements. Nothing seems to work, even if it is present in the code.
library(plotly)
library(tidyverse)
set.seed(123456)
pal <- c("black", "orange", "darkgreen", "pink")
pal <- setNames(pal, levels(as.factor(c("gr1","gr2","gr3","gr4"))))
linedat = data.frame(x = rep(mean(1:50),2),
y = rep(mean(1:50),2),
z = c(0,1))
zoom = 3
pg = plotly::plot_ly(x = 1:50,
y = 1:50,
z = outer(1:50,1:50,"+")/100) %>%
add_surface(contours = list(
z = list(show = TRUE, start = 0, end = 1, size = 0.05)),
opacity = 1) %>%
add_markers(x = rnorm(50,25,5),
y = rnorm(50,25,5),
marker = list(opacity = 0.9),
type="scatter3d",
mode = "markers",inherit = FALSE,
colors = pal,
color = as.factor(sample(1:4,50,replace = T)),
z = rbinom(50,1,.5)) %>%
layout(scene=list(title = "Title won't show up????",
xaxis = list(title = 'trait1'),
yaxis = list(title = 'trait1',autorange = "reversed"),
camera = list(eye = list(x = cos(0.8*pi)*zoom, y = sin(pi*1.3)*zoom, z= 2.00)),
legend = list(title=list(text='<b> Groups </b>')))) %>%
add_trace(data=linedat, x=~x, y=~y, z=~z,
type="scatter3d", mode="lines",
line = list(color = pal[1],
width = 14),name = "Avg. data1")%>%
add_trace(data=linedat, x=~x+20, y=~y+20, z=~z,
type="scatter3d", mode="lines",
line = list(color = pal[4],
width = 14),name = "Avg. data2")
pg
See how here I'm able to set the colour of the points, but I'm not able to get the names of the colours to show in the legend (I modified the code to match what #Kat suggested):
library(plotly)
library(tidyverse)
set.seed(123456)
pal <- c("black", "orange", "darkgreen", "pink")
pal <- setNames(pal, levels(as.factor(c("gr1","gr2","gr3","gr4"))))
linedat = data.frame(x = rep(mean(1:50),2),
y = rep(mean(1:50),2),
z = c(0,1))
zoom = 3
pg = plotly::plot_ly(x = 1:50,
y = 1:50,
z = outer(1:50,1:50,"+")/100) %>%
add_surface(contours = list(
z = list(show = TRUE, start = 0, end = 1, size = 0.05)),
opacity = 1, colorbar = list(title = "Only one Z")) %>%
add_markers(x = rnorm(50,25,5),
y = rnorm(50,25,5),
marker = list(color = pal[as.factor(sample(1:4,50,replace = T))],opacity = 0.9),
type="scatter3d",
mode = "markers",inherit = FALSE,
colors = pal,
z = rbinom(50,1,.5)) %>%
layout(title = "Title that won't show",
margin = list(t = 40),
legend = list(title = list(
text = "<br>Legends deserve names, too")),
scene=list(xaxis = list(title = 'trait1'),
yaxis = list(title = 'trait1',autorange = "reversed"),
camera = list(eye = list(x = cos(0.8*pi)*zoom,
y = sin(pi*1.3)*zoom, z= 2.00)))) %>%
add_trace(data=linedat, x=~x, y=~y, z=~z,
type="scatter3d", mode="lines",
line = list(color = pal[1],
width = 14),name = "Avg. data1")%>%
add_trace(data=linedat, x=~x+20, y=~y+20, z=~z,
type="scatter3d", mode="lines",
line = list(color = pal[4],
width = 14),name = "Avg. data2");pg
Your original plot_ly call and add_trace calls can remain as is. I've included the changes needed for the layout call and added the call needed for colorbar.
The layout first.
layout(title = "Title that won't show", # <------ give my plot a name
margin = list(t = 40), # don't smash that title, either
legend = list(title = list(
text = "<br>Legends deserve names, too")), # <--- name my legend
scene=list(title = "Title won't show up????", # <- this can go away
xaxis = list(title = 'trait1'),
yaxis = list(title = 'trait1',autorange = "reversed"),
camera = list(eye = list(x = cos(0.8*pi)*zoom,
y = sin(pi*1.3)*zoom, z= 2.00)),
legend = list(title=list(text='<b> Groups </b>')))) %>%
colorbar(title = "Only one Z") # <--- give me one z colorbar title
(Some colors are different in the image; I didn't realize I had the pal object...sigh)
Update
This addresses your additional questions.
First, I'm not getting an error from the method in which I title the color bar. You'll have to share what error you're getting.
I didn't realize that it was ignoring the colors you set in markers, either. The easiest way to address this is to call that trace first. Since nothing else was declared within markers, I called opacity outside of the list, but it's fine the way you have it.
First, I commented out the setNames call, because that won't work for the marker's trace and it doesn't matter to the other traces.
library(plotly)
set.seed(123456)
pal <- c("black", "orange", "darkgreen", "pink")
# pal <- setNames(pal, levels(as.factor(c("gr1","gr2","gr3","gr4"))))
linedat = data.frame(x = rep(mean(1:50),2),
y = rep(mean(1:50),2),
z = c(0,1))
zoom = 3
I made plot_ly() empty and put all the data for the surface in that trace. I also added inherit = false so that the layout could go at the end without the data errors.
set.seed(123456)
pg = plotly::plot_ly() %>%
add_trace(inherit = F,
x = rnorm(50,25,5),
y = rnorm(50,25,5),
type="scatter3d",
mode = "markers",
opacity = .8,
colors = pal,
color = as.factor(sample(1:4, 50, replace = T)),
z = rbinom(50,1,.5)) %>%
add_surface(x = 1:50,
y = 1:50,
z = outer(1:50,1:50,"+")/100,
colorscale = "Viridis",
contours = list(
z = list(show = TRUE, start = 0, end = 1, size = 0.05)),
opacity = 1) %>%
add_trace(data=linedat, x=~x, y=~y, z=~z,
type="scatter3d", mode="lines",
line = list(color = pal[1],
width = 14),name = "Avg. data1", inherit = F) %>%
add_trace(data=linedat, x=~x+20, y=~y+20, z=~z,
type="scatter3d", mode="lines",
line = list(color = pal[4],
width = 14),name = "Avg. data2", inherit = F) %>%
The last part is the layout, but this is not different than my original answer.
layout(title = "Title that won't show", # <------ give my plot a name!
margin = list(t = 40), # don't smash that title, either
legend = list(title = list(
text = "<br>Legends deserve names, too"),
tracegroupgap = 350), # <--- name my legend!
scene=list(title = "Title won't show up????", # <--- this can go away!
xaxis = list(title = 'trait1'),
yaxis = list(title = 'trait1',autorange = "reversed"),
camera = list(eye = list(x = cos(0.8*pi)*zoom,
y = sin(pi*1.3)*zoom, z= 2.00)),
legend = list(title=list(text='<b> Groups </b>')))) %>%
colorbar(title = "Only one Z") # <--- give me one z for the title!
pg
I am trying to plot stacked bar chart using plot_ly() in R. The problem is I'm unable to place labels for each stack in the bar.
This is my data frame
df <- data.frame("QuarterYear" = c("2019 Q1","2019 Q2","2019 Q2","2019 Q3","2019 Q3","2019 Q3"), "Size" = c("Medium","Large","Medium","Large","Medium","Small"),
"percentage" = c(100,29,71,13,74,13))
This is the code for plotting the stacked bar chart
plot_ly(df, x = df$QuarterYear,
y = df$percentage,
type = 'bar',
name = df$Size,
text = paste(df$percentage,"%"),
textposition = 'top',
hoverinfo = 'text',
hovertext = paste('Size: ', df$Size,
'<br> % of Total count: ', paste(df$percentage,"%")),
color = df$Size) %>%
layout(yaxis = list(title = "% of Count", zeroline = FALSE,
showline = FALSE, ticksuffix = "%"), barmode = 'stack',hoverlabel = list(bgcolor= 'white')) %>%
layout(legend = list(orientation = "h",
xanchor = "center",
x = 0.5,
y = -0.13))
Can anyone help me to solve this?
Thanks in advance.
The y axis called in add_annotation needs a small workaround as below:
plot_ly(df, x = df$QuarterYear,
y = df$percentage,
type = 'bar',
name = df$Size,
text = paste(df$percentage,"%"),
textposition = 'top',
hoverinfo = 'text',
hovertext = paste('Size: ', df$Size,
'<br> % of Total count: ', paste(df$percentage,"%")),
color = df$Size) %>%
layout(yaxis = list(title = "% of Count", zeroline = FALSE,
showline = FALSE, ticksuffix = "%"), barmode = 'stack',
hoverlabel = list(bgcolor= 'white')) %>%
layout(legend = list(orientation = "h",
xanchor = "center",
x = 0.5,
y = -0.13)) %>%
add_annotations(text = paste0(df$percentage, "%"), x = df$QuarterYear,
y = unlist(tapply(df$percentage, df$QuarterYear, FUN=cumsum))-(df$percentage/2),
showarrow = FALSE)
I'm trying to create a simple bar chart using the plotly package in R. I want to add labels above each bar, but I've only be able to successfully add in the counts. Is it all possible to add in percentages next to each count? This is what I have:
fig_valve <- plot_ly(valve_df,
x = ~vlvsz_c,
y = ~count,
type = "bar",
hoverinfo = "x+y")
fig_valve <- fig_valve %>%
add_text(text = ~count,
textposition = "top",
textfont = list(size = 11, color = "black"),
showlegend = FALSE) %>%
layout(title = "",
xaxis = list(title = "Valve Size", showgrid = FALSE),
yaxis = list(title = "Count", showgrid = FALSE),
showlegend = FALSE,
font = t)
The output:
I'm wondering if I can add in the percentages for each category. Greatly appreciate any suggestions!
You can add the percentages next to the counts via text = ~paste0(count, " (", scales::percent(count / sum(count)), ")") where I use scales::percent for nice formatting. Using mtcars as example data, try this:
library(plotly)
library(dplyr)
library(scales)
fig_valve <- mtcars %>%
count(cyl, name = "count") %>%
plot_ly(
x = ~cyl,
y = ~count,
type = "bar",
hoverinfo = "x+y")
fig_valve <- fig_valve %>%
add_text(text = ~paste0(count, " (", scales::percent(count/sum(count)), ")"),
textposition = "top",
textfont = list(size = 11, color = "black"),
showlegend = FALSE) %>%
layout(title = "",
xaxis = list(title = "Valve Size", showgrid = FALSE),
yaxis = list(title = "Count", showgrid = FALSE),
showlegend = FALSE,
font = t)
fig_valve
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.