plotly 中同一轴上的多个图
Multiple plots in the same axis in plotly
我正在尝试使用 plotly 绘制绘图,其中所有绘图都共享同一轴。
这是一个数据集
X variable value
1 1 SH 0.002814895
2 1 VH 0.002591173
3 1 SS 0.002599700
4 1 HCG 0.002810790
这是它的绘图
plot_ly(rr1, x=~variable, y=~value, type = 'scatter', marker=list(size=10, color='rgba(255,182,193,0.9)',
line=list(color='rgba(152,0,0,0.8)', width = 2)), name = "Character and Degree")%>%
layout(title = "Graph", font = list(family = "Arial Black"),yaxis=list(zerolinecolor = toRGB("red"),
gridcolor = "rgb(190,190,190)"), xaxis=list(gridcolor = "rgb(190,190,190)")) %>%
layout(plot_bgcolor='rgb(230, 230, 230)',paper_bgcolor='rgb(220, 220, 220)',
font = list(color = 'rgb(100,100,100)'))
这是另一个数据集
X variable value
1 1 SH 0.000982607
2 1 VH 0.000917569
3 1 SS 0.000911039
4 1 HCG 0.000971009
这是它的情节
plot_ly(rr2, x=~variable, y=~value, type = 'scatter', marker=list(size=10, color='rgba(38,182,193,0.9)',
line=list(color='rgba(75,0,0,0.8)', width = 2)), name = "Character and Degree")%>%
layout(title = "Graph", font = list(family = "Arial Black"),yaxis=list(zerolinecolor = toRGB("red"),
gridcolor = "rgb(190,190,190)"), xaxis=list(gridcolor = "rgb(190,190,190)")) %>%
layout(plot_bgcolor='rgb(230, 230, 230)',paper_bgcolor='rgb(220, 220, 220)',
font = list(color = 'rgb(100,100,100)'))
我希望这两个图共享同一个轴,颜色保持原样。
您可以合并两个数据框并使用两个 add_markers
,每个值列一个:
df1 <- read.table(text="X variable value
1 1 SH 0.002814895
2 1 VH 0.002591173
3 1 SS 0.002599700
4 1 HCG 0.002810790", header=TRUE)
df2 <- read.table(text=" X variable value
1 1 SH 0.000982607
2 1 VH 0.000917569
3 1 SS 0.000911039
4 1 HCG 0.000971009", header=TRUE)
df12 <- merge(df1, df2, by=c("X","variable"))
library(plotly)
plot_ly(df12, x=~variable) %>%
add_markers(y=~value.x, type = 'scatter') %>%
add_markers(y=~value.y, type = 'scatter')
但是y-axis标题和图例不太好:
所以,修改它们:
ax <- list(
title = "Value",
titlefont = list(
family = "Arial, sans-serif",
size = 18,
color = "Red"
),
showticklabels = TRUE
)
plot_ly(df12, x=~variable) %>%
add_markers(y=~value.x, type = 'scatter', name="value1") %>%
add_markers(y=~value.y, type = 'scatter', name="value2") %>%
layout(yaxis=ax)
我正在尝试使用 plotly 绘制绘图,其中所有绘图都共享同一轴。 这是一个数据集
X variable value
1 1 SH 0.002814895
2 1 VH 0.002591173
3 1 SS 0.002599700
4 1 HCG 0.002810790
这是它的绘图
plot_ly(rr1, x=~variable, y=~value, type = 'scatter', marker=list(size=10, color='rgba(255,182,193,0.9)',
line=list(color='rgba(152,0,0,0.8)', width = 2)), name = "Character and Degree")%>%
layout(title = "Graph", font = list(family = "Arial Black"),yaxis=list(zerolinecolor = toRGB("red"),
gridcolor = "rgb(190,190,190)"), xaxis=list(gridcolor = "rgb(190,190,190)")) %>%
layout(plot_bgcolor='rgb(230, 230, 230)',paper_bgcolor='rgb(220, 220, 220)',
font = list(color = 'rgb(100,100,100)'))
这是另一个数据集
X variable value
1 1 SH 0.000982607
2 1 VH 0.000917569
3 1 SS 0.000911039
4 1 HCG 0.000971009
这是它的情节
plot_ly(rr2, x=~variable, y=~value, type = 'scatter', marker=list(size=10, color='rgba(38,182,193,0.9)',
line=list(color='rgba(75,0,0,0.8)', width = 2)), name = "Character and Degree")%>%
layout(title = "Graph", font = list(family = "Arial Black"),yaxis=list(zerolinecolor = toRGB("red"),
gridcolor = "rgb(190,190,190)"), xaxis=list(gridcolor = "rgb(190,190,190)")) %>%
layout(plot_bgcolor='rgb(230, 230, 230)',paper_bgcolor='rgb(220, 220, 220)',
font = list(color = 'rgb(100,100,100)'))
我希望这两个图共享同一个轴,颜色保持原样。
您可以合并两个数据框并使用两个 add_markers
,每个值列一个:
df1 <- read.table(text="X variable value
1 1 SH 0.002814895
2 1 VH 0.002591173
3 1 SS 0.002599700
4 1 HCG 0.002810790", header=TRUE)
df2 <- read.table(text=" X variable value
1 1 SH 0.000982607
2 1 VH 0.000917569
3 1 SS 0.000911039
4 1 HCG 0.000971009", header=TRUE)
df12 <- merge(df1, df2, by=c("X","variable"))
library(plotly)
plot_ly(df12, x=~variable) %>%
add_markers(y=~value.x, type = 'scatter') %>%
add_markers(y=~value.y, type = 'scatter')
但是y-axis标题和图例不太好:
所以,修改它们:
ax <- list(
title = "Value",
titlefont = list(
family = "Arial, sans-serif",
size = 18,
color = "Red"
),
showticklabels = TRUE
)
plot_ly(df12, x=~variable) %>%
add_markers(y=~value.x, type = 'scatter', name="value1") %>%
add_markers(y=~value.y, type = 'scatter', name="value2") %>%
layout(yaxis=ax)