r-geom_smooth()曲线未显示在绘图上

r-geom_smooth()曲线未显示在绘图上,r,ggplot2,linear-regression,R,Ggplot2,Linear Regression,为什么geom_平滑线没有显示在以下代码生成的绘图中 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 = nr

为什么geom_平滑线没有显示在以下代码生成的绘图中

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()

test。如果将重新订购零件包装为.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())

扩展数据和
ydata可能是数字的,但是
几何平滑
似乎无法识别您的
重新排序
函数输出。如果将重新订购零件包装为.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())

删除重新排序调用时会发生什么?@jdobres-曲线会回来!但我确实需要对我的实际代码进行重新排序(这只是一个示例)。你有什么建议?当你取消重新排序的调用时会发生什么?@jdobres-曲线回来了!但我确实需要对我的实际代码进行重新排序(这只是一个示例)。你有什么建议?