Related
I'm trying to make a dual axis plot of rainfall and temperature. I have ordered the months on the bottom, but that causes my line graph to screw up. How do I make sure the added line uses the same x axis?
temprain<-data.frame(month = c(1:12),
Train = c(250,220, 180,97,38,27,31,47,70,140,200,250),
Tair = c(17,16, 15,13,9,6,5,9,12,13,14,16))
tempseq<-seq(0,20,by=0.5)
rainseq<-seq(0,260,by=1)
xlab<-list(type = "category",
categoryorder = "array",
categoryarray = month.name,
showgrid = TRUE,
showline = TRUE,
autorange = TRUE,
showticklabels = TRUE,
ticks = "outside",
tickangle = 0
)
plot_ly(temprain) %>%
add_bars(x = ~MonthName, y = ~Train, type = "bar", name = "Rain") %>%
add_lines(x = ~MonthName, y = ~Tair, yaxis = "y2", name = "Temp") %>%
layout(xaxis = xlab,
yaxis = list(showline = TRUE, side = "left",
title = "Rainfall (mm)Temp", range = tempseq),
yaxis2 = list(showline = TRUE, side = "right",
overlaying = "y", title = "Air Temp (C)", range = rainseq),
showlegend = FALSE,
margin = list(pad = 0, b = 50, l = 50, r = 50))
I tried this as well, and it doesn't work, the temp graph disappears
plot_ly(temprain, x = ~MonthName, y = ~Tair, name = "Temp") %>%
add_bars(x = ~MonthName, y = ~Train, yaxis = "y2", type = "bar", name = "Rain") %>%
layout(xaxis = xlab,
yaxis = list(showline = TRUE, side = "left",
title = "Air Temp (C)", range = tempseq),
yaxis2 = list(showline = TRUE, side = "right",
overlaying = "y",
title = "Rainfall (mm)", range = rainseq),
showlegend = FALSE,
margin = list(pad = 0, b = 50, l = 50, r = 50))
Below is the solution:
Your data:
temprain<-data.frame(month = c(1:12),
Train = c(250,220, 180,97,38,27,31,47,70,140,200,250),
Tair = c(17,16, 15,13,9,6,5,9,12,13,14,16))
Generate a column for month abbreviations from month:
mymonths <- c("Jan","Feb","Mar",
"Apr","May","Jun",
"Jul","Aug","Sep",
"Oct","Nov","Dec")
# match the month numbers against abbreviations:
temprain$MonthAbb = mymonths[ temprain$month ]
# This is the code to archieving a consistent combined graph:
temprain$MonthAbb <- factor(temprain$MonthAbb, levels = c(as.character(temprain$MonthAbb)))
Now plot your data:
fig <- plot_ly(temprain)
# Add the Train trace:
fig <- fig %>% add_trace(x = ~MonthAbb, y = ~Train, name = "Train", type = "bar")
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "<b>Tair</b>")
# Add the Tair trace:
fig <- fig %>% add_trace(x = ~MonthAbb, y = ~Tair, name = "Tair", yaxis = "y2", mode = "lines+markers", type = "scatter")
fig <- fig %>% layout(yaxis2 = ay,
xaxis = list(title="Month"),
yaxis = list(title="<b>Train</b>"))%>%
layout(xaxis = list(
zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
yaxis = list(
zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff')
)
fig
Output:
I am using Plotly to make a scatter ternary plot. I want to color points by one of the values in the data frame (titled mu). However, the colorbar isn't showing. Here is my code:
library(plotly)
df <- eqData0
# axis layout
axis <- function(title) {
list(
title = title,
titlefont = list(
size = 20
),
tickfont = list(
size = 15
),
tickcolor = 'rgba(0,0,0,0)',
ticklen = 5
)
}
fig <- df %>% plot_ly()
fig <- fig %>% add_trace(
type = 'scatterternary',
mode = 'markers',
a = ~u1eq,
b = ~u2eq,
c = ~bueq,
marker = list(
symbol = 100,
color = ~mu,
size = 14,
line = list('width' = 2),
colorscale = 'YlGnBu'
),
colorbar = list(
xanchor = 'middle',
yanchor = 'middle'
)
)
m <- list(
l = 50,
r = 50,
b = 100,
t = 100,
pad = 4
)
fig <- fig %>% layout(autosize = F, width = 500, height = 500, margin = m,
ternary = list(
sum = 1,
aaxis = axis(TeX("$u_1$")),
baxis = axis(TeX("$u_2$")),
caxis = axis(TeX("$\\bar{u}$"))
)
)
fig <- fig %>% config(mathjax = 'cdn')
fig
Somehow the colorbar is still not showing! I'm not sure why because all the Plotly scatterplot examples online make getting the colorbar to show up seem easy.
It looks like you were missing showscale=TRUE in the trace definition.
Trying:
#fake data:
df <- data.frame(u1eq = c(0.2, 0.3, 0.5), u2eq=c(0.6, 0.3, 0.1), bueq=c(0.2, 0.4, 0.4), mu=c(1, 1.8, 2))
# axis layout
axis <- function(title) {
list(
title = title,
titlefont = list(
size = 20
),
tickfont = list(
size = 15
),
tickcolor = 'rgba(0,0,0,0)',
ticklen = 5
)
}
fig <- df %>% plot_ly()
fig <- fig %>% add_trace(
type = 'scatterternary',
mode = 'markers',
a = ~u1eq,
b = ~u2eq,
c = ~bueq,
marker = list(
colorscale = 'YlGnBu',
symbol = 100,
color = ~mu,
size = 14,
line = list('width' = 2),
showscale = TRUE
)
)
m <- list( l = 50, r = 50, b = 100, t = 100, pad = 4)
fig <- fig %>% layout(autosize = F, width = 500, height = 500, margin = m,
ternary = list(
sum = 1,
aaxis = axis(TeX("$u_1$")),
baxis = axis(TeX("$u_2$")),
caxis = axis(TeX("$\\bar{u}$")) )
) %>% config(mathjax = 'cdn')
fig
I am trying to connect the stacked bars with lines.
Expectation :
However I am unable to draw the lines between the bars. Have tried with the following script however it is not adding the line.
Using add_trace instead of 'add_lines' is not working.
df = data.frame(Aria = 20:25, Acqua = 21:26, Fuoco = 22:27,
Terra = 23:28, Cielo = 24:29,
Labels = c( 'Antonio', 'Maria', 'Giovanni',
'Sergio', 'Giorgio', 'Michele' ) )
evo_bar_plot_variant = function(plot_data, var_x, x_name = 'X axis',
y_name = 'Y axis', ... ){
df = data.frame(plot_data)
df = na.omit(df)
var = quos(...)
names_vars = names( var )
y_vars = names_vars[ startsWith( names_vars, 'var_y' ) ]
y_var_names = sapply(1:length(y_vars), function(j){
quo_name(var[[y_vars[j]]] )})
row_sum = df %>%
select( y_var_names ) %>%
rowSums()
xenc = enquo( var_x )
cols = colorRampPalette(c("white", "#4C68A2"))( length( y_vars ) )
#... Plot parameters .....
font_size = list( size = 12, family = 'Lato' )
gray_axis = '#dadada'
p = plot_ly(data = df, x = xenc, y = var[[ y_vars[1] ]],
name = quo_name( var[[ y_vars[1] ]] ),
type = 'bar', marker = list( color = cols[1],
line = list( color = '#E1E1E1', width = 0.8 ) ),
hoverlabel = list( font = font_size ) ) %>%
layout(title = list( text = 'Bar', x = 0 ), barmode = 'stack',
yaxis = list( title = y_name, showgrid = F,
zerolinecolor = gray_axis,
titlefont = font_size, side = 'right' ),
xaxis = list(title = x_name, linecolor = gray_axis,
zerolinecolor = gray_axis,
tickfont = font_size, titlefont = font_size),
legend = list(font = font_size, orientation= 'h',
font = font_size, x = 1 , y = 1.2,
xanchor = "left", yanchor = 'top' ))
if( length( y_vars ) >= 2 ){
for( i in 2:length( y_vars ) ){
p = p %>%
add_trace(y = var[[ y_vars[i] ]],
name = quo_name( var[[ y_vars[i] ]] ),
marker = list(color = cols[i],
line = list(color = '#E1E1E1', width = 0.8)),
hoverlabel = list(font = font_size))
}
}
p = p %>%
add_annotations(xref = 'x', yref = 'y',
y = ( row_sum ) + 5, x = xenc,
text = paste( row_sum ),
font = font_size, showarrow = F )
p
}
evo_bar_plot_variant( df, var_x = Labels, var_y1 = Aria, var_y2 = Acqua, var_y3 = Fuoco, var_y4 = Terra,
var_y5 = Cielo )
Getting output like this :
Sorry, I dropped a few lines of your example, as it isn't really minimal. Furthermore, I switched from dplyr to data.table as I'm more familar with it and melting the table makes thing much easier.
However, I hope the following is still helpful to you:
library(plotly)
library(data.table)
DF = data.frame(
Aria = 20:25,
Acqua = 21:26,
Fuoco = 22:27,
Terra = 23:28,
Cielo = 24:29,
Labels = c('Antonio', 'Maria', 'Giovanni',
'Sergio', 'Giorgio', 'Michele')
)
setDT(DF)
DT <- melt.data.table(DF, id.vars = "Labels")
DT[, c("label_group", "cumsum_by_label") := .(.GRP, cumsum(value)), by = Labels]
lineDT <- rbindlist(list(DT[, .(
label_group = label_group - 0.4,
cumsum_by_label = cumsum_by_label,
variable = variable
)],
DT[, .(
label_group = label_group + 0.4,
cumsum_by_label = cumsum_by_label,
variable = variable
)]))
p <- plot_ly(
DT,
x = ~ label_group,
y = ~ value,
color = ~ variable,
type = "bar",
colors = ~ colorRampPalette(c("white", "#4C68A2"))(length(unique(variable)) + 1)[-1],
legendgroup = ~ variable,
showlegend = TRUE
) %>%
layout(
title = list(text = 'Bar', x = 0),
barmode = 'stack',
legend = list(itemclick = FALSE, itemdoubleclick = FALSE)
) %>%
layout(
xaxis = list(
title = "X axis",
ticktext = ~ Labels,
tickvals = ~ label_group,
tickmode = "array"
),
yaxis = list(title = "")
) %>%
add_annotations(
text = ~ value,
xref = 'x',
yref = 'y',
y = ~ cumsum_by_label - value / 2,
x = ~ label_group,
showarrow = FALSE
) %>%
add_annotations(
data = DT[, .(maxval = max(cumsum_by_label),
label_group = unique(label_group)), by = Labels],
inherit = FALSE,
text = ~ maxval,
xref = 'x',
yref = 'y',
y = ~ maxval,
x = ~ label_group,
showarrow = FALSE,
yshift = 20
) %>%
add_lines(
data = lineDT,
inherit = FALSE,
x = ~ c(label_group),
y = ~ cumsum_by_label,
color = ~ variable,
legendgroup = ~ variable,
showlegend = FALSE,
hoverinfo = "none"
)
p
The labels within bubbles are appearing with size proportional to size argument. However I want to keep the labels in constant sizes.
Which argument should I use to keep them at constant size ?
Code that I am using is provided below.
df = data.frame( x = c( 3, 2, 2, 4, 6, 8 ), y = c( 3, 2, 3, 4, 6, 7 ), size = c( 20, 20, 20, 30, 40, 40 ), labels = letters[1:6] )
evo_bubble <- function(plot_data ,x_var, y_var, z_var, t_var) {
# Trasform data into dataframe and quos
df <- data.frame(plot_data)
xenc <- enquo(x_var)
yenc <- enquo(y_var)
zenc <- enquo(z_var)
tenc <- enquo(t_var)
df <- df %>% mutate( bubble_size = !!zenc*50 ) # Modify the denominator if you want to change the dimension of the bubble
# Set parameters for the plot
bubble_pal <- c("white", "#AECEE8" )
gray_axis <- '#dadada'
font_size <- list(size = 12, family = 'Lato')
width <- 0.5
legend_name <- Hmisc::capitalize( quo_name(zenc) ) # WATCH OUT! it works only with the package with Hmisc
decimal <- ',.2f'
sep <- ','
#x_name <- capitalize(quo_name(xenc))
y_name <- Hmisc::capitalize(quo_name(yenc))
p <- plot_ly(df, x = xenc, y = yenc, name = '', text = tenc, type = "scatter", mode = 'markers+text',
hoverlabel = list(font = font_size), size = zenc, color = zenc, hoverinfo = "text+y", colors= bubble_pal,
marker = list(size = df$bubble_size, line = list(color = gray_axis)) ) %>% hide_colorbar()
p <- p %>% layout(xaxis = list(zeroline = F,
title = '',
linecolor = gray_axis,
titlefont = font_size,
tickfont = font_size,
rangemode='tozero',
gridcolor = gray_axis,
gridwidth = width,
hoverformat = decimal,
mirror = "ticks",
tickmode = 'array',
tickcolor = gray_axis,
linewidth = width,
showgrid = F ),
yaxis = list(title = y_name,
zerolinecolor = gray_axis,
linecolor = gray_axis,
mirror = "ticks",
hoverformat = '.2f',
linewidth = width,
tickcolor = gray_axis,
tickformat = '.2f',
titlefont = font_size,
tickfont = font_size,
showgrid = FALSE) ) %>%
config(displayModeBar = F)
return(p)
}
evo_bubble( df, x, y, size, labels )
Expected image :
Obtained image :
Please ignore the colors in plot.
You can use add_text to get the desired result:
library(plotly)
library(dplyr)
DF = data.frame( x = c( 3, 2, 2, 4, 6, 8 ), y = c( 3, 2, 3, 4, 6, 7 ), size = c( 20, 20, 20, 30, 40, 40 ), labels = letters[1:6] )
evo_bubble <- function(plot_data, x_var, y_var, z_var, t_var) {
# browser()
# Trasform data into dataframe and quos
DF <- data.frame(plot_data)
xenc <- enquo(x_var)
yenc <- enquo(y_var)
zenc <- enquo(z_var)
tenc <- enquo(t_var)
DF <- DF %>% mutate( bubble_size = !!zenc*50 ) # Modify the denominator if you want to change the dimension of the bubble
# Set parameters for the plot
bubble_pal <- c("white", "#AECEE8" )
gray_axis <- '#dadada'
font_size <- list(size = 12, family = 'Lato')
width <- 0.5
legend_name <- Hmisc::capitalize( quo_name(zenc) ) # WATCH OUT! it works only with the package with Hmisc
decimal <- ',.2f'
sep <- ','
#x_name <- capitalize(quo_name(xenc))
y_name <- Hmisc::capitalize(quo_name(yenc))
p <- plot_ly(DF, x = xenc, y = yenc, name = '', type = "scatter", mode = 'markers',
hoverlabel = list(font = font_size), size = zenc, color = zenc, hoverinfo = "text+y", colors= bubble_pal,
marker = list(size = DF$bubble_size, line = list(color = gray_axis))) %>%
add_text(text = tenc, textfont = font_size, textposition = "middle center") %>% hide_colorbar()
p <- p %>% layout(xaxis = list(zeroline = F,
title = '',
linecolor = gray_axis,
titlefont = font_size,
tickfont = font_size,
rangemode='tozero',
gridcolor = gray_axis,
gridwidth = width,
hoverformat = decimal,
mirror = "ticks",
tickmode = 'array',
tickcolor = gray_axis,
linewidth = width,
showgrid = F ),
yaxis = list(title = y_name,
zerolinecolor = gray_axis,
linecolor = gray_axis,
mirror = "ticks",
hoverformat = '.2f',
linewidth = width,
tickcolor = gray_axis,
tickformat = '.2f',
titlefont = font_size,
tickfont = font_size,
showgrid = FALSE) ) %>%
config(displayModeBar = F)
return(p)
}
evo_bubble( DF, x, y, size, labels )
I am trying to plot waterfall chart with the following code. The only issue I am facing currently is the data marker which is not at the correct place. I want the data marker to be just below the end of each bar.
source('./r_files/flatten_HTML.r')
library("plotly")
dataset <- data.frame(Category = c("Akash Jain","Ankit Jain","Pankaj Jain","Nitin Pandey","Gopal Pandit","Ramnath Agarwal"),
TH = c(-62,-71,-1010,44,-44,200))
#dataset <- data.frame(Category = Values$Category, TH = Values$TH)
#dataset <- as.data.frame(cbind(Values$Category,Values$TH))
dataset$Category = dataset$Category
dataset$TH = dataset$TH
dataset$SortedCategoryLabel <- sapply(dataset$Category, function(x) gsub(" ", " <br> ", x))
dataset$SortedCategory <- factor(dataset$SortedCategoryLabel, levels = dataset$SortedCategoryLabel)
dataset$id <- seq_along(dataset$TH)
dataset$type <- ifelse(dataset$TH > 0, "in", "out")
dataset$type <- factor(dataset$type, levels = c("out", "in"))
dataset$end <- cumsum(dataset$TH)
dataset$start <- c(0, head(dataset$end, -1))
Hover_Text <- paste(dataset$Category, "= ", dataset$TH, "<br>")
dataset$colors <- ifelse(dataset$type =="out","red","green")
g <- plot_ly(dataset, x = ~SortedCategory, y = ~start, type = 'bar', marker = list(color = 'rgba(1,1,1, 0.0)'), hoverinfo = 'text') %>%
add_trace(y = dataset$TH , marker = list(color = ~colors), hoverinfo = "text", text = Hover_Text ) %>%
layout(title = '',
xaxis = list(title = ""),
yaxis = list(title = ""),
barmode = 'stack',
margin = list(l = 50, r = 30, b = 50, t = 20),
showlegend = FALSE) %>%
add_annotations(text = dataset$TH,
x = dataset$SortedCategoryLabel,
y = dataset$end,
xref = "dataset$SortedCategoryLabel",
yref = "dataset$end",
font = list(family = 'Arial',
size = 14,
color = "black"),
showarrow = FALSE)
g
Attached the screenshot of the waterfall chart.
So for the first bar, I need the data marker to be just below the end of red bar. Currently it is overlapping with the bar. And similarly for others.
Any help would be really appreciated.
Regards,
Akash
You should specify valign and height inside add_annotations:
vert.align <- c("bottom","top")[as.numeric(dataset$TH>0)+1]
g <- plot_ly(dataset, x = ~SortedCategory, y = ~start, type = 'bar',
marker = list(color = 'rgba(1,1,1, 0.0)'), hoverinfo = 'text') %>%
add_trace(y = dataset$TH , marker = list(color = ~colors), hoverinfo = "text",
text = Hover_Text ) %>%
layout(title = '',
xaxis = list(title = ""),
yaxis = list(title = ""),
barmode = 'stack',
margin = list(l = 50, r = 30, b = 50, t = 20),
showlegend = FALSE) %>%
add_annotations(text = dataset$TH,
x = dataset$SortedCategoryLabel,
y = dataset$end,
xref = "x",
yref = "y",
valign=vert.align, height=40,
font = list(family = 'Arial',
size = 14,
color = "black"),
showarrow = FALSE)
g