You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly对数刻度折线图含零值线条消失的解决方法问询

Plotly含零值的对数刻度折线图处理及Y轴标签优化

问题背景

在Plotly中绘制包含零值的对数刻度折线图时,零值会导致线条中断消失。以下是问题复现代码:

library(tidyverse)
library(lubridate)
library(plotly)

df2 <- tibble::tribble(
  ~SAMPLE_DATE, ~REPORT_RESULT_VALUE,
  "2018-10-04",                 0.05,
  "2019-05-05",                 0.01,
  "2019-10-04",                    0,
  "2020-06-05",                 0.01,
  "2020-09-11",                    0,
  "2021-04-23",                    0,
  "2022-05-08",                 0.06 ) %>% 
  mutate(SAMPLE_DATE = ymd(SAMPLE_DATE))


plot_ly(data = df2) %>%
  add_trace(x = ~SAMPLE_DATE,
            y = ~REPORT_RESULT_VALUE,
            mode = "lines+markers") %>%
  layout(xaxis = list(title = 'Sample date'),
         yaxis = list(title = "Concentration (mg/L)",
                      type = "log"))

此前在Plotly相关论坛搜索到类似问题,但未找到有效解决方案。


多系列场景的Y轴标签优化需求

基于修改后的代码实现了多系列折线图,但希望优化Y轴标签的分布,使其更合理。示例代码如下:

df3 <- tibble::tribble(
  ~SAMPLE_DATE, ~REPORT_RESULT_VALUE, ~CHEMICAL_NAME,
  "2018-10-04",                 0.05, "a",
  "2019-05-05",                 0.01, "a",
  "2019-10-04",                    0, "a",
  "2020-06-05",                 0.01, "a",
  "2020-09-11",                    0, "a",
  "2021-04-23",                    0, "a",
  "2022-05-08",                 0.06, "a",
  "2018-10-04",                 95, "b",
  "2019-05-05",                 90, "b",
  "2019-10-04",                    80, "b",
  "2020-06-05",                 91, "b",
  "2020-09-11",                    90, "b",
  "2021-04-23",                    90, "b",
  "2022-05-08",                 96, "b",
  "2018-10-04",                 9.5, "c",
  "2019-05-05",                 9.0, "c",
  "2019-10-04",                    8.0, "c",
  "2020-06-05",                 9.1, "c",
  "2020-09-11",                    9.0, "c",
  "2021-04-23",                    9.0, "c",
  "2022-05-08",                 9.6, "c") %>% 
  mutate(SAMPLE_DATE = ymd(SAMPLE_DATE))

ggplotly(
  ggplot(df3, aes(SAMPLE_DATE, REPORT_RESULT_VALUE, colour = CHEMICAL_NAME)) +
    geom_line() +
    geom_point() +
    scale_y_continuous(trans = scales::pseudo_log_trans(sigma = 0.1),
                       breaks = scales::breaks_pretty(n = 10)) +
    labs(x = 'Sample date', y = "Concentration (mg/L)")
  )

当前使用scales::breaks_pretty(n = 10)设置Y轴刻度,但标签分布不够合理,需要优化。


内容的提问来源于stack exchange,提问作者william3031

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 03:15:46