[英]What function can plot the distribution on the Y axis of line chart in R?
Please tell me in R, what function or package can plot the distribution of time series data on the Y axis of a line chart.
比如下图。
感谢您的任何帮助。
您可以使用ggplot2
和patchwork
来做到这一点。
首先加载库:
library(ggplot2)
library(patchwork)
现在让我们创建一些示例数据以供使用:
df <- airquality
df$Date <- seq(from = as.Date("1973-05-01"),
to = as.Date("1973-09-30"),
by = "1 day")
我们可以像这样制作密度 plot
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")
)
而主要的时间序列 plot 是这样的:
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.