After changing the data from df () to ts (), I lost the column names and I have a problem with editing the tooltip in ggplotly, do you have any tips on how to take references and rename the tooltip and prevent the year from repeating itself?
data before the change enter image description here code
library(lubridate)
library(forecast)
library(plotly)
Pekin_3lata$date <- strptime(Pekin_3lata$date, "%Y-%m-%d" )
Pekin_3lata <- mutate(Pekin_3lata, Month = month(date))
# Year
Pekin_3lata <- mutate(Pekin_3lata, Year = year(date))
Pekin_3lata$date <- as.POSIXct(Pekin_3lata$date)
Pekin_3lata$Year <- as.numeric(as.character(Pekin_3lata$Year))
Pekin_3lata <- ts(Pekin_3lata["pm25"], start = c(2019, 1), end = c(2021, 12), frequency = 12)
data after the change enter image description here
creating a chart
season <- ggseasonplot(Pekin_3lata,
xlab("miesiąc"),
year.labels=FALSE,
season.labels = c("st", "lut", "mrz", "kw", "maj", "cz", "lip", "sier", "wrz", "paź", "lis", "gr"))
geom_point()
ggtitle("Zmienność zanieczyszczenia powietrza w Pekinie")
scale_color_brewer(name= "Rok", palette = "Set1")
gg<-ggplotly(season)
gg
And this is what the graph and tooltip look like, and I would like to see only the year and month and the data from pm25
CodePudding user response:
Losing the column names is due to replacing the data frame built with the first five lines of assignments with a time series object in the sixth line. You don't need the ts object to plot time series data. I replaced that code, staying in the data frame. The tool tip duplication I got around by specifying in the plotly call which tool tips to display. I referred to group rather than year. As you didn't supply data, I used the ggplot dataset "economics", and formatted as if it were your data. The time format in your data set may not match the economics format.
Hope this helps! :-)
library(plotly)
library(ggplot2)
library(dplyr)
library(lubridate)
library(forcats)
economics <-
economics |> mutate(month = as_factor(month(date)), year = as_factor(year(date))) |>
filter(year %in% (1970:1975))
level_key <-
c(
"1" = "st",
"2" = "lut",
"3" = "mrz",
"4" = "kw",
"5" = "maj",
"6" = "cz" ,
"7" = "lip",
"8" = "sier",
"9" = "wrz",
"10" = "paź",
"11" = "lis",
"12" = "gr"
)
economics <- economics |> mutate(month = recode(month, !!!level_key))
season <-
economics |> ggplot(aes(month, pce, group = year, colour = year))
geom_line()
geom_point()
scale_color_brewer(palette = "Set1")
labs(
colour = "Rok",
x = "miesiąc",
y = "pm25",
title = "Zmienność zanieczyszczenia powietrza w Pekinie"
)
ggplotly(season, tooltip = c("month", "pce", "group"))
CodePudding user response:
Now the plot looks like that: https://i.stack.imgur.com/TBw4v.png
here the data I use: https://docs.google.com/spreadsheets/d/1Y29Mrh4orHkF9pcbTugCjq5WJRMj4hRP/edit?usp=sharing&ouid=117828732736190351192&rtpof=true&sd=true [
And the code now:
Pekin_3lata$date <- strptime(Pekin_3lata$date, "%Y-%m-%d" )
Pekin_3lata <-
Pekin_3lata |> mutate(month = as_factor(month(date)), year = as_factor(year(date))) |>
filter(year %in% (2019:2021))
level_key <-
c(
"1" = "st",
"2" = "lut",
"3" = "mrz",
"4" = "kw",
"5" = "maj",
"6" = "cz" ,
"7" = "lip",
"8" = "sier",
"9" = "wrz",
"10" = "paź",
"11" = "lis",
"12" = "gr"
)
Pekin_3lata <- Pekin_3lata |> mutate(month = recode(month, !!!level_key))
season <-
Pekin_3lata |> ggplot(aes(month, pm25, group = year, colour = year))
geom_line()
#geom_point()
scale_color_brewer(palette = "Set1")
labs(
colour = "Rok",
x = "miesiąc",
y = "pm25",
title = "Zmienność zanieczyszczenia powietrza w Pekinie"
)
ggplotly(season, tooltip = c("month", "pm25", "group"))