Antonio
Antonio

Reputation: 1111

Scatterplot by specific date in R

Could you help me to create a code to generate a scatter plot considering a certain date? I'm gonna explain:

I would like to make a scatter plot similar to the one I did in excel. That is, he is considering the DR sums, which is in yellow.

enter image description here

But as it has different dates, for example, it will be another chart, as you can see below, which I made for 02/08:

enter image description here

Is there any code to do this? Ie generate a scatter chart depending on date I choose?

I will inser the database below:

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))

Thanks!

Upvotes: 0

Views: 401

Answers (2)

mikebader
mikebader

Reputation: 1289

You may also wish to create a function that does what @Quixotic22 suggested. In this, you would specify the date in the function that will produce a scatter plot for the requested date:

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")

enter image description here

Upvotes: 1

Quixotic22
Quixotic22

Reputation: 2924

Here is the tidyverse option making use of pipes, lubridate and ggplot2. You have a fait bit of reading to do but this will give you an example

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

Upvotes: 1

Related Questions