Plotly: Annotate outliers with sample names in boxplot

后端 未结 4 1851
滥情空心
滥情空心 2021-01-06 16:09

I am trying to create a boxplot with ggplot and plotly with the dataset airquality where Month is on the x-axis and Ozone values are o

4条回答
  •  没有蜡笔的小新
    2021-01-06 16:17

    We can almost get it like this:

    library(ggplot2)
    library(plotly)
    library(datasets)
    data(airquality)
    # add months
    airquality$Month <- factor(airquality$Month,
                               labels = c("May", "Jun", "Jul", "Aug", "Sep"))
    # add sample names
    airquality$Sample <- paste0('Sample_',seq(1:nrow(airquality)))
    # boxplot
    gg <- ggplot(airquality, aes(x = Month, y = Ozone)) +
      geom_boxplot()
    ggly <- ggplotly(gg)
    # add hover info
    hoverinfo <- with(airquality, paste0("sample: ", Sample, "

    ", "month: ", Month, "
    ", "ozone: ", Ozone)) ggly$x$data[[1]]$text <- hoverinfo ggly$x$data[[1]]$hoverinfo <- c("text", "boxes") ggly

    Unfortunately, the hovering does not work for the first box plot...

提交回复
热议问题