[英]How do you add plot borders to Plotly subplots in R?
我正在嘗試制作具有共享軸的繪圖網格,並且我希望每個子圖都有繪圖邊框(對於整個繪圖區域而不是有邊框,這是可以接受的,但不是理想的)。 我無法完成這項工作,結果讓我覺得這在 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'))
如果您在布局屬性中為每個圖指定相同的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
我敢肯定有人會為您提供一個純粹的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)
設置 shareX 和 shareY = FALSE 以保留邊框。
注意,如果您在 SEAnalyst 提供的代碼中也設置了 shareX 或 shareY = TRUE,您將看到一些邊框也沒有保留。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.