An awful graph with R - r

I've been doing these awful graphs with R with a very basic code below
mydata %>%
mutate(week = week(date)) %>%
ggplot(aes(x = week))+
geom_freqpoly()
In the data there are recorded events, in the standard date format, in all four weeks of a month. But as you can see in the picture, the graph dives to the bottom in between of the weeks making it look awful. So how to make the graph go from one point to the other without this dive?
To reconstruct the data frame
structure(list(ID = c(82, 23, 81, 76, 56, 17, 11, 50, 69, 84),
pvm = structure(c(1295395200, 1295222400, 1295395200, 1295654400,
1294272000, 1294272000, 1293926400, 1294185600, 1294012800,
1295222400), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))

Not sure if this is something you're looking for but you can use geom_point and geom_line to produce a 'better graph'. I'm not sure what the data is meant to show and why you're using geom_freqpoly
Data <- structure(list(ID = c(82, 23, 81, 76, 56, 17, 11, 50, 69, 84),
pvm = structure(c(1295395200, 1295222400, 1295395200, 1295654400,
1294272000, 1294272000, 1293926400, 1294185600, 1294012800,
1295222400), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA,-10L), class = c("tbl_df", "tbl", "data.frame"))
ggplot(Data, aes(x=pvm, y=ID), group = 1)+
geom_point()+
geom_line()
Geom_point and Line graoh
New to this answering questions game but let me know if this isn't what you're looking for.

Related

I cannot figure out why my dates don't work

structure(list(Position = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), Date = structure(c(1685750400, 1685750400, 1685750400, 1685750400, 1685750400, 1685750400, 1685750400, 1685750400, 1685750400, 1685750400), tzone = "UTC", class = c("POSIXct", "POSIXt")), Time
= structure(c(-2209017523, -2209017518, -2209017513, -2209017508, -2209017503, -2209017498, -2209017493, -2209017488, -2209017483, -2209017478), tzone = "UTC", class = c("POSIXct", "POSIXt")), DateTime = structure(c(1685808077, 1685808082, 1685808087,
1685808092, 1685808097, 1685808102, 1685808107, 1685808112, 1685808117, 1685808122), tzone = "UTC", class = c("POSIXct", "POSIXt")), Temperatuur = c(21.2, 21.2, 21.6, 21.7, 22, 22.2, 20.1, 20.2, 20.3, 20.3), Treatment = c("Tempex", "Tempex", "Tempex",
"Tempex", "Tempex", "Tempex", "Tempex", "Tempex", "Tempex", "Tempex")), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))
My R code:
time.start=as.POSIXct("2023-06-03, 16:00",format="%H:%M")
time.end=as.POSIXct("2023-07-03, 07:30",format="%H:%M")
ggplot(logger, aes(DateTime, Temperatuur, color = Treatment))+
geom_line(size = 1)+
scale_x_datetime(limits = c(time.start, time.end),
breaks = date_breaks("24 hours"),
labels = date_format("%H:%M"))
I just cannot figure out why I am getting this: plot
Can someone please help me?
I tried to follow advice online, but that got me nowhere. Changing from 24 to 12 hours also didn't help. Maybe it's a problem in my excel file, but that all seems alright.
If we remove the commas from the time.* strings and fix the errant format=, it should work.
time.start <- as.POSIXct("2023-06-03 16:00")
time.end <- as.POSIXct("2023-07-03 07:30")
ggplot(logger, aes(DateTime, Temperatuur, color = Treatment)) +
geom_line(size = 1) +
scale_x_datetime(
limits = c(time.start, time.end),
breaks = date_breaks("24 hours"),
labels = date_format("%H:%M"))
I think perhaps you are confounding the use of labels=date_format("%H:%M") as what you think time.start/time.end should be (or how they should look), which is incorrect. Since the x-axis is (I'm inferring) a POSIXt object, then the time.* objects also must be the same object. You don't need to make them "look" (i.e., "%H:%M") the same as how you want the axis labels rendered, it is handled automatically by ggplot.

structure x-axis date as mm/dd/yy using lattice in R

I want the dates on the x-axis to appear as mm-dd-yy using lattice. Only every month needs to be shown ex: 10/1/2022, 11/1/2022 etc.
I think I may have to use format(pretty(df$date), "%d-%m-%y") based on another post but when I try I get an error:
Error in multiple && !outer : invalid 'x' type in 'x && y'
Here is my code:
library(lattice)
library(latticeExtra)
obj1 <- xyplot(Nconc + Sconc ~ date, df, type = "l", main="Title", lwd=2, col= c("red", "steelblue"))
obj2 <- xyplot(CountyCounts ~ date, df, type = "l", lwd=2, col="green")
p <- doubleYscale(obj1, obj2, use.style=FALSE, add.ylab2 = TRUE)
p
Here is a sample of my data:
structure(list(date = structure(c(1634688000, 1634774400, 1634860800,
1635033600, 1635120000, 1635206400, 1635379200, 1635465600, 1635638400,
1635811200, 1635897600, 1636070400, 1636416000, 1636502400, 1636588800,
1636675200, 1636934400, 1637020800, 1637107200, 1637280000, 1637366400,
1637625600, 1637712000, 1637971200, 1638057600, 1638144000, 1638230400,
1638316800, 1638403200), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Nconc = c(2.63e-05, 2.4e-05, 1.83e-05, 1.47e-05, 2.11e-05,
2.09e-05, 3.99e-05, 5.22e-05, 9.17e-06, 2.89e-05, 4.79e-05,
1.9e-05, 3.12e-05, 1.81e-05, 5.79e-06, 4.49e-05, 7.61e-06,
1.51e-05, 3.61e-05, 1.46e-05, 1.16e-05, 3.53e-05, 0.000232,
3.09e-05, 1.31e-06, 1.24e-05, 4.99e-05, 7.76e-05, 0.00016
), Sconc = c(2.98e-05, 2.79e-05, 1.75e-05, 2.2e-05, 2.62e-05,
2.56e-05, 4.27e-05, 5.6e-05, 1.26e-05, 3.92e-05, 6.11e-05,
1.42e-05, 2.53e-05, 2.12e-05, 4.31e-06, 4.57e-05, 9.83e-06,
1.07e-05, 3.65e-05, 2.51e-05, 1.23e-05, 2.63e-05, 0.000212,
3.99e-05, 1.42e-06, 1.08e-05, 4.09e-05, 7.89e-05, 0.000113
), CountyCounts = c(46, 45, 47, 17, 49, 49, 29, 38, 19, 20,
30, 25, 38, 24, 19, 27, 27, 30, 39, 17, 11, 23, 21, 19, 21,
43, 45, 42, 28)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -29L))
You could add
scales = list(x = list(format = "%d-%m-%y"))
to the call creating obj1 to get the format you want. But to control the locations exactly, you will need to specify them via at, e.g.,
scales = list(x = list(at = seq(as.POSIXct("2021-11-01"), by = "1 month", length.out = 5),
format = "%d-%m-%y", at = ))
You could try playing with tick.number = 5 (default) to control the locations, and you might get lucky, but there are no guarantees that you will get exactly 1 tick per month that way.

Randomize one column values based on multiple other columns

I have the following df:
structure(list(Donorcode = c("406A001", "406A002", "406A003",
"406A004"), Doos = c(1, 1, 2, 2), `Leeftijd T0` = c(70, 73, 79,
75), Instituut = c("Spaarne ziekenhuis", "Spaarne ziekenhuis",
"Spaarne ziekenhuis", "Spaarne ziekenhuis"), Datum = structure(c(1567468800,
1567468800, 1567468800, 1567468800), class = c("POSIXct", "POSIXt"
), tzone = "UTC")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-4L))
I need to randomize the column 'Donorcode' based on the other 4 columns, not one column 'weighs' more than the other so the order of which column randomizes the Donorcode column first does not matter.
Is there a way to do this in R?
Many thanks!

How make two row names in the Rshiny?

I have a dataset, which has a common feature - at the end of column names (after comma) is written the group to which a specific column corresponds to. Is it possible to create a table where two row column names will be used? In an example, the first row is Up and goes Quantity, Price, Quality. Is it also possible to somehow separate by empty column/ or some bold border these 3 groups (Up, Down and Total)? I know there is DT library that helps to make it easier, however, I am looking for the solution using shiny library only.
I also found that using tags$style() and CSS can help to solve it, however not familiar with CSS.
library(shiny)
df <- structure(list(Year = c(2022L, 2022L, 2022L, 2022L, 2022L),
Week = c(0, 1, 2, 3, 4),
`Quantity, Up` = c(335, 305, 740, 910, 515),
`Price, Up` = c(1, 2, 5, 5, 3),
`Quality, Up` = c(243, 243, 243, 12321.434052, 1706.327462),
`Quantity, Down` = c(-175, -900, -205, -35, 0),
`Price, Down` = c(243, 243, 1219.717851, 902.819827, 0),
`Quality, Down` = c(2834.205418, 243, -1219.717851, 902.819827, 0),
`Quantity, Total` = c(510, 1205, 945, 945, 515),
`Price, Total` = c(431, 32, 423, 342, 243),
`Quality, Total` = c(24, 4, -2, 42, 1706.327462)),
class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -5L),
groups = structure(list(Year = 2022L, .rows = structure(list(1:5), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))),
class = c("tbl_df", "tbl", "data.frame" ), row.names = c(NA, -1L), .drop = TRUE))
ui <- fluidPage(
# Application title
titlePanel("aFRR"),
# plot graphs
mainPanel(tabsetPanel(
tabPanel("Up",
h3(helpText("aFRR Price and Quantity")),
tableOutput("table_up"))
)
)
)
server <- function(input, output, session) {
output$table_up <- renderTable(df, na = "missing",align = 'c',striped = TRUE
)
}
shinyApp(ui, server)

how to plot multiple daily time series in one plot

I have time series data of 27 days (from 2018-04-09 to 2018-5-15 without weekends) with 7 observations per day (08:00 t0 20:00 every two hours) with two variables per observation (di and eu).
I want to plot all days as line plots in one plot.
I found solutions to plot one plot per day with a ggplot facet plot and I found solutions to plot the whole timeseries in one plot (di and eu from 2018-04-09 to 2018-05-15).
But nothing that let me overlay 27 daily plots for one variable in one 8:00 to 20:00 plot.
The first three days as example data with dput():
structure(list(date_time = structure(c(1523260800, 1523268000,
1523275200, 1523282400, 1523289600, 1523296800, 1523304000, 1523347200,
1523354400, 1523361600, 1523368800, 1523376000, 1523383200, 1523390400,
1523433600, 1523440800, 1523448000, 1523455200, 1523462400, 1523469600,
1523476800), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
di = c(75, 90, 35, 70, 75, 15, 5, 65, 55, 15, 15, 0, NA,
15, 55, 55, 5, 25, NA, 60, NA), eu = c(15, 0, 65, 30, 15,
65, 70, 40, 45, 75, 75, 100, NA, 85, 45, 30, 90, 65, NA,
20, NA)), row.names = c(NA, -21L), class = c("tbl_df", "tbl",
"data.frame"))
A plot with all 27 days in one plot may look confusing, but I like to try it, to see wether it makes a trend in the data obvious. A plot for each weekday would be a nice addition.
You could determine the day and hour up front and then plot with respective groups like this:
library(tidyverse)
library(lubridate)
df %>%
gather(metric, value, -date_time) %>%
mutate(
hour_of_day = hour(date_time),
day = day(date_time)
) %>%
ggplot(aes(x = hour_of_day, y = value)) +
geom_line(aes(group = day)) +
facet_wrap( ~ metric)

Resources