R中特定日期的散点图
Scatterplot by specific date in R
你能帮我创建一个代码来生成考虑特定日期的散点图吗?我要解释一下:
我想绘制一个类似于我在 excel 中所做的散点图。也就是说,他正在考虑黄色的DR总和。
但是因为它有不同的日期,例如,它将是另一个图表,如下所示,我为 02/08 制作的:
有没有代码可以做到这一点?即根据我选择的日期生成散点图?
我将在下面插入数据库:
df <- structure(
list(date = c("2021-08-01","2021-08-01","2021-08-01","2021-08-01","2021-08-01",
"2021-08-02","2021-08-02","2021-08-02","2021-08-02","2021-08-02","2021-08-02"),
D1 = c(0,1,0,0,5,0,1,0,0,9,4), DR01 = c(2,1,0,0,3,0,1,0,1,7,2),
DR02 = c(2,0,0,0,4,2,1,0,1,4,2), DR03 = c(2,0,0,2,6,2,0,0,1,5,2),
DR04 = c(2,0,0,5,6,2,0,0,3,7,2), DR05 = c(2,0,0,5,6,2,0,0,7,7,2),
DR06 = c(2,0,0,5,7,2,0,0,7,7,1), DR07 = c(2,0,0,6,9,2,0,0,7,8,1)),
class = "data.frame", row.names = c(NA, -11L))
谢谢!
这是使用管道的 tidyverse
选项,lubridate
和 ggplot2
。
你有一点阅读要做,但这会给你一个例子
df %>%
mutate(
date = ymd(date) #convert date field to date type (currently string)
) %>%
#filter(date == ymd('2021-08-02')) %>% #decide if you want to filer or facet
gather( "key", "value", D1:DR07) %>% #convert from wide to long
group_by(date, key) %>%
summarise(value = sum(value, na.rm=T)) %>% #total the figures
ggplot(aes(x= key, y = value)) + #start of plot
geom_point() +
facet_grid(date ~ .) #optional - mini chart for each date - depends on how many dates you really have if loads you could do something like days ~ month etc
您可能还希望创建一个执行@Quixotic22 建议的功能。在此,您将在函数中指定日期,该日期将为请求的日期生成散点图:
library(dplyr)
library(tidyr)
library(ggplot2)
library(lubridate)
scatter_date <- function(dt, dta = df) {
dta %>%
mutate(date = ymd(date)) %>%
filter(date == ymd(dt)) %>%
summarize(across(starts_with("DR"), sum)) %>%
pivot_longer(everything(), names_pattern = "DR(.+)", values_to = "val") %>%
mutate(name = as.numeric(name)) %>%
ggplot(aes(x = name, y = val)) + geom_point()
}
scatter_date("2021-08-01")
你能帮我创建一个代码来生成考虑特定日期的散点图吗?我要解释一下:
我想绘制一个类似于我在 excel 中所做的散点图。也就是说,他正在考虑黄色的DR总和。
但是因为它有不同的日期,例如,它将是另一个图表,如下所示,我为 02/08 制作的:
有没有代码可以做到这一点?即根据我选择的日期生成散点图?
我将在下面插入数据库:
df <- structure(
list(date = c("2021-08-01","2021-08-01","2021-08-01","2021-08-01","2021-08-01",
"2021-08-02","2021-08-02","2021-08-02","2021-08-02","2021-08-02","2021-08-02"),
D1 = c(0,1,0,0,5,0,1,0,0,9,4), DR01 = c(2,1,0,0,3,0,1,0,1,7,2),
DR02 = c(2,0,0,0,4,2,1,0,1,4,2), DR03 = c(2,0,0,2,6,2,0,0,1,5,2),
DR04 = c(2,0,0,5,6,2,0,0,3,7,2), DR05 = c(2,0,0,5,6,2,0,0,7,7,2),
DR06 = c(2,0,0,5,7,2,0,0,7,7,1), DR07 = c(2,0,0,6,9,2,0,0,7,8,1)),
class = "data.frame", row.names = c(NA, -11L))
谢谢!
这是使用管道的 tidyverse
选项,lubridate
和 ggplot2
。
你有一点阅读要做,但这会给你一个例子
df %>%
mutate(
date = ymd(date) #convert date field to date type (currently string)
) %>%
#filter(date == ymd('2021-08-02')) %>% #decide if you want to filer or facet
gather( "key", "value", D1:DR07) %>% #convert from wide to long
group_by(date, key) %>%
summarise(value = sum(value, na.rm=T)) %>% #total the figures
ggplot(aes(x= key, y = value)) + #start of plot
geom_point() +
facet_grid(date ~ .) #optional - mini chart for each date - depends on how many dates you really have if loads you could do something like days ~ month etc
您可能还希望创建一个执行@Quixotic22 建议的功能。在此,您将在函数中指定日期,该日期将为请求的日期生成散点图:
library(dplyr)
library(tidyr)
library(ggplot2)
library(lubridate)
scatter_date <- function(dt, dta = df) {
dta %>%
mutate(date = ymd(date)) %>%
filter(date == ymd(dt)) %>%
summarize(across(starts_with("DR"), sum)) %>%
pivot_longer(everything(), names_pattern = "DR(.+)", values_to = "val") %>%
mutate(name = as.numeric(name)) %>%
ggplot(aes(x = name, y = val)) + geom_point()
}
scatter_date("2021-08-01")