r - кривая geom_smooth () не отображается на графике - PullRequest
0 голосов
/ 08 декабря 2018

Почему линия geom_smooth не отображается на графике, сгенерированном с помощью следующего кода?

test <- function() {
  require(ggplot2)
  # browser()
  set.seed(1);

  df <- data.frame(matrix(NA_real_, nrow = 50, ncol = 2))
  colnames(df) <- c("xdata", "ydata")

  df$xdata = as.numeric(sample(1:100, size = nrow(df), replace = FALSE))
  df$ydata = as.numeric(sample(1:3, size = nrow(df), prob=c(.60, .25, .15), replace = TRUE))

  plot1 <- ggplot(df, aes(x = reorder(xdata,-ydata), y = ydata)) + 
    geom_point(color="black") + 
    geom_smooth(method = "loess") + 
    theme(legend.position = "none", axis.text.x = element_blank(), axis.ticks.x = element_blank() )
  plot1
}
test()

Мои данные x и y определенно являются числовыми, как рекомендуется в этом вопросе: geom_smooth в ggplot2не работает / показывает

Участок:

enter image description here

1 Ответ

0 голосов
/ 08 декабря 2018

xdata и ydata могут быть числовыми, но geom_smooth, похоже, не распознает вывод вашей функции reorder как таковой.Если вы оберните as.numeric вокруг части повторного заказа, строка вернется:

ggplot(df, aes(x = as.numeric(reorder(xdata,-ydata)), y = ydata)) + 
  geom_point(color="black") + 
  geom_smooth(method = "loess") + 
  theme(legend.position = "none", axis.text.x = element_blank(), 
        axis.ticks.x = element_blank())

reorder wrapped in as numeric

...