How to add X label, Y label, z label in R-plotly contour plot

Viewed 371

I have created a contour plot using R-plotly. Unfortunately, I am unable to add title of the plot, x axis title and, y axis title on the contour plot. I would also like to incorporate the Z labels (location name/sample ID) inside the plot. can any body help me to add those? My dataframe contains x=latitude, y=longitude, chla=Z, which is collected form 6 locations. My code is below

install.packages ("plotly") 
library(plotly)
df<-read.csv("contour_Jan20.csv",head=T)
df
fig <- plot_ly(df,x= df$Lat, y=df$Long, z=df$Chl,  
 z = ~volcano, type = "contour", contours = list(showlabels = TRUE)
)
fig <- fig %>% colorbar(title = "Concentration \n in mg/L")
fig
2 Answers

Here's an example using mtcars -

library(plotly)

fig <- plot_ly(mtcars,x= ~mpg, y=~disp, z=~am, type = "contour", 
               contours = list(showlabels = TRUE))

fig <- fig %>% 
        colorbar(title = "Concentration \n in mg/L") %>%
        layout(title = 'Title of the plot', plot_bgcolor = "#e5ecf6")
fig

fig <- plot_ly(mtcars,x= ~mpg, y=~disp, z=~am, type = "contour", 
               contours = list(showlabels = TRUE))
t <- list(
  family = "Courier New",
  size = 14,
  color = "blue")
t1 <- list(
  family = "Times New Roman",
  color = "red"
)
t2 <- list(
  family = "Courier New",
  size = 14,
  color = "green")
t3 <- list(family = 'Arial')

fig <- fig %>% 
  colorbar(title = "legend title", font =t2) %>%
  layout(title = 'Plot title', font =t1,
         plot_bgcolor = "#e5ecf6",xaxis = list(title = "X title", font = t3), 
yaxis = list(title = "y title", font = t3))
fig

Related