geom_smooth不会出现在散点图中

时间:2018-02-13 11:43:53

标签: r ggplot2

我正在尝试在geom_smooth散点图中绘制ggplot2行,但是当我添加该函数时它不会出现。

以下是我的数据框df的一个标记。

df <- structure(list(Kennisnamedatum = structure(c(17168, 17169, 17170, 
17171, 17172, 17173, 17174, 17175, 17176, 17177), class = "Date"), 
    misdrijven_per_dag = c(334L, 321L, 292L, 263L, 284L, 247L, 
    233L, 214L, 252L, 281L)), .Names = c("Kennisnamedatum", "misdrijven_per_dag"
), row.names = c(NA, -10L), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), vars = "Kennisnamedatum", drop = TRUE, indices = list(
    0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), group_sizes = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
    Kennisnamedatum = structure(c(17168, 17169, 17170, 17171, 
    17172, 17173, 17174, 17175, 17176, 17177), class = "Date")), row.names = c(NA, 
-10L), class = "data.frame", vars = "Kennisnamedatum", drop = TRUE, .Names = "Kennisnamedatum"))

我希望有人可以通过阅读我的情节代码告诉我自己做错了什么。

library(ggplot2)
library(ggiraph)

ggplot(df, aes(x = Kennisnamedatum, y = misdrijven_per_dag)) +
  geom_point_interactive(aes(tooltip = Kennisnamedatum,
                             data_id = Kennisnamedatum),
                         alpha = 0.6,
                         colour = "#607D8B" ) +
  geom_smooth() +
  scale_x_date(date_breaks = "1 month",
               date_labels = "%b")

我收到此警告消息:geom_smooth() using method = 'loess' and formula 'y ~ x'

secondary constructor

1 个答案:

答案 0 :(得分:1)

我不知道ggiraph,因此我没有使用geom_point_interactive进行测试。正如@Jimbou所提到的,如果我使用geom_point,我获得图表没有问题,但没有交互性。 我可以使用plotly::ggplotly为您提供一个选项,以使用您的数据添加交互性:

library(ggplot2)
library(plotly)
plot <- ggplot(df, aes(x = Kennisnamedatum, y = misdrijven_per_dag)) +
  geom_smooth() +
  geom_point(aes(text = Kennisnamedatum),
             alpha = 0.6,
             colour = "#607D8B") +
  scale_x_date(date_breaks = "1 day",
               date_labels = "%b %d")

ggplotly(plot, tooltip = "text")

请注意,我更改了x的比例,因此它会显示您提供的数据。