As I said, the lines below that you provided worked well for me. Can you
explain what this line does?:
# reshape to long format
?? pivot_longer(-Dtime, names_to = "NO2") %>%
-------------------------------------------------
library(ggplot2)
library(dplyr)
library(tidyr)
b %>%
?? mutate(Dtime = paste(Sys.Date(), Dtime),
???????????????? Dtime = as.POSIXct(Dtime)) %>%
?? select(Dtime, DNO2, MNO2) %>%
?? # reshape to long format
?? pivot_longer(-Dtime, names_to = "NO2") %>%
?? # now plot
?? ggplot(aes(Dtime, value, color = NO2)) +
?? geom_line() +
?? geom_point() +
?? scale_color_manual(values = c("orange", "skyblue")) +
?? # make datetime labels
?? scale_x_datetime(date_breaks = "1 mins", date_labels =
"%H:%M") +
?? theme_bw()
[[alternative HTML version deleted]]
Yes, but if you want to learn R (or relearn R) it would be better for you to
decompress the code.
You know what b looks like so add the next step. If you want to be able to see
the original then save the output to another data frame.
New_df <- b %>%
mutate(Dtime = paste(Sys.Date(), Dtime),
Dtime = as.POSIXct(Dtime))
What do you have now?
Then add the next pipe %>% and code until the next %>%.
When you look at the dataframe after reshape() it will be very obvious, though
if you still have questions please ask.
Tim
-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of DFP
Sent: Wednesday, September 21, 2022 6:35 PM
To: Rui Barradas <ruipbarradas at sapo.pt>; r-help at r-project.org
Subject: Re: [R] Need help plotting
[External Email]
As I said, the lines below that you provided worked well for me. Can you explain
what this line does?:
# reshape to long format
pivot_longer(-Dtime, names_to = "NO2") %>%
-------------------------------------------------
library(ggplot2)
library(dplyr)
library(tidyr)
b %>%
mutate(Dtime = paste(Sys.Date(), Dtime),
Dtime = as.POSIXct(Dtime)) %>%
select(Dtime, DNO2, MNO2) %>%
# reshape to long format
pivot_longer(-Dtime, names_to = "NO2") %>%
# now plot
ggplot(aes(Dtime, value, color = NO2)) +
geom_line() +
geom_point() +
scale_color_manual(values = c("orange", "skyblue")) +
# make datetime labels
scale_x_datetime(date_breaks = "1 mins", date_labels =
"%H:%M") +
theme_bw()
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7C2e922af564854b594aae08da9c21a172%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637993965506049641%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=gnPZJIDTjVr8cv9s3foPjzYYUXWYX5Y18%2FLRKQpXyp4%3D&reserved=0
PLEASE do read the posting guide
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7C2e922af564854b594aae08da9c21a172%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637993965506049641%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=aLDvzRXPm3MSdulLOCbKDUSL59bO3RvhwAAkGl%2B%2BJ8M%3D&reserved=0
and provide commented, minimal, self-contained, reproducible code.
Hello, This type of problems generally has to do with reshaping the data. The format should be the long format and the data is in wide format. Inline. ?s 23:35 de 21/09/2022, DFP escreveu:> As I said, the lines below that you provided worked well for me. Can you > explain what this line does?: > > # reshape to long format > ?? pivot_longer(-Dtime, names_to = "NO2") %>% > > ------------------------------------------------- > > library(ggplot2) > library(dplyr) > library(tidyr) > > b %>% > ?? mutate(Dtime = paste(Sys.Date(), Dtime), > ???????????????? Dtime = as.POSIXct(Dtime)) %>% > ?? select(Dtime, DNO2, MNO2) %>% > ?? # reshape to long format > ?? pivot_longer(-Dtime, names_to = "NO2") %>%After selecting only the columns Dtime and *NO2 you need to reshape the data to long format. Some ways of telling pivot_longer what are the columns to become one are - explicitly writing all columns to be pivotted; - with negative indexing, all others will be put under the new name "NO2"; - with a selection function such as ends_with("NO2"). This is probably more readable code if you return to it some time from now. pivot_longer(ends_with("NO2"), names_to = "NO2") %>% With data in the long format map column NO2 to the color aesthetic and ggplot will automatically understand that you have 2 groups, DNO2 and MNO2, and plot 2 lines and 2 groups of points (one for each color) . Hope this helps, Rui Barradas> ?? # now plot > ?? ggplot(aes(Dtime, value, color = NO2)) + > ?? geom_line() + > ?? geom_point() + > ?? scale_color_manual(values = c("orange", "skyblue")) + > ?? # make datetime labels > ?? scale_x_datetime(date_breaks = "1 mins", date_labels = "%H:%M") + > ?? theme_bw() > >