1
votes

I'm trying to draw plots using the plotly where all of them share the same axis. This is one dataset

  X variable       value
1 1       SH 0.002814895
2 1       VH 0.002591173
3 1       SS 0.002599700
4 1      HCG 0.002810790

and this is its plotly graph

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)'))

enter image description here

and this is the other dataset

   X variable       value
 1 1       SH 0.000982607
 2 1       VH 0.000917569
 3 1       SS 0.000911039
 4 1      HCG 0.000971009

and this is its plot

     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)'))

enter image description here

I want these two graphs to share the same axis leaving the colours as it is.

1

1 Answers

0
votes

You can merge the two dataframes and use two add_markers, one for each value column:

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')

But the y-axis title and the legends are not nice:

enter image description here

So, modify them:

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)

enter image description here