Change title position in plotly R - r

I created the world map for the dataset I am working with however, the title of the plot is way above the chart and there is a huge space in between.
map_World <- list(
scope = 'world',
lakecolor = toRGB('white'))
Map4 <- plot_geo(regions) %>%
add_trace(
z = ~column1.x, locations = ~`ISO`,
color = ~column1.x, colors = c("red", "blue", "green")
) %>%
colorbar(title = "Legend",x = 1, y = 0.8) %>%
layout(title = "Sub indices for the different dimensions",
geo = map_World)
I expect the title to be right above the chart but it is not working.

I believe you can adjust the chart title attributes:
https://github.com/plotly/plotly.js/pull/3276
Try for your layout something like:
layout(title = list(text = "Sub indices for the different dimensions", y = 0.8),
geo = map_World)

Related

plotly stacked area graph custom colours from named vector

I am trying to use custom colours with a dataset in Plotly. The named vector works from a line graph and other graphs, but doesn't for the stacked area graph. Any ideas?
Also posted here: https://community.rstudio.com/t/plotly-stacked-area-graph-custom-colours-from-named-vector/153342
library(plotly)
library(tidyverse)
library(palmerpenguins) # for the dataset
penguins_cols <- c("Adelie" = "blue",
"Gentoo" = "red",
"Chinstrap" = "green")
# works for line graphs
plot_ly(penguins,
colors = penguins_cols) %>%
add_trace(x = ~bill_length_mm,
y = ~bill_depth_mm,
color = ~species,
type = "scatter",
mode = "lines+markers")
# doesn't work for area graphs
plot_ly(penguins,
colors = penguins_cols) %>%
add_trace(x = ~bill_length_mm,
y = ~bill_depth_mm,
fillcolor = ~species,
mode = "none",
stackgroup = 'one')
Perhaps there is more direct way but from the docs setting the colors via fillcolor seems to be the way to go, i.e. use fillcolor = ~penguins_cols[species] and set the names for the legend entries via name = ~species.
library(ggplot2)
library(plotly)
library(palmerpenguins)
penguins_cols <- c("Adelie" = "blue",
"Gentoo" = "red",
"Chinstrap" = "green")
plot_ly(penguins) %>%
add_trace(x = ~bill_length_mm,
y = ~bill_depth_mm,
name = ~species,
fillcolor = ~penguins_cols[species],
mode = "none",
type = "scatter",
stackgroup = 'one')
#> Warning: Ignoring 2 observations

R Markdown not able to display two plots within the same if-statement

I am creating an R-Markdown document to help with reporting final exam results at our school. For the mathematics exam, I need a conditional statement to display appropriate plots, because the students do not need to take an oral exam (Oral = NA) if their written score is above a certain threshold. So I have an if-statement that checks whether the sum of the Oral_Exam variable (1 for those who had to take it, 0 otherwise) is larger than zero, and if so, create a 3D scatterplot where the students who had to take an oral exam are marked with red, followed by another plot of the same type, only with the students who had to go to the oral exam, colored according to oral exam result. If none of the students had to go to an oral exam, it is checked in a later if-statement, and only one plot is produced. My code looks like this:
```{r warning = FALSE, message = FALSE, echo = FALSE, eval = params$subj == "Matematika"}
if(sum(fulldata$Oral_exam) > 0){
fulldata_color = fulldata %>% mutate(Oral_exam, = as.character(Oral_exam), color = recode(Oral_exam, '1' = "red", '0' = "green"))
div(plot_ly(data = fulldata_color, x = ~Long_A_percent, y = ~Long_B_percent, z = ~Short_percent, marker = list(color = ~color), type="scatter3d", mode="markers", text = ~Name, width = 800, height = 800) %>% layout(
scene = list(aspectmode = "cube", xaxis = list(range = c(0,100), title = 'Long A (x)'),yaxis = list(range = c(0,100), title = 'Long B (y)'), zaxis = list(range = c(0,100), title = 'Short (z)'))), align = "center")
enter code here
Oral_data = fulldata %>% filter(!is.na(Oral_percent))
div(plot_ly(data = Oral_data, x = ~Long_A_percent, y = ~Long_B_percent, z = ~Short_percent,color = ~Oral_percent, type="scatter3d", mode="markers", text = ~Name, width = 800, height = 800) %>% layout(
scene = list(aspectmode = "cube", xaxis = list(range = c(0,100), title = 'Long A (x)'),yaxis = list(range = c(0,100), title = 'Long B (y)'), zaxis = list(range = c(0,100), title = 'Short (z)'))), align = "center")
}
This code, when knit, results in only the second plot being created, and it looks like the way I intend it to. However, if I break it up into two if statements with the same condition, and put one plotting command (and the corresponding command for the creation of the data frame), both plots are displayed correctly
I can work around it by having two if-statements instead of two, but it would be good to know why it doesn't work, especially since I have used multiple plots in the same code chunk (although not in the same if-statement) in the same document, and it has always worked as intended.
You can store plotly objets in variables and print them outside if:
```{r}
p1 <- NULL
p2 <- NULL
if(TRUE) {
p1 <- plot_ly(x = 1, y = 1, type = "scatter", mode = "marker")
p2 <- plot_ly(x = 1, y = 10, type = "scatter", mode = "marker")
}
p1
p2
```

Color code forest plot points and error bars by range

I have a data.frame of regression coefficients with the associated p-values:
library(dplyr)
set.seed(1)
effects.df <- data.frame(contrast = paste0("C",1:5), effect = rnorm(5), stringsAsFactors = F) %>%
dplyr::mutate(effect.error = abs(effect)/sqrt(5)) %>%
dplyr::mutate(p.value = pnorm(effect/effect.error)) %>%
dplyr::arrange(p.value)
effects.df$contrast <- factor(effects.df$contrast,levels = effects.df$contrast)
Which I want to display as a forest plot (X-axis are the effect size and Y-axis are the 'contrast's), where the points and their associated error bars (effect.error) are color coded by 1-p.value, using R's plotly.
Here's what I'm trying:
library(plotly)
effects.plot <- plot_ly(x = effects.df$effect, y = effects.df$contrast, type = 'scatter', mode = "markers", marker = list(size = 8, colorbar = "Hot", color = 1-effects.df$p.value)) %>%
layout(xaxis=list(title = "Effect size",zerolinewidth = 2, zerolinecolor = plotly::toRGB('black'), showgrid = F), yaxis = list(showgrid = F)) %>%
add_trace(error_x = list(array = effects.df$effect.error, width = 5),marker = list(size = 8,colorbar = "Hot", color = 1-effects.df$p.value))
It's close because it's color-coding the points how I want them to but not the error bars.
Any idea how to:
Color the error bars similar to the points?
Get the color-bar to show?
I'm not sure that it will allow you to color the error bars separately without some (a lot) of creativity. If you created separate traces for each color, you might be able to force it to comply.
There are many ways you could show the color bar. Here's one way:
(effects.plot <- plot_ly(data = effects.df,
x = ~effect,
y = ~contrast,
error_x = list(array = ~effect.error,
width = 5,
color = "black"),
type = 'scatter',
mode = "markers",
marker = list(colorscale = "Hot",
colorbar = list(size = 8),
color = 1 - effects.df$p.value)) %>%
layout(xaxis=list(title = "Effect size",
zerolinewidth = 2,
zerolinecolor = plotly::toRGB('black'),
showgrid = F),
yaxis = list(showgrid = F)) # set the joined color axis
)
By the way, I noticed that the colors you have are gray and red, not black and white, as shown in my image. You're getting a different color scale than you were expecting.
You can see what I mean by plotting this a different way:
(effects.plot <- plot_ly(data = effects.df,
x = ~effect,
y = ~contrast,
error_x = list(array = ~effect.error,
width = 5,
color = "black"),
type = 'scatter',
mode = "markers",
marker = list(coloraxis = "coloraxis",
color = 1 - effects.df$p.value)) %>%
layout(xaxis=list(title = "Effect size",
zerolinewidth = 2,
zerolinecolor = plotly::toRGB('black'),
showgrid = F),
yaxis = list(showgrid = F),
coloraxis = list(colorbar = "Hot", size = 8))
)
This plot is not using the "Hot" color scale. That scale is shown in the first image.
The easiest way to solve this is to use ggplot2 and then to convert it to a plotly object:
Libraries and data:
library(dplyr)
library(plotly)
library(ggplot2)
set.seed(1)
effects.df <- data.frame(contrast = paste0("C",1:5), effect = rnorm(5), stringsAsFactors = F) %>%
dplyr::mutate(effect.error = abs(effect)/sqrt(5)) %>%
dplyr::mutate(p.value = pnorm(effect/effect.error)) %>%
dplyr::arrange(p.value)
Here I also add a horizontal dashed y-line to mark the p-value = 0.05 cutoff:
effects.df$contrast <- factor(effects.df$contrast,levels=effects.df$contrast)
y.intercept <- min(which(effects.df$p.value > 0.05))-0.5
pp <- ggplot(effects.df)+geom_vline(xintercept=0,color="black")+geom_point(aes(y=contrast,x=effect,color=p.value))+
geom_errorbarh(aes(y=contrast,xmin=effect-effect.error,xmax=effect+effect.error,x=effect,color=p.value,height=0.1))+
scale_color_continuous(low="darkred",high="gray")+theme_minimal()+xlab("Effect Size")+
geom_hline(yintercept=y.intercept,linetype="dashed",color="black",size=0.25)
Which gives:
And the plotly object:
ggplotly(pp)

Plotly R: Interaction between subplot legend and highlight select

I am trying to get another plot to react to both legend (select/deselect levels of a factor) and drag select. In the following toy example drag select works as intended: the violin and boxplot are re-rendered to depict the points that are selected using the mouse. The legend works for the scatterplot, but not for the boxplot and violin that don't seem to share the legend. I read many posts about shared legend, but nothing worked for subplots of different types.
Even more surprisingly, when deplying to plotly cloud, the plot also loses it's drag select feature (altough I don't need the plotly cloud, I thought I should mention this).
Just to be clear, I'd like the boxplot and violin to be re-rendered based both on selecting on legend and drag-select on scatterplot. As these features theoretically work, the legend should provide the full data or subsets based on levels of the factor used for legend from which the drag selection could be carried out afterwards.
library(plotly)
d <- mtcars
d$cyl <- as.factor(d$cyl)
d <- highlight_key(mtcars)
sp <- plot_ly(d, x = ~mpg, y = ~disp,
color = ~factor(cyl), colors = c("red", "green", "blue"),
legendgroup = ~factor(cyl), showlegend = T) %>%
add_markers()
box <-
plot_ly(d, y = ~disp,
color = I("black"),
legendgroup = ~factor(cyl), showlegend = F) %>%
add_boxplot(name = " ")
violin <-
plot_ly(d, y = ~disp,
color = I("black"),
legendgroup = ~factor(cyl), showlegend = F) %>%
add_trace(type = "violin", name = " ")
p <-
subplot(sp, box, violin, shareY = TRUE, titleX = TRUE, titleY = TRUE) %>%
layout(
dragmode = "select",
barmode = "overlay",
title = "Click and drag scatterplot",
showlegend = TRUE
) %>%
highlight(on = "plotly_selected", off = "plotly_deselect")
p
Any help is greatly appreciated.

Is there a way to hide Trace Names in Plotly (specifically R)?

I've been wracking my brain over how to get rid of the trace name with plotly and can't seem to find anything. It seems adding the trace name is a unique feature of plotly boxplots. I could just name it " " but I need the original trace name so that I can reference it when overlaying a marker. I've simplified the code as much as possible to the root issue. Is there a way to hide the trace name?
housing = read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data")
colnames(housing) = c("CRIM","ZN","INDUS","CHAS","NOX","RM","AGE","DIS","RAD","TAX","PTRATIO","B","LSTAT","MEDV")
housing %>%
plot_ly( x = ~RM,
type="box",
name = "RM",
showlegend = FALSE
) %>%
add_markers(x=6, y="RM",
marker = list(color = "blue", size = 15)
)
If you want to hide the trace names in a box chart, you could hide the axis' labels by using showticklabels = F.
In the example below the trace name is also hidden in the hover labels by setting hoverinfo = 'x'.
library(plotly)
housing = read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data")
colnames(housing) = c("CRIM","ZN","INDUS","CHAS","NOX","RM","AGE","DIS","RAD","TAX","PTRATIO","B","LSTAT","MEDV")
housing %>%
plot_ly( x = ~RM,
y = 'RM',
type="box",
name = "RM",
showlegend = FALSE,
hoverinfo = 'x'
) %>%
add_markers(x=6, y="RM",
marker = list(color = "blue", size = 15)
) %>% layout(yaxis = list(showticklabels = F))
housing

Resources