Black Lives Matter. Please consider donating to Black Girls Code today.

Multiple X Axis Line chart using R (subplot)

Hi there!

I’m trying to create a multiple X axis line chart. Code snippet below

p1 <- plot_ly(M, x=interaction(M[[4]],M[[3]]), y=M[[5]], color= M[[1]], colors=“Accent”, mode = “markers+lines”)
p2 <- plot_ly(F, x=interaction(F[[4]],F[[3]]), y=F[[5]], color=F[[1]], colors=“Set3”, mode = “markers+lines”)
p3 <- subplot(p1,p2, nrows =2, shareX=FALSE,shareY=FALSE, margin=0.05) %>% layout(
autosize = TRUE,
showlegend = FALSE,
font = list(family=“Arial”,size=9,color=“gray”),
xaxis= list(title=“M”, titlefont = list(size=10),zeroline = FALSE,showgrid = FALSE, type = “category”, categoryorder=“category ascending”)
xaxis2= list(title=“F”, titlefont = list(size=10),zeroline = FALSE,showgrid = FALSE, type = “category”, categoryorder=“category ascending”),
yaxis = list(showticklabels=TRUE, autorange = TRUE, title=“Y1”, ticks=“inside”, zeroline=FALSE),
yaxis2 = list(showticklabels=TRUE, autorange = TRUE, title=“Y2”, ticks=“inside”, zeroline=FALSE) ,
paper_bgcolor = “#fff”,
margin=list(l=35,r=10,b=30,t=20)
)
P <- plotly_POST(p3,filename = “Line”, fileopt = “overwrite”);
url <- P$url

Each axis looks like

I have two issues:

  1. How do I have 1 distinct Screening / Visit value show up on the axis?
  2. If I can’t have 1, then how would I go about removing the dot (.) between the visit and the number?

I know it’s due to the x=interaction(…) but is there a cleaner way to do a multiple X axis in plotly using R? Do I have to convert from ggplot? The documentation is relatively sparse, Please don’t link the multiple Y axis example as that hasn’t helped.

Thank you thank you! :moneybag: