ggplot2 制作奇怪的、模糊的线条

ggplot2 making strange, smeared lines

我有以下代码可以使用 ggplot2 (here is the data file) 进行绘图:

sig1 <- ggplot(var_dat_df %>%
                filter(!(variable %in% c("LogDiffSq", "cusum_ker", "de_ker", "hr_ker"))),
               aes(x = i, y = -log10(value), group = variable, color = variable)) +
          geom_line() +
          scale_color_manual(values = c("#1b9e77", "#d95f02", "#7570b3"),
                             labels = c("CUSUM", "DE", "HR"),
                             name = "Statistic") +
          geom_hline(yintercept = -log10(0.05), color = "red", linetype = "dashed") +
          scale_y_continuous(breaks = c(-log10(0.05), 5, 10, 15, 17),
                             labels = expression(alpha, 5, 10, 15, 17)) +
          xlab("Index") + ylab(expression(-log[10](p))) +
          labs(title = "Statistical Significance of Detected Change",
               subtitle = "Without Using Kernel Estimation for Long-Run Variance") +
          theme_bw() +
          theme(plot.title = element_text(size = rel(2)),
                legend.position = "bottom")

出现以下错误信息:

Warning message:
In eval(expr, envir, enclos) : NaNs produced

这是结果图:

顶部的绿条是什么?它们为什么会出现,我该如何摆脱它们?

这是因为您对 log10 的输入值为零(或非常小)。你可以试试这个:

value_for_log0 <- NA # define value_for_log0 as the value you want to have as output of log10 when it's nearly 0 

ggplot(var_dat_df %>%
         filter(!(variable %in% c("LogDiffSq", "cusum_ker", "de_ker", "hr_ker"))),
       aes(x = i, y = ifelse(round(value, 15)==0, value_for_log0,-log10(value)), group = variable, color = variable)) +
  geom_line() +
  scale_color_manual(values = c("#1b9e77", "#d95f02", "#7570b3"),
                     labels = c("CUSUM", "DE", "HR"),
                     name = "Statistic") +
  geom_hline(yintercept = -log10(0.05), color = "red", linetype = "dashed") +
  scale_y_continuous(breaks = c(-log10(0.05), 5, 10, 15, 17),
                     labels = expression(alpha, 5, 10, 15, 17)) +
  xlab("Index") + ylab(expression(-log[10](p))) +
  labs(title = "Statistical Significance of Detected Change",
       subtitle = "Without Using Kernel Estimation for Long-Run Variance") +
  theme_bw() +
  theme(plot.title = element_text(size = rel(2)),
        legend.position = "bottom")