I use Highcharts and Shiny. I plotlines line to the x axis through the parameter plotlines . It is necessary to remove it, but I do not understand how to do it.

For JS there is a function RemovePlotLine("id") . Can I use it in R and how?

Code example:

  usg_first <- read.table(file= "USG - 1k.csv",header=TRUE,sep=";") usg_fifth <- read.table(file= "USG - 5k.csv",header=TRUE,sep=";") if(!require(shiny)) install.packages("shiny") if(!require(highcharter)) install.packages("highcharter") if(!require(dplyr)) install.packages("dplyr") if(!require(tidyr)) install.packages("tidyr") library(shiny) library(highcharter) library(dplyr) library(tidyr) Av_usg <- c( mean(usg_first$Average_thickness_nm), mean(usg_fifth$Average_thickness_nm)) Sigm <- c(sd(usg_first$Average_thickness_nm)/sqrt(length(usg_first$Average_thickness_nm)),sd(usg_fifth$Average_thickness_nm)/sqrt(length(usg_fifth$Average_thickness_nm))) LCL <- c((Av_usg[1] - 3 * Sigm[1]), (Av_usg[2] - 3 * Sigm[2])) UCL <- c((Av_usg[1] + 3 * Sigm[1]), (Av_usg[2] + 3 * Sigm[2])) i <- 1 ui <- fluidPage( titlePanel("Статистическое управление процессом"), sidebarLayout( 

Here I set the drop-down list, when switching which lines should be updated.

  # Лист с графиками selectInput("mat", label = "Пластина", choices = c("USG - 1k" = "first", "USG - 5k" = "fifth")), ), mainPanel( highchartOutput2("hcontainer2", height = "800px") ) ) ) # Создание сервера server = function(input, output) { # Настройка параметров графика output$hcontainer2 <- renderHighchart({ hc <- highchart() %>% hc_add_series(data = usg_first$Average_thickness_nm, type = input$plot_type, name = "Средняя толщина пластины, [нм]") %>% 

I want to delete this line on the Y axis:

  hc_yAxis(title = list(text ="Средняя толщина пластины, [нм]"), allowDecimals = TRUE, plotLines = list(list( value = Av_usg[1], color = 'green', id = 'avg', width = 3, label = list(text = "Cреднее значение толщин", style = list(color = 'black', fontWeight = 'bold')) ))) %>% hc_xAxis(visible = FALSE) %>% if (input$mat != "first") { material <- switch(input$mat, first = usg_first, fifth = usg_fifth ) if (input$mat == "first"){i = 1} else {i = 2} hc <- hc %>% hc_rm_series("Средняя толщина пластины, [нм]")%>% 

Here you need to insert, that I delete the line on the Y axis.

  hc$y$hc_opts$yAxis$plotLines[1]<- NULL hc_add_series(data = material$Average_thickness_nm, type = input$plot_type, name = "Средняя толщина пластины, [нм]", ShowInLegend = FALSE) } # Вывод графика hc }) } # Запуск shinyApp(ui = ui, server = server) 
  • Give a minimal sample code that reproduces the problem so that people who try to help you can reproduce the problem in themselves. The passages you cite only make it difficult to understand the essence of the question. - Artem Klevtsov

1 answer 1

The highchart object highchart inherited from the list class, therefore, accessing and modifying its structure is similar to working with a list. Here is an example of deleting a vertical line from the X axis. In the example below, the highchart object highchart saved to the variable p .

 p$x$hc_opts$xAxis$plotLines[1] <- NULL # Для оси X p$x$hc_opts$yAxis$plotLines[1] <- NULL # Для оси Y 

To analyze the changes that occur when adding lines on the axis, let's use an example from another answer.

 library(highcharter) data(diamonds, mpg, package = "ggplot2") hc <- hchart(mpg, "scatter", hcaes(x = displ, y = hwy, group = class)) %>% hc_xAxis(title = list(text = "Month in x Axis"), opposite = TRUE, plotLines = list( list(label = list(text = "This is a plotLine"), color = "#FF0000", width = 2, value = 2), list(label = list(text = "This is a plotLine"), color = "#FF0000", width = 2, value = 6))) %>% hc_yAxis(title = list(text = "Temperature in y Axis"), opposite = TRUE, minorTickInterval = "auto", minorGridLineDashStyle = "LongDashDotDot", showFirstLabel = FALSE, showLastLabel = FALSE, plotLines = list( list(label = list(text = "This is a plotBand"), color = "#FF0000", width = 2, value = 30))) 

Now consider where the lines are added:

 str(hc$x$hc_opts$xAxis$plotLines) #> List of 2 #> $ :List of 4 #> ..$ label:List of 1 #> .. ..$ text: chr "This is a plotLine" #> ..$ color: chr "#FF0000" #> ..$ width: num 2 #> ..$ value: num 2 #> $ :List of 4 #> ..$ label:List of 1 #> .. ..$ text: chr "This is a plotLine" #> ..$ color: chr "#FF0000" #> ..$ width: num 2 #> ..$ value: num 6 str(hc$x$hc_opts$yAxis$plotLines) #> List of 1 #> $ :List of 4 #> ..$ label:List of 1 #> .. ..$ text: chr "This is a plotBand" #> ..$ color: chr "#FF0000" #> ..$ width: num 2 #> ..$ value: num 30 

To remove lines, you need to remove the desired item from these lists as shown above in the answer.

  • Thank you very much! And what does p $ x $ mean at the beginning? - Peter Baranov
  • I saved the highchart object to the variable p . Further path in the structure of the object. - Artem Klevtsov
  • Unfortunately, I can’t remove the line, I’m using it as it should, but it still doesn’t work. Attached the sample code in the question - Peter Baranov
  • For the Y axis, the path will be hc$y$hc_opts$xAxis$plotLines . In the question did not see the code that deletes the line. - Artem Klevtsov
  • Added code to delete. Writes that he cannot find the function "%>% <-". I used to write that the allegedly wrong dimension: incorrect number of dimensions - Peter Baranov