我正在尝试制作具有共享轴的图的网格,并且我希望每个子图都具有图边框(对于整个图区域而言,具有边框是可以接受的,尽管不理想,但可以接受)。我无法完成这项工作,结果使我认为在Plotly中可能无法实现。以下是我尝试过的三种变体以及结果。
library(plotly)
library(magrittr)
set.seed(0)
x <- seq(from=0, to=9, by=1)
y1 <- rnorm(10)
y2 <- rnorm(10)
y3 <- rnorm(10)
y4 <- rnorm(10)
# Attempt 1
p1 <- plot_ly(showlegend=FALSE) %>%
add_markers(x = x, y = y1)
p2 <- plot_ly(showlegend=FALSE) %>%
add_markers(x = x, y = y2)
p3 <- plot_ly(showlegend=FALSE) %>%
add_markers(x = x, y = y3)
p4 <- plot_ly(showlegend=FALSE) %>%
add_markers(x = x, y = y4)
p <- subplot(p1, p2, p3, p4,
nrows = 2, shareX = TRUE, shareY = TRUE) %>%
layout(title='Attempt 1', xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))
# Attempt 2
p1 <- plot_ly(showlegend=FALSE) %>%
add_markers(x = x, y = y1) %>%
layout(xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))
p2 <- plot_ly(showlegend=FALSE) %>%
add_markers(x = x, y = y2) %>%
layout(xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))
p3 <- plot_ly(showlegend=FALSE) %>%
add_markers(x = x, y = y3) %>%
layout(xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))
p4 <- plot_ly(showlegend=FALSE) %>%
add_markers(x = x, y = y4) %>%
layout(xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))
p <- subplot(p1, p2, p3, p4,
nrows = 2, shareX = TRUE, shareY = TRUE) %>%
layout(title='Attempt 2')
# Attempt 3
p1 <- plot_ly(showlegend=FALSE) %>%
add_markers(x = x, y = y1) %>%
layout(xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))
p2 <- plot_ly(showlegend=FALSE) %>%
add_markers(x = x, y = y2) %>%
layout(xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))
p3 <- plot_ly(showlegend=FALSE) %>%
add_markers(x = x, y = y3) %>%
layout(xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))
p4 <- plot_ly(showlegend=FALSE) %>%
add_markers(x = x, y = y4) %>%
layout(xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))
p <- subplot(p1, p2, p3, p4,
nrows = 2, shareX = TRUE, shareY = TRUE) %>%
layout(title='Attempt 3', xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))
答案 0 :(得分:3)
如果在布局属性中为每个图赋予相同的range
,则将保留您的单独边界线。
以下是我建议您定义范围的方法:
#find the max and min Y, which you will use as your range values
your_Ys<-c(y1,y2,y3,y4)
max_y<-ceiling(max(your_Ys))
min_y<-floor(min(your_Ys))
我没有在每个绘图中列出属性列表,而是在此处定义x和Y属性:
#These are the layout attributes for Y
ay <- list(
showline = TRUE,
mirror = "ticks",
linecolor = toRGB("black"),
linewidth = 2,
range = c(min_y, max_y)
)
#These are the layout attributes for X
ax <- list(
showline = TRUE,
mirror = "ticks",
linecolor = toRGB("black"),
linewidth = 2,
range = c(-1, 10)
)
现在是时候将它们放在一起了。
p1 <- plot_ly(showlegend=FALSE) %>%
add_markers(x = x, y = y1) %>% layout( xaxis = ax, yaxis = ay)
p2 <- plot_ly(showlegend=FALSE) %>%
add_markers(x = x, y = y2) %>% layout( xaxis = ax, yaxis = ay)
p3 <- plot_ly(showlegend=FALSE)%>%
add_markers(x = x, y = y3) %>%layout( xaxis = ax, yaxis = ay)
p4 <- plot_ly(showlegend=FALSE) %>%
add_markers(x = x, y = y4)%>% layout( xaxis = ax, yaxis = ay)
p <- subplot(p1, p2, p3, p4,
nrows = 2, shareX = FALSE, shareY = FALSE) %>%
layout(title='Tada!')
p
答案 1 :(得分:1)
我确定有人会为您提供纯粹的plotly
解决方案,但这是围绕我们制作ggplot
对象,然后转换为plotly
library(plotly)
library(tidyverse)
set.seed(0)
x <- seq(from=0, to=9, by=1)
y1 <- rnorm(10)
y2 <- rnorm(10)
y3 <- rnorm(10)
y4 <- rnorm(10)
p1 <-
{ggplot(tibble(x, y1), aes(x,y1))+
geom_point(color = "blue")+
labs(x='', y='')+
theme_bw()+
theme(panel.border = element_rect(color = "black"))} %>%
ggplotly()
p2 <-
{ggplot(tibble(x, y2), aes(x,y2))+
geom_point(color = "orange")+
labs(x='', y='')+
theme_bw()+
theme(panel.border = element_rect(color = "black"))} %>%
ggplotly()
p3 <-
{ggplot(tibble(x, y3), aes(x,y3))+
geom_point(color = "green")+
labs(x='', y='')+
theme_bw()+
theme(panel.border = element_rect(color = "black"))} %>%
ggplotly()
p4 <-
{ggplot(tibble(x, y4), aes(x,y4))+
geom_point(color = "red")+
labs(x='', y='')+
theme_bw()+
theme(panel.border = element_rect(color = "black"))} %>%
ggplotly()
subplot(p1, p2, p3, p4,nrows = 2, shareX = TRUE, shareY = TRUE)
答案 2 :(得分:0)
设置 shareX 和 shareY = FALSE 以保留边框。
注意如果您还在 SEAnalyst 提供的代码中设置了 shareX 或 shareY = TRUE,您将看到一些边框也没有保留。