ggplot2制作奇怪的,涂抹的线条

时间:2016-12-02 06:32:47

标签: r plot ggplot2 statistics nan

我有以下代码使用 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

以下是结果图:

plot

顶部的绿色条纹是什么?它们为什么会出现,我怎样才能摆脱它们呢?

1 个答案:

答案 0 :(得分:3)

这是因为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")

enter image description here