I would like to declutter a plot created with plotly and remove the repetitions of the year on the x-axis. The year should be shown only for the first month. For the rest of the labels the month (without the year) is enough.
I have tried to achieve this result with the ifelse function - without success (see reproducible example below). Is there any way to use ifelse or if_else to set the axis labels in plotly? I think it works that way in ggplot.
library(tidyverse)
library(plotly)
set.seed(42)
df <-
data.frame(date = seq(ymd('2021-01-01'), ymd('2021-12-12'), by = 'weeks'),
value = cumsum(sample(-10:20, length(seq(ymd('2021-01-01'), ymd('2021-12-12'), by = 'weeks')), replace = TRUE)))
df %>%
plot_ly(x = ~date, y = ~value) %>%
add_lines() %>%
layout(xaxis = list(dtick = "M1", tickformat = ~ifelse(date <= "2021-01-31", "%b\n%Y", "%b")))
Related
I am trying to plot two series at different scales on same plot with dygraph lib in r.
dygraph(data.frame(x = 1:10, y = runif(10),y2=runif(10)*100)) %>%
dyAxis("y", valueRange = c(0, 1.5)) %>%
dyAxis(runif(10)*100,name="y2", valueRange = c(0, 100)) %>%
dyEvent(2, label = "test") %>%
dyAnnotation(5, text = "A")
however, The plot does not fit the data with larger scale, I cannot figure out how to align the two axises. I suspect the option independentTicks in dyAxis() function does the trick but I cannot find how to use it in the documentation. Please help out with this. Best
One way could be:
We pass the named vector of the column with higher values to dySeries function:
See here https://rstudio.github.io/dygraphs/gallery-axis-options.html
library(dygraphs)
library(dplyr)
df = data.frame(x = 1:10, y = runif(10),y2=runif(10)*100)
y2 <- df %>%
pull(y2)
names(y2) <- df$x
dygraph(df) %>%
dySeries("y2", axis = 'y2')
Please opine on how the order of the x axis on a Plotly bar chart can be arranged.
I am using a toy example with the diamond dataset and trying to arrange clarity by ascending mean depth. I am very familiar with ggplot but quite new to plotly. I have seen some postings online regarding this issue but none seem to be definitive. After rendering the plot, I think that the clarity categories are indeed ordered correctly, hovering the mouse to get the label values would suggest this, but these values (61.3, 61.3, 61.4, 61.6, 61.7,61.7,61.8, 61.9 for all clarity groups) don't obviously map to the y axis which is on a scale of 0 to 16k. This is confusing me. I am not looking to use the ggplotly wrapper, I am looking for a plotly solution, thanks.
library(tidyverse)
library(plotly)
set.seed(321)
my_diamonds <- ggplot2::diamonds %>%
slice(sample(nrow(.), 1000))
my_diamonds %>%
group_by(clarity) %>%
mutate(mean_depth = mean(depth)) %>%
ungroup() %>%
plot_ly(
data = .
, x = ~ clarity
, y = ~ mean_depth
) %>%
layout(
title = "Mean Depth for each Clarity Category"
, xaxis = list(categoryorder = "array", categoryarray = ~ reorder(clarity, mean_depth))
)
Data has not been processed fully prior to plotting. Once you select the appropriate information, your code plots fine. I have subtracted 61 from the mean_depth as it will be easier to see the bar order. You can remove the subtraction. Try this
set.seed(321)
my_diamonds <- ggplot2::diamonds %>%
slice(sample(nrow(.), 1000))
my_diamonds %>%
group_by(clarity) %>%
mutate(mean_depth = mean(depth)-61) %>%
distinct(clarity,mean_depth) %>% arrange(mean_depth) %>%
ungroup() %>%
plot_ly(
data = .
, x = ~ clarity
, y = ~ mean_depth
) %>%
layout(
title = "Mean Depth for each Clarity Category"
, xaxis = list(categoryorder = "array", categoryarray = ~ reorder(clarity, mean_depth))
)
i have this small dataset that i need to plot with plotly. Im struggling with it :/
Im looking to have 3 colored lines (each line for each of the rows (1,2,3). The x axis needs to be the column names and the Y axis represents each one of the numeric values.
My code so far looks wrong
plot_ly (x = colnames(a), y = a[1], type = "scatter" ,mode = "lines" )
I'm not sure that this is your desired plot, but it sounded closest to your description. I adapted a few columns of your data to describe.
The plot will be easier if data is in longer form with pivot_longer. Also easier if you add row numbers to your data so you can plot one line for each row number.
Since plotly will plot your xaxis categories alphabetically, you will want to relevel your name factor (name is your column names) to order of your columns.
In your plot_ly statement, use split to plot by row number.
library(plotly)
library(tidyverse)
a %>%
mutate(rn = row_number()) %>%
pivot_longer(cols = -rn, names_to = "name", values_to = "value") %>%
mutate(name = factor(name, levels = colnames(a))) %>%
plot_ly(x = ~name, y = ~value, split = ~rn, type = "scatter", mode = "lines")
Output
Data
a <- data.frame(
N_of_Brands = c(-.4, .8, -.4),
Brand_Runs = c(-.26, .70, -.75),
Total_Volume = c(-.69, .15, -.015),
No_of_Trans = c(-.81, .45, -.35)
)
I am trying to make a boxplot in highchart to include it in a shiny app, along with another graph I already have.
The problem is that boxplot, as far as I can tell, do not behave like other plots and when you map a date to the x-axis, it is treated as a character string, this mean: the plot display the entire date ex: "2018-04-01" an not Apr'18 like it does in other plots.
Here I put a little reprex of what I have done
# Packages
library(tidyverse)
library(lubridate)
library(highcharter)
library(magrittr)
library(plotly)
# Data
stocks <- data.frame(
time = rep(as.Date('2009-01-01') + month(1:12), times = 10),
stock_price = rnorm(120, 0, 1)
)
# line plot
stocks %>%
group_by(time) %>%
summarise(mean_price = mean(stock_price)) %>%
hchart(.,
type = "line",
hcaes(x = "time",
y = "mean_price"))
# Box plot first try
# hchart boxplot
stocks %$%
hcboxplot(x = stock_price, time) %>%
hc_chart(type = "column")
After doing this first try, I try to create an abbreviated date and map it to the x-axis as follows, but the boxes are shown ordered alphabetically not chronologically
# hchart boxplot
stocks %>%
mutate(month = month(time, label = T),
year = str_extract(as.character(year(time)), "..$"),
time2 = paste(month, year, sep = "'")) %$%
hcboxplot(x = stock_price, time2) %>%
hc_chart(type = "column")
My desired output is a plot with x-axis like the line plot or like plotly's output
stocks %>%
group_by(time) %>%
plot_ly(x = ~time, y = ~stock_price, type = "box")
With the help of arrange() and fct_inorder(), I believe I've achieved your desired outcome:
stocks %>%
arrange(time) %>%
mutate(
month = month(time, label = T),
year = str_extract(as.character(year(time)), "..$"),
time2 = fct_inorder(paste(month, year, sep = "'"))
) %$%
hcboxplot(x = stock_price, time2) %>%
hc_chart(type = "column")
df <- data.frame(X1 = rep(1:5,1), X2 = rep(4:8,1), var1 = sample(1:10,5), row.names = c(1:5))
library("ggvis")
graph <- df %>%
ggvis(~X1) %>%
layer_lines(y = ~ var1) %>%
add_axis("y", orient = "left", title = "var1") %>%
add_axis("x", orient = "bottom", title = "X1") %>%
add_axis("x", orient = "top", title = "X2" )
graph
Obviously, the top x-axis (X2) is not correct here since it refers to the same variable as X1. I know how to create a scaled dual-y axis in ggvis. But how can I create a similar dual axis on different X? Those two X-axis should refer to different variables (X1 and X2 in this example).
I know this could be a really BAD idea to make dual X-axis. But one of my working dataset may need me to do so. Any comments and suggestions are appreciated!
The second axis needs to have a 'name' in order for the axis to know which variable to reflect. See below:
df <- data.frame(X1 = rep(1:5,1),
X2 = rep(4:8,1),
var1 = sample(1:10,5),
row.names = c(1:5))
library("ggvis")
df %>%
ggvis(~X1) %>%
#this is the line plotted
layer_lines(y = ~ var1) %>%
#and this is the bottom axis as plotted normally
add_axis("x", orient = "bottom", title = "X1") %>%
#now we add a second axis and we name it 'x2'. The name is given
#at the scale argument
add_axis("x", scale = 'x2', orient = "top", title = "X2" ) %>%
#and now we plot the second x-axis using the name created above
#i.e. scale='x2'
layer_lines(prop('x' , ~X2, scale='x2'))
And as you can see the top x-axis reflects your X2 variable and ranges between 4 and 8.
Also, as a side note: You don't need rep(4:8,1) to create a vector from 4 to 8. Just use 4:8 which returns the same vector.