How can I round decimals in plotly? - r

Is there a way to round decimals in plotly? For example,
I am getting the values as 24.32544 M.
Instead of this, can I get as 24.32 M?
asd <- data.frame(a = c(1,2,3,4), b = c(24325443,35345442,3245353453,345353523), c = c(5435352345,234534534,324534534,23453532))
plot_ly(asd, x = ~a, y = ~b, name = 'b', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~c, name = 'c', mode = 'lines') %>%
layout(xaxis = list(title = paste0("Week"),showgrid = F,rangemode = "tozero"),
yaxis = list(title = "",showgrid = F,rangemode = "tozero"),
legend = legend_cus,
hovermode = 'x unified')

p <- ggplot(asd, aes(a, b,
text = paste0("a: ", round(a), "</br></br>b: ", round(b))))
+ geom_point()
ggplotly(p, tooltip = "text")
To conver to million format see: Format numbers with million (M) and billion (B) suffixes

Related

How can I add a subcategory to axis in Plotly with R?

I am trying to add a second category to x axis with Plotly under R like this:
Here is my code:
library(plotly)
data <- data.frame(day= c(1:4),
visit = c("visit1","visit1", "visit2", "visit2"),
val = c(1:4),
flag = c("","","", 1))
fig <- plot_ly(data= data, x = ~day) %>%
add_trace(y = ~val,
type = 'scatter',
mode = 'lines+markers',
line = list(width = 2,
dash = 'solid')) %>%
add_trace(data= data %>% filter(flag == 1), y = 0,
type = 'scatter',
hoverinfo = "text",
mode = 'markers',
name = "flag",
text = paste("<br>N°",data$ID[data$flag == 1],
"<br>Day",data$day[data$flag == 1]),
marker = list(
color = 'red',
symbol = "x",
size = 12
),
showlegend = T
)
fig
I have tried this, which seems good but the markers disappear from the graph, maybe due to the filter on data.
library(plotly)
data <- data.frame(day= c(1:4),
visit = c("visit1","visit1", "visit2", "visit2"),
val = c(1:4),
flag = c("","","", 1))
fig <- plot_ly(data= data, x = ~list(visit,day)) %>%
add_trace(y = ~val,
type = 'scatter',
mode = 'lines+markers',
line = list(width = 2,
dash = 'solid')) %>%
add_trace(data= data %>% filter(flag == 1), y = 0,
type = 'scatter',
hoverinfo = "text",
mode = 'markers',
name = "flag",
text = paste("<br>N°",data$ID[data$flag == 1],
"<br>Day",data$day[data$flag == 1]),
marker = list(
color = 'red',
symbol = "x",
size = 12
),
showlegend = T
)
fig
You didn't provide a reproducible question, so I've made data. (Well, data I shamelessly stole from here). This creates a bar graph with two sets of x-axis labels. One for each set of bars. One for each group of bars. The content of the x-axis is the same in both traces.
library(plotly)
fig <- plot_ly() %>%
add_bars(x = list(rep(c("first", "second"), each = 2),
rep(LETTERS[1:2], 2)),
y = c(2, 5, 2, 6),
name = "Adults") %>%
add_bars(x = list(rep(c("first", "second"), each = 2),
rep(LETTERS[1:2], 2)),
y = c(1, 4, 3, 6),
name = "Children")
fig
Update
You added data and code trying to apply this to your data. I added an update and apparently missed what the problem was. Sorry about that.
Now that I'm paying attention (let's hope, right?), here is an actual fix to the actual problem.
For this change, I modified your data. Instead of the flag annotated with a one, I changed it to zero. Then I used flag as a variable.
data <- data.frame(day = c(1:4),
visit = c("visit1","visit1", "visit2", "visit2"),
val = c(1:4),
flag = c("","","", 0))
fig <- plot_ly(data= data, x = ~list(visit,day)) %>%
add_trace(y = ~val,
type = 'scatter', mode = 'lines+markers',
line = list(width = 2,
dash = 'solid')) %>%
add_trace(y = ~flag,
type = 'scatter', hoverinfo = "text",
mode = 'markers', name = "flag",
text = paste("<br>N°",data$ID[data$flag == 1],
"<br>Day",data$day[data$flag == 1]),
marker = list(
color = 'red', symbol = "x", size = 12),
showlegend = T)
fig
You're going to get a warning about discrete & non-discrete data, which isn't really accurate, but it shows up, nonetheless.

Localize plotly plot time axis for dates in R

I would like to localize a plotly graph date axis date format for a special country. Instead of labeling dates in the time axis as "Jan 8 Jan 9 Jan 10...", I want to label them "Oca 8 Oca 9 Oca 10" in Turkish locale. The sample plotly code in R is given below. I know that I need to use config(locale = "tr_TR") to do this. But I am not sure how to do this in the code below.
plotly::plot_ly() %>%
plotly::add_trace(
x = ~date,
y = ~Belgium,
type = "scatter",
mode = "lines+markers",
name = "Türkiye"
) %>%
plotly::add_trace(
x = ~date,
y = ~France,
type = "scatter",
mode = "lines+markers",
name = "Fransa"
) %>%
plotly::add_trace(
x = ~date,
y = ~Spain,
type = "scatter",
mode = "lines+markers",
name = "İspanya"
) %>%
plotly::add_trace(
x = ~date,
y = ~Italy,
type = "scatter",
mode = "lines+markers",
name = "Italya"
) %>%
plotly::layout(
title = "",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Toplam Onaylı Vakalar"),
xaxis = list(title = "Date"),
# paper_bgcolor = "black",
# plot_bgcolor = "black",
# font = list(color = 'white'),
hovermode = "compare",
margin = list(
# l = 60,
# r = 40,
b = 10,
t = 10,
pad = 2
)
)```
I just added the following
)%>%
plotly::config(
locale='tr')
)
This configuration sets the date axis labeling based on system locale.

plotly in r 3d arrows between two points

I would like to draw arrows between two points. This is the problem, where I made the three arrows with a graphical program to explain my problem.
That is my source code:
require(plotly)
a <- c(5,16,16,17,18,23,25,27,37,42,5)
b <- c(13,12,26,15,14,6,10,22,14,25,17)
c <- c(12,14,25,26,8,9,27,30,31,26.5,12)
ap <- c(5,12,16,17,11,23,25,27,37,21.5,5)
bp <- c(13,12,26,15,14,6,10,22,14,12.5,17)
cp <- c(12,14,25,26,8,9,27,30,31,26,12)
rnn <- c("U1", "U2", "U3", "U4", "U5", "U6", "U7", "U8", "U9", "U10", "U11")
m <- list( symbol = 200, size = 8, line = list( color = toRGB("yellow"), width = 2 ) )
m1 <- list( symbol = "triangle", color="black", sizemod = "diameter", size = 5, line = list( color = "black", width = 2 ) )
plot_ly(x = ap, y = bp, z = cp) %>%
add_trace(marker = m1, type = "scatter3d", mode = "text+markers",
name = "projected", linetypes = NULL, text = rnn) %>%
add_trace(x = a, y = b, z = c, marker = m, type = "scatter3d", mode = "text+markers",
name = "original", linetypes = NULL, text = rnn)
Many thanks.
Not exactly what you are looking for, but you could try this:
aaa <- c(16, 12)
bbb <- c(12, 12)
ccc <- c(14, 14)
aaaa <- c(18, 11)
bbbb <- c(14, 14)
cccc <- c(8, 8)
aaaaa <- c(42, 21.5)
bbbbb <- c(25, 12.5)
ccccc <- c(26.5, 26)
plot_ly() %>%
add_trace(x = aaa, y = bbb, z = ccc, type = "scatter3d", mode = "lines",
name = "lines", showlegend = FALSE) %>%
add_trace(x = aaaa, y = bbbb, z = cccc, type = "scatter3d", mode = "lines",
name = "lines", showlegend = FALSE) %>%
add_trace(x = aaaaa, y = bbbbb, z = ccccc, type = "scatter3d", mode = "lines",
name = "lines", showlegend = FALSE) %>%
add_trace(x = ap, y = bp, z = cp, marker = m1, type = "scatter3d", mode = "text+markers",
name = "projected", linetypes = NULL, text = rnn) %>%
add_trace(x = a, y = b, z = c, marker = m, type = "scatter3d", mode = "text+markers",
name = "original", linetypes = NULL, text = rnn)

How to add text to a plotly boxplot in r

I would like to mark the outlier that appears on my chart writing where it is. Is this possible with plotly?
The code of my graph is here:
library(plotly)
set.seed(1234)
plot_ly(y = rnorm(50), type = 'box') %>%
add_trace(y = rnorm(50, 1)) %>%
layout(title = 'Box Plot',
xaxis = list(title = "cond", showgrid = F),
yaxis = list(title = "rating"))
It's not clear what you tried and what's not working, but one way to identify outliers is to use boxplot.stats() and then you can use that information to add annotations.
library(plotly)
set.seed(1234)
d <- rnorm(50)
d2 <- rnorm(50, 1)
plot_ly(y = d, type = 'box') %>%
add_trace(y = d2) %>%
layout(title = 'Box Plot',
xaxis = list(title = "cond", showgrid = F),
yaxis = list(title = "rating"),
annotations = list(
x = -0.01,
# use boxplot.stats() to get the outlier's y coordinate
y = boxplot.stats(d)$out,
text = "Outlier",
showarrow = FALSE,
xanchor = "right"
)
)

Second Y-Axis in a R plotly graph

I combined 2 charts and I am trying to add the second y-axis, but every time I add the yaxis = "y2" to my code, I lose have the my bar graphs.
> MediaDate Spend Search_Visits Other_Visits MediaDate2
> 2016-04-01 $39654.36 19970 2899 Apr 2016
> 2016-05-01 $34446.28 14460 2658 May 2016
> 2016-06-01 $27402.36 12419 2608 Jun 2016
my original code is:
p <- plot_ly(x= w$MediaDate2,y=w$Search_Visits,name = "Paid Search",
type = "bar")
p2 <- add_trace(p, x=w$MediaDate2, y=w$Other_Visits,name = "Other Traffic",
type = "bar")
spend_visits <- layout(p2, barmode = "stack")
spendvisits2 <- spend_visits %>% add_trace(data=w, x=MediaDate2, y=round(Spend,0), fill="tonexty", mode="lines",
text=w$MediaDate2, hoverinfo='name+y+text', name="Spend")
When I add the yaxis= "y2", only the area chart remains:
`spendvisits2 <- spend_visits %>% add_trace(data=w, x=MediaDate2, y=round(Spend,0), yxis="y2" fill="tonexty", mode="lines",
text=w$MediaDate2, hoverinfo='name+y+text', name="Spend")`
Any suggestions would be immensely helpful. Thank you
See this quick Plotly tutorial for multiple axes. You need to specify the attributes for the second y axis in layout().
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"))
Try this
plot_ly(df) %>%
add_trace(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"))
Adding type='scatter' to what Vance Lopez commented worked for me:
plot_ly(df, x = ~MediaDate, y = ~Spend, type = "bar", name = "Spend") %>%
add_trace(x = ~MediaDate, y = ~Visits, type = 'scatter', mode = "lines", yaxis = "y2", name = "Visits") %>%
layout(yaxis2 = list(overlaying = "y", side = "right"))

Resources