【问题标题】:What function can plot the distribution on the Y axis of line chart in R?什么函数可以在R中绘制折线图Y轴上的分布?
【发布时间】:2022-01-31 17:20:31
【问题描述】:

请告诉我在R中,什么函数或包可以在折线图的Y轴上绘制时间序列数据的分布。

如下图。

感谢您的帮助。

【问题讨论】:

  • 您可以将 y 变量传递给hist(),它将创建一个严格的值直方图,而不考虑时间。
  • @sconfluentus 不幸的是,hist 不能用于绘制变量的密度和趋势。这就是 OP 的要求。
  • 我没有看到他们想要两者,只是他们想要密度......

标签: r plot distribution


【解决方案1】:

您可以使用ggplot2patchwork 执行此操作。

从加载库开始:

library(ggplot2)
library(patchwork)

现在让我们创建一些示例数据以供使用:

df <- airquality
df$Date <- seq(from = as.Date("1973-05-01"), 
               to   = as.Date("1973-09-30"), 
               by   = "1 day")

我们可以这样制作密度图

p1 <- ggplot(df, aes(y = Temp)) +
  geom_density(fill = "blue", alpha = 0.2, color = "blue") +
  theme_classic() +
  theme(axis.ticks.x = element_line(color = "white"),
        axis.text.x = element_text(color = "white"),
        axis.title.x = element_text(color = "white"),
        axis.line.x = element_line(color = "white")
        )

主要的时间序列图是这样的:

p2 <- ggplot(df, aes(Date, Temp)) +
  geom_line(color = "blue") +
  theme_classic() +
  theme(panel.background = element_rect(fill = "#fef8d6"),
        axis.ticks.length.y = unit(0, "pt"),
        axis.text.y = element_blank(),
        axis.title.y = element_blank())

要绘制它们,我们可以这样做:

p1 + p2 + plot_layout(widths = c(1, 8))

【讨论】:

  • 衷心感谢您对我的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-01
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
相关资源
最近更新 更多