添加一个 plotly figure 作为 inset 到另一个 plotly figure
Add a plotly figure as an inset to another plotly figure
我认为这是数据科学中可能经常出现的一种绘图情况。
我有一些数据正在对其进行统计检验,我想将这些数据与统计检验的结果一起绘制在一个图中。
这是一个例子:
数据是两个分布(来自两组):
library(dplyr)
set.seed(1)
data.df <- data.frame(val = c(rnorm(100,0,1),rnorm(100,1,1)),
group = c(rep("A",100),rep("B",100)))
我使用密度图绘制这些数据:
density.df <- do.call(rbind,lapply(levels(data.df$group),function(g){
dens <- density(dplyr::filter(data.df,group == g)$val)
data.frame(x = dens$x, y = dens$y, group = g)
}))
library(plotly)
density.plot <- plot_ly(x = density.df$x, y = density.df$y, type = 'scatter', mode = 'lines',color = density.df$group) %>%
layout(xaxis = list(title= "Value", zeroline = F), yaxis = list(title = "Density", zeroline = F))
给出:
假设我用两个感兴趣的因素对它们拟合了一些统计模型,并获得了这些结果:
effects.df <- data.frame(effect = c(0.5, 0.75), effect.error = c(0.05,0.1), factor = c("Factor-A","Factor-B"))
我用所谓的毛虫图绘制这些数据:
effects.plot <- plot_ly(type = 'scatter', mode = "markers", marker = list(size = 13, color = "black"),
x = effects.df$effect, y = effects.df$factor, showlegend = F) %>%
layout(xaxis = list(range=c(-1,1), title = "Effect Size", zeroline = T, showticklabels = T, font = list(size=15)),
yaxis = list(title = NA, zeroline = F, showticklabels = T, tickvals = effects.df$factor, ticktext = as.character(effects.df$factor)), font = list(size = 15)) %>%
add_trace(error_x = list(array = effects.df$effect.error, width = 5, color = "black"), showlegend = F)
给出:
现在我想添加 effects.plot
作为 density.plot
右上象限的插图。我特别希望将其添加为插图,而不是使用 plotly
的 subplot
函数对它们进行分组。
所以我的问题是我该怎么做?
plotly
确实有一个 example 添加插图,但我尝试将我的案例放入其中但失败了。
有什么想法吗?
您需要使用 layout
定义一对新的轴(xaxis2
和 yaxis2
),然后将第二个绘图引用到该坐标系。
library(dplyr)
library(plotly)
set.seed(1)
data.df <- data.frame(val = c(rnorm(100,0,1),rnorm(100,1,1)),
group = c(rep("A",100),rep("B",100)))
density.df <- do.call(rbind, lapply(levels(data.df$group),
function(g) {
dens <- density(dplyr::filter(data.df,group == g)$val)
data.frame(x = dens$x, y = dens$y, group = g)
}) )
effects.df <- data.frame(effect = c(0.5, 0.75),
effect.error = c(0.05,0.1),
factor = c("Factor-A","Factor-B"))
density.plot <- density.df %>%
plot_ly(x = ~x, y = ~y, color = ~group,
type = 'scatter', mode = 'lines') %>%
add_markers(x = ~effect, y = ~factor, showlegend = F,
error_x=list(array=~effect.error, width=5, color="black"),
marker = list(size = 13, color = "black"),
xaxis = 'x2', yaxis = 'y2', data=effects.df, inherit=F) %>%
layout(xaxis = list(title= "Value", zeroline = F),
yaxis = list(title = "Density", zeroline = F),
xaxis2 = list(domain = c(0.7, 0.95), anchor='y2', range=c(-1,1), title = "Effect Size",
zeroline = T, showticklabels = T, font = list(size=15)),
yaxis2 = list(domain = c(0.5, 0.95), anchor='x2', title = NA, zeroline = F,
showticklabels = T, tickvals = effects.df$factor,
ticktext = as.character(effects.df$factor)))
我认为这是数据科学中可能经常出现的一种绘图情况。
我有一些数据正在对其进行统计检验,我想将这些数据与统计检验的结果一起绘制在一个图中。
这是一个例子:
数据是两个分布(来自两组):
library(dplyr)
set.seed(1)
data.df <- data.frame(val = c(rnorm(100,0,1),rnorm(100,1,1)),
group = c(rep("A",100),rep("B",100)))
我使用密度图绘制这些数据:
density.df <- do.call(rbind,lapply(levels(data.df$group),function(g){
dens <- density(dplyr::filter(data.df,group == g)$val)
data.frame(x = dens$x, y = dens$y, group = g)
}))
library(plotly)
density.plot <- plot_ly(x = density.df$x, y = density.df$y, type = 'scatter', mode = 'lines',color = density.df$group) %>%
layout(xaxis = list(title= "Value", zeroline = F), yaxis = list(title = "Density", zeroline = F))
给出:
假设我用两个感兴趣的因素对它们拟合了一些统计模型,并获得了这些结果:
effects.df <- data.frame(effect = c(0.5, 0.75), effect.error = c(0.05,0.1), factor = c("Factor-A","Factor-B"))
我用所谓的毛虫图绘制这些数据:
effects.plot <- plot_ly(type = 'scatter', mode = "markers", marker = list(size = 13, color = "black"),
x = effects.df$effect, y = effects.df$factor, showlegend = F) %>%
layout(xaxis = list(range=c(-1,1), title = "Effect Size", zeroline = T, showticklabels = T, font = list(size=15)),
yaxis = list(title = NA, zeroline = F, showticklabels = T, tickvals = effects.df$factor, ticktext = as.character(effects.df$factor)), font = list(size = 15)) %>%
add_trace(error_x = list(array = effects.df$effect.error, width = 5, color = "black"), showlegend = F)
给出:
现在我想添加 effects.plot
作为 density.plot
右上象限的插图。我特别希望将其添加为插图,而不是使用 plotly
的 subplot
函数对它们进行分组。
所以我的问题是我该怎么做?
plotly
确实有一个 example 添加插图,但我尝试将我的案例放入其中但失败了。
有什么想法吗?
您需要使用 layout
定义一对新的轴(xaxis2
和 yaxis2
),然后将第二个绘图引用到该坐标系。
library(dplyr)
library(plotly)
set.seed(1)
data.df <- data.frame(val = c(rnorm(100,0,1),rnorm(100,1,1)),
group = c(rep("A",100),rep("B",100)))
density.df <- do.call(rbind, lapply(levels(data.df$group),
function(g) {
dens <- density(dplyr::filter(data.df,group == g)$val)
data.frame(x = dens$x, y = dens$y, group = g)
}) )
effects.df <- data.frame(effect = c(0.5, 0.75),
effect.error = c(0.05,0.1),
factor = c("Factor-A","Factor-B"))
density.plot <- density.df %>%
plot_ly(x = ~x, y = ~y, color = ~group,
type = 'scatter', mode = 'lines') %>%
add_markers(x = ~effect, y = ~factor, showlegend = F,
error_x=list(array=~effect.error, width=5, color="black"),
marker = list(size = 13, color = "black"),
xaxis = 'x2', yaxis = 'y2', data=effects.df, inherit=F) %>%
layout(xaxis = list(title= "Value", zeroline = F),
yaxis = list(title = "Density", zeroline = F),
xaxis2 = list(domain = c(0.7, 0.95), anchor='y2', range=c(-1,1), title = "Effect Size",
zeroline = T, showticklabels = T, font = list(size=15)),
yaxis2 = list(domain = c(0.5, 0.95), anchor='x2', title = NA, zeroline = F,
showticklabels = T, tickvals = effects.df$factor,
ticktext = as.character(effects.df$factor)))