How to change axis features in plotly? - r

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.

Related

Change the labels of a 3d plot using R plotly

I created following 3d plot using plotly in R.
library(plotly)
library(MASS)
mu_v=c(0.5,0.4,0.5,0.2)
sigma_m=matrix(data = c(1,0.5,0.7,0.4,0.5,1,0.8,0.9,0.7,0.8,1,0.5,0.4,0.9,0.5,1),nrow = 4,byrow = T)
data1=mvrnorm( 10000,mu = mu_v , Sigma = sigma_m )
data1=as.data.frame(data1)
colnames(data1)=c("X1","X2","X3","X4")
plot_ly(x=~X1, y=~X2, z=~X3, type="scatter3d", color=~(X4), mode="markers",data = data1
, marker = list(
size = 5,
opacity = 10)) %>% layout(plot_bgcolor = "#bababa")
Is there a way to change the labels of axis and the coloring variable ? Also Can I change the background of the xy,yz and xz surface and increase the plotting area?
Update
I tried to edit the legend and change the background color as follows:
plot_ly(x=~X1, y=~X2, z=~X3, type="scatter3d", color=~(X4), mode="markers",data = data1
, marker = list(
size = 5,
opacity = 10)) %>%
layout(scene = list(legend = list(title = "legend"),plot_bgcolor='#e5ecf6'))
But it didnt work
Here is an option to address your first 2 questions (change x/y/z axis and color legend titles).
plot_ly(
x = ~X1,
y = ~X2,
z = ~X3,
type = "scatter3d",
mode = "markers",
data = data1,
marker = list(
color = ~X4,
colorbar = list(title = "Legend"), # Change colour legend title
colorscale = "Blues", # Change colour palette
size = 5,
opacity = 10)) %>%
layout(
plot_bgcolor = "#bababa",
scene = list(
xaxis = list(title = "Axis 1"), # Change x/y/z axis title
yaxis = list(title = "Axis 2"),
zaxis = list(title = "Axis 3")))

Setting colours in plotly R is not working (factors) in add markers

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

R plotly stacked bar chart text labels not aligned to bars

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

Overlaying scatter area in plot_ly

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:

Plot.ly Double- Axis Formatting Content in R

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 = "$"))

Resources