gpt4 book ai didi

r - R 或 matlab 中的季节性箱线图

转载 作者:太空宇宙 更新时间:2023-11-03 20:23:21 25 4
gpt4 key购买 nike

enter image description here

DATE        obs1        obs2    obs3    
1981-01-01 2032.409 3142.46 1741.143
1981-01-02 2023.687 3870.04 1735.256
1981-01-03 2014.274 4126.25 1728.556
1981-01-04 2005.795 2615.91 1722.985
1981-01-05 2000.674 2940.83 1722.317
1981-01-06 1998.477 3258.69 1723.937
1981-01-07 1997.014 3371.6 1724.104
1981-01-08 1995.576 3184.13 1722.624
1981-01-09 1993.706 3540.76 1719.592
1981-01-10 1991.286 3312.43 1715.156
1981-01-11 1988.633 3028.65 1710.141
1981-01-12 1986.147 3212.79 1705.183
1981-01-13 1984.229 3193.23 1700.789
1981-01-14 1982.756 3294.52 1697.785
1981-01-15 1981.548 3553.78 1696.068
1981-01-16 1980.561 3492.28 1694.544
1981-01-17 1979.792 2452.09 1692.36
1981-01-18 1979.224 1873.82 1689.525
1981-01-19 1978.845 3218.28 1686.452

我需要在 R 中为每日数据绘制季节性(冬季、 Spring 、夏季和秋季)箱线图,如上所示。对于不同的电台,我有 10 年以上格式的数据。情节应该在一个图中,每个季节有多个箱线图。

最佳答案

使用 tidyverselubridate 的解决方案。 tidyverse 包括用于执行数据整理的 dplyrtidyr,以及用于创建绘图的 ggplot2lubridate 用于处理数据框中的日期。

由于您提供的数据集不是很有用,因为它仅包含 1 月份的几条记录,因此无法创建显示季节性差异的箱线图,因此我决定创建一个新的示例数据框。我的示例数据框的结构与您的数据集相似,它应该会为您提供一些提示,作为您解决实际问题的起点。

# Set the seed for reproducibility
set.seed(123)

library(tidyverse)
library(lubridate)

# Create example data frame
dt <- data_frame(DATE = seq(ymd("1980-01-01"), ymd("1989-12-31"), by = 1)) %>%
mutate(obs1 = rnorm(nrow(.), mean = 0, sd = 1),
obs2 = rnorm(nrow(.), mean = 1, sd = 2),
obs3 = rnorm(nrow(.), mean = 2, sd = 3))

head(dt)
# # A tibble: 6 x 4
# DATE obs1 obs2 obs3
# <date> <dbl> <dbl> <dbl>
# 1 1980-01-01 -0.56047565 0.7874145 2.7827006
# 2 1980-01-02 -0.23017749 0.1517417 8.5720252
# 3 1980-01-03 1.55870831 0.7193725 1.3293478
# 4 1980-01-04 0.07050839 0.5454177 0.3253155
# 5 1980-01-05 0.12928774 1.4101239 6.1245771
# 6 1980-01-06 1.71506499 -0.6491910 4.5034395

tail(dt)
# # A tibble: 6 x 4
# DATE obs1 obs2 obs3
# <date> <dbl> <dbl> <dbl>
# 1 1989-12-26 -0.3629796 0.6750946 0.8586325
# 2 1989-12-27 0.1102218 2.8572337 9.8541328
# 3 1989-12-28 -0.2700741 1.7614026 1.9109596
# 4 1989-12-29 0.6920973 0.5275611 -0.4756240
# 5 1989-12-30 0.9282803 1.3811225 1.5222535
# 6 1989-12-31 0.5931301 -1.6638739 4.1157087

示例数据框包含 10 年的记录和 3 个观察组。每列的值服从具有不同均值和标准差的正态分布。

第一步是通过将数据集从宽格式转换为长格式来处理数据框,并添加一个显示季节信息的列。

dt2 <- dt %>% 
# Convert data frame from lwide format to long format
gather(Observation, Value, -DATE) %>%
# Remove "obs" in the Observation column
mutate(Observation = str_replace(Observation, "obs", "")) %>%
# Convert the DATE column to date class
mutate(DATE = ymd(DATE)) %>%
# Create Month column
mutate(Month = month(DATE)) %>%
# Create Season column
mutate(Season = case_when(
Month %in% c(12, 1, 2) ~ "winter",
Month %in% c(3, 4, 5) ~ "spring",
Month %in% c(6, 7, 8) ~ "summer",
Month %in% c(9, 10, 11) ~ "fall",
TRUE ~ NA_character_
))

之后,我们可以使用 ggplot2 来创建箱线图。请注意,我使用 stat_summary 向每个组添加一条红线以表示平均值。

# Create a boxplot using ggplot2
# Specify the aesthetics
ggplot(dt2, aes(x = Season, y = Value, fill = Observation)) +
# Specify the geom to be boxplot
geom_boxplot() +
# Add a red line to the mean
stat_summary(aes(ymax = ..y.., ymin = ..y..),
fun.y = "mean",
geom = "errorbar", # Use geom_errorbar to add line as mean
color = "red",
width = 0.7,
position = position_dodge(width = 0.75), # Add the line to each group
show.legend = FALSE)

enter image description here

关于r - R 或 matlab 中的季节性箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47613708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com