Adjust the yAxis fontSize in highchart in R - r

Is there a way to increase the yAxis labels in highcharts in R?
highchart() %>%
hc_title(text = "some plot name") %>%
hc_add_series(subset1_melted_nona,type = "scatter", hcaes(x = variable, y = value), value= 10, color="darkblue", name="freq", marker = list(radius = 2)) %>%
hc_add_series(subset1_melted_nona, type = "arearange", hcaes(x=variable, low =errmin, high=errmax), yAxis=1, name="cov", color="red") %>%
hc_add_series(subset1_melted_nona, type= "line", hcaes(x = variable, y = enb), yAxis=2, name="comp", color="#CC6600") %>%
hc_yAxis_multiples(
list(lineWidth = 3, lineColor="darkblue",title=list(text="freq")),
list(lineWidth = 3, opposite = TRUE, lineColor="red", title=list(text="cov")),
list(lineWidth = 3, opposite = TRUE, lineColor="#CC6600", title=list(text="comp")))

You can use JS to edit labels from the available Highcharts JS API:
https://api.highcharts.com/highcharts/xAxis.labels.style.fontSize
You can find an article on how to do this here:
https://www.highcharts.com/blog/tutorials/working-with-highcharts-javascript-syntax-in-r/

Related

Plotly R - order data on y axis based on numeric value (x axis)

I have this data:
myData <- data.frame(Zupanija = c('GZ','ZGZ','KZ','VZ','KK','M','BB','VP','PS','BP','OB','VU','KA','SM','PG','LS','ZA','ŠK','SD','IS','DN'),
Inv = c(5205740135,2017069611,568884637,908563032,487561769,735161926,284291246,195329869,257799660,295494321,721957349,383617802,464253852,298576348,1182794616,277411269,677612459,405016102,3041655541,1039402830,642317513))
I want to do simple Plotly point graph and I would like to order data on y axis by the Inv variable (x axis) with this code:
myData %>%
plot_ly(x = ~Inv, y = ~ Zupanija) %>%
add_trace(type = 'scatter',
mode = 'markers',
stroke = I("black"),
span = I(1),
size= ~ sqrt(Inv),
color = ~ sqrt(Inv),
colors = inferno(50, alpha = 1, begin = 0, end = 1, direction = 1),
alpha = 1,
showlegend = FALSE)%>%
hide_colorbar()%>%
layout(xaxis = list(title = "HAMAG - ukupna ulaganja"),
yaxis = list(title=F, categoryorder = "total ascending"))
You can see that altough I put this part of the code layout(xaxis = list(title = "HAMAG - ukupna ulaganja"), yaxis = list(title=F, categoryorder = "total ascending")) - I do not get wanted result. Some variables are sorted but some variables on the y axis are not sorted based on x value: e.g. ZGZ (2B) is above GZ (5B) and BB is above KA, although it has smaller value on x. Does anyone see my mistake? tnx.
One option would be to manually arrange your dataset in your desired order and setting the categoryorder to "trace":
library(plotly)
library(viridis)
myData %>%
arrange(reorder(Zupanija, Inv)) %>%
plot_ly(x = ~Inv, y = ~Zupanija) %>%
add_trace(
type = "scatter",
mode = "markers",
stroke = I("black"),
span = I(1),
size = ~ sqrt(Inv),
color = ~ sqrt(Inv),
colors = inferno(50, alpha = 1, begin = 0, end = 1, direction = 1),
alpha = 1,
showlegend = FALSE
) %>%
hide_colorbar() %>%
layout(
xaxis = list(title = "HAMAG - ukupna ulaganja"),
yaxis = list(title = F, categoryorder = "trace")
)

Plotly: How to remove the confidence interval values (from the error bar) in the tooltip?

I am trying to remove the error bar values in the tooltip in an R Plotly graph.
I have tried playing around with the hovertext arguments from here: https://plot.ly/r/hover-text-and-formatting/ but can't get it to work.
I have these in a function where sometimes there are error bars, but most of the time there aren't (because I don't have the data) so the extra detail in the tooltip is not needed (as it just shows +0/-0).
With the example below, I would want it to just show 2010, 5 without the confidence intervals.
Any ideas?
library(tidyverse)
library(plotly)
data <- tibble(x = c(2010, 2011, 2012),
y = c(5, 6, 7),
err_high = c(1, 1, 1),
err_low = c(0.9, 1, 1.1))
#plotly graph
plot_ly() %>%
add_trace(data = data, x = ~x, y = ~y,
name = 'Actual', type = 'scatter', mode = 'lines+markers',
line = list(shape = 'linear', width= 4, dash = 'solid'),
error_y = list(type = "data", symmetric = FALSE, array = ~err_high, arrayminus = ~err_low)) %>%
layout(xaxis = list(title = 'Year'),
yaxis = list (title = 'Value', rangemode = "tozero"))
You can include
text=paste(data$x, data$y, sep=', '),
hoverinfo='text',
in add_trace() to get this:
Plot:
Complete code:
library(tidyverse)
library(plotly)
data <- tibble(x = c(2010, 2011, 2012),
y = c(5, 6, 7),
err_high = c(1, 1, 1),
err_low = c(0.9, 1, 1.1))
#plotly graph
plot_ly() %>%
add_trace(data = data, x = ~x, y = ~y,
name = 'Actual', type = 'scatter', mode = 'lines+markers',
line = list(shape = 'linear', width= 4, dash = 'solid'),
text=paste(data$x, data$y, sep=', '),
hoverinfo='text',
error_y = list(type = "data", symmetric = FALSE, array = ~err_high, arrayminus = ~err_low)) %>%
layout(xaxis = list(title = 'Year'),
yaxis = list (title = 'Value', rangemode = "tozero"))

Change order of subplots

Is there a way to change the order of subplots in plotly for R? Is there a way to manually change the levels of a factor in this code?
I want a plot with Weight in the first plot followed by a,b,c in order above it. But what I get as output is Weight, c, a and b as shown in the image graph
Here is my code
df<-data.frame("time"= seq(0.01,10,length.out=100),"Weight"=1:100, "a"=rnorm(100),"b"=rnorm(100),"c"=rnorm(100))
q <- df%>%
tidyr::gather(variable, value, -time) %>%
transform(id = as.integer(factor(variable))) %>%
plot_ly(x = ~time, y = ~value, color = ~variable, colors = "Dark2",
yaxis = ~paste0("y", id)
) %>%
layout(
xaxis = list(title = "Time,s",tickfont = list(size = 17),titlefont = list(size = 20)),
yaxis = list(tickfont = list(size = 17), title="DP"),
hoverlabel = list(font=list(size=20))
) %>%
add_lines() %>%
subplot(nrows = length(df)-1, shareX = TRUE)
One way to do this is re-ordering factor levels as below:
# set.seed to keep the exact same results
set.seed(123)
df<-data.frame("time"= seq(0.01,10,length.out=100),"Weight"=1:100, "a"=rnorm(100),"b"=rnorm(100),"c"=rnorm(100))
DF <- df%>%
tidyr::gather(variable, value, -time) %>%
transform(id = as.integer(factor(variable)))
DF$variable <- factor(DF$variable, levels = c("Weight", "a", "b", "c")) #re-order
q <- DF %>%
plot_ly(x = ~time, y = ~value, color = ~variable, colors = "Dark2",
yaxis = ~paste0("y", sort(id, decreasing =F))) %>% #sort the order
layout(
xaxis = list(title = "Time,s",tickfont = list(size = 17),titlefont = list(size = 20)),
yaxis = list(tickfont = list(size = 17), title="DP"),
hoverlabel = list(font=list(size=20))
) %>%
add_lines() %>%
subplot(nrows = length(df)-1, shareX = TRUE)
q
You will need sort(id, decreasing =F) to get exact same order of what you set in factor(DF$variable, levels = c("Weight", "a", "b", "c")).
Exchange the data, the name, and the line features of the top and bottom subplots as following code,
#assign q$x$data to one template variable p
p = q$x$data
#exchange the data, name, and line features of q$x$data[[1]] and q$x$data[[4]]
q$x$data[[1]]$x = p[[4]]$x
q$x$data[[1]]$y = p[[4]]$y
q$x$data[[1]]$name = p[[4]]$name
q$x$data[[1]]$line = p[[4]]$line
q$x$data[[4]]$x = p[[1]]$x
q$x$data[[4]]$y = p[[1]]$y
q$x$data[[4]]$name = p[[1]]$name
q$x$data[[4]]$line = p[[1]]$line
#show
q
The problem might have been caused by yaxis = ~paste0("y", id). I replaced it with yaxis = ~paste0(id, "y") to get the correct order. You may need to change some code to get the right format.
library(plotly)
df<-data.frame("time"= seq(0.01,10,length.out=100),"Weight"=1:100, "a"=rnorm(100),"b"=rnorm(100),"c"=rnorm(100))
q <- df%>%
tidyr::gather(variable, value, -time) %>%
transform(id = as.integer(factor(variable))) %>%
plot_ly(x = ~time, y = ~value, color = ~variable, colors = "Dark2",
yaxis = ~paste0(id, "y")
) %>%
layout(
xaxis = list(title = "Time,s",tickfont = list(size = 17),titlefont = list(size = 20)),
yaxis = list(tickfont = list(size = 17), title="DP"),
hoverlabel = list(font=list(size=20))
) %>%
add_lines() %>%
subplot(nrows = length(df), shareX = TRUE)
q

R-plotly: Subplot shows only bottom plot

I would like to reproduce the "Scaled Subplots" in https://plot.ly/r/subplots/ for the mtcars data.
mtcars %>%
transform(id = as.integer(factor(am))) %>%
plot_ly(x = ~mpg, y = ~qsec, color = ~factor(vs), yaxis = ~paste0("y", id)) %>%
add_markers() %>%
subplot(nrows = 2, shareX = TRUE)
Only the bottom subplot shows up:
I thought that I did faithfully copy/translated the code, but something must be wrong.
Of note, the subplot discriminates am, whereas the color discriminates vs.
I tried am for both the subplot and the color:
mtcars %>%
transform(id = as.integer(factor(am))) %>%
plot_ly(x = ~mpg, y = ~qsec, color = ~am, yaxis = ~paste0("y", id)) %>%
add_markers() %>%
subplot(nrows = 2, shareX = TRUE)
It does not help much, but the two grids appear:
On the latter example, I expected the am==0 (blueish) dots to be in the top subplot.
Any suggestion?
packageVersion('plotly')
[1] ‘4.9.0’
Well, this way here you can do this, but must create two plots and than use subplot()
p1 = mtcars %>%
filter(am==0) %>%
plot_ly(x = ~mpg, y = ~qsec, color = ~vs, legendgroup = "am0", name = "0",
type = "scatter" , mode = "markers", showlegend = FALSE)
p2 = mtcars %>%
filter(am==1) %>%
plot_ly(x = ~mpg, y = ~qsec, color = ~vs, legendgroup = "am1", name = "1",
type = "scatter" , mode = "markers")
subplot(p1,p2,nrows = 2, shareX = TRUE, titleY = TRUE, which_layout = 1)
Here the output:

Add jitter to box plot using markers in plotly

I made a boxplot:
dat %>%
plot_ly(y = ~xval, color = ~get(col), type = "box",
boxpoints = "all", jitter = 0.7,
pointpos = 0, marker = list(size = 3),
source = shiny_source, key = shiny_key,
hoverinfo = 'text', text = txt)
but problem is that jittered points are not interactive and cannot be marked separately, so I came with an idea to add those points using add_markers:
dat %>%
plot_ly(y = ~xval, color = ~get(col), type = "box",
boxpoints = FALSE, jitter = 0.7,
pointpos = 0, marker = list(size = 3),
source = shiny_source, key = shiny_key,
hoverinfo = 'col', text = txt
) %>%
add_markers(x = ~get(col), y = ~varval, size = I(6))
but now points are in straight line and I'd like to add some jitter (for example by using beeswarm package). But I don't know how to get coordinates of qualitative variable IC0 on X axis. Any ideas?
I find myself in the same potential case often with plotly and ggplot2-- 3 lines of code to get 90% of what I want, and 30 lines of code to get the aesthetics just right.
One potential solution/workaround: Take advantage of R's "factors are coded with integers" paradigm, plot everything on a numeric scale, and then cover your tracks by hiding x labels and x hover values.
dat <- data.frame(xval = sample(100,1000,replace = TRUE),
group = as.factor(sample(c("a","b","c"),1000,replace = TRUE)))
dat %>%
plot_ly() %>%
add_trace(x = ~as.numeric(group),y = ~xval, color = ~group, type = "box",
hoverinfo = 'name+y') %>%
add_markers(x = ~jitter(as.numeric(group)), y = ~xval, color = ~group,
marker = list(size = 6),
hoverinfo = "text",
text = ~paste0("Group: ",group,
"<br>xval: ",xval),
showlegend = FALSE) %>%
layout(legend = list(orientation = "h",
x =0.5, xanchor = "center",
y = 1, yanchor = "bottom"
),
xaxis = list(title = "Group",
showticklabels = FALSE))
Yields the following

Resources