I am looking at data from Nov to April and would like to have a plot starting from Nov to April. Below is my sample code to screen out month of interests.
library(tidyverse)
mydata = data.frame(seq(as.Date("2010-01-01"), to=as.Date("2011-12-31"),by="days"), A = runif(730,10,50))
colnames(mydata) = c("Date", "A")
DF = mydata %>%
mutate(Year = year(Date), Month = month(Date), Day = day(Date)) %>%
filter(Month == 11 | Month == 12 | Month == 01 | Month == 02 | Month == 03 | Month == 04)
I tried to re-order the data starting at month 11 followed by month 12 and then month 01,02,03,and,04. I used the code factor(Month, levels = c(11,12,01,02,03,04)) along with the code above but it didn't work.
I wanted a plot that starts at month Nov and ends on April. The following code gave me attached plot
ggplot(data = DF, aes(Month,A))+
geom_bar(stat = "identity")+ facet_wrap(~Year, ncol = 2)
Right now, the plot is starting at January all the way to December- I dont want this. I want the plot starting at November, and all the way to April. I tried to label the plot using scale_x_date(labels = date_format("%b", date_breaks = "month", name = "Month") which didn't work. Any help would
I converted Month to character before applying factor() and it worked.
DF = mydata %>%
mutate(Year = year(Date), Month = month(Date), Day = day(Date)) %>%
filter(Month %in% c(11, 12, 1, 2, 3, 4)) %>%
mutate(Month = sprintf("%02d", Month)) %>%
mutate(Month = factor(Month, levels = c("11","12","01","02","03","04")))
ggplot(data = DF, aes(Month,A))+
geom_bar(stat = "identity")+ facet_wrap(~Year, ncol = 2)
Output:
user2332849 answer is close but does introduce an error. The bar are not in the correct order. For example for 2010, it plot is showing November and December's data prior to the beginning of the year's data. In order to plot in the proper order the year will need adjustment so that the calendar starts on month 11 and goes to month 4.
#Convert month to Factor and set desired order
DF$Month<- factor(DF$Month, levels=c(11, 12, 1, 2, 3, 4))
#Adjust the year to match the year of the beginning of series
#For example assign Jan, Feb, Mar and April to prior year
DF$Year<-ifelse(as.integer(as.character(DF$Month)) <6, DF$Year-1, DF$Year)
#plot
ggplot(data = DF, aes(Month,A))+
geom_bar(stat = "identity") +
facet_wrap(~Year, ncol = 3)
In the plot below the first 4 months of 2010 is shifted to become the last 4 periods of the prior year. And the last 2 months of 2011 is ready for the first 4 months of 2012.
Related
I am trying to compare different years' variables but I am having trouble plotting them together.
The time series is a temperature series which can be found in https://github.com/gonzalodqa/timeseries as temp.csv
I would like to plot something like the image but I find it difficult to subset the months between the years and then combine the lines in the same plot under the same months
If someone can give some advice or point me in the right direction I would really appreciate it
You can try this way.
The first chart shows all the available temperatures, the second chart is aggregated by month.
In the first chart, we force the same year so that ggplot will plot them aligned, but we separate the lines by colour.
For the second one, we just use month as x variable and year as colour variable.
Note that:
with scale_x_datetime we can hide the year so that no one can see that we forced the year 2020 to every observation
with scale_x_continous we can show the name of the months instead of the numbers
[just try to run the charts with and without scale_x_... to understand what I'm talking about]
month.abb is a useful default variable for months names.
# read data
df <- readr::read_csv2("https://raw.githubusercontent.com/gonzalodqa/timeseries/main/temp.csv")
# libraries
library(ggplot2)
library(dplyr)
# line chart by datetime
df %>%
# make datetime: force unique year
mutate(datetime = lubridate::make_datetime(2020, month, day, hour, minute, second)) %>%
ggplot() +
geom_line(aes(x = datetime, y = T42, colour = factor(year))) +
scale_x_datetime(breaks = lubridate::make_datetime(2020,1:12), labels = month.abb) +
labs(title = "Temperature by Datetime", colour = "Year")
# line chart by month
df %>%
# average by year-month
group_by(year, month) %>%
summarise(T42 = mean(T42, na.rm = TRUE), .groups = "drop") %>%
ggplot() +
geom_line(aes(x = month, y = T42, colour = factor(year))) +
scale_x_continuous(breaks = 1:12, labels = month.abb, minor_breaks = NULL) +
labs(title = "Average Temperature by Month", colour = "Year")
In case you want your chart to start from July, you can use this code instead:
months_order <- c(7:12,1:6)
# line chart by month
df %>%
# average by year-month
group_by(year, month) %>%
summarise(T42 = mean(T42, na.rm = TRUE), .groups = "drop") %>%
# create new groups starting from each July
group_by(neworder = cumsum(month == 7)) %>%
# keep only complete years
filter(n() == 12) %>%
# give new names to groups
mutate(years = paste(unique(year), collapse = " / ")) %>%
ungroup() %>%
# reorder months
mutate(month = factor(month, levels = months_order, labels = month.abb[months_order], ordered = TRUE)) %>%
# plot
ggplot() +
geom_line(aes(x = month, y = T42, colour = years, group = years)) +
labs(title = "Average Temperature by Month", colour = "Year")
EDIT
To have something similar to the first plot but starting from July, you could use the following code:
# libraries
library(ggplot2)
library(dplyr)
library(lubridate)
# custom months order
months_order <- c(7:12,1:6)
# fake dates for plot
# note: choose 4 to include 29 Feb which exist only in leap years
dates <- make_datetime(c(rep(3,6), rep(4,6)), months_order)
# line chart by datetime
df %>%
# create date time
mutate(datetime = make_datetime(year, month, day, hour, minute, second)) %>%
# filter years of interest
filter(datetime >= make_datetime(2018,7), datetime < make_datetime(2020,7)) %>%
# create increasing group after each july
group_by(year, month) %>%
mutate(dummy = month(datetime) == 7 & datetime == min(datetime)) %>%
ungroup() %>%
mutate(dummy = cumsum(dummy)) %>%
# force unique years and create custom name
group_by(dummy) %>%
mutate(datetime = datetime - years(year - 4) - years(month>=7),
years = paste(unique(year), collapse = " / ")) %>%
ungroup() %>%
# plot
ggplot() +
geom_line(aes(x = datetime, y = T42, colour = years)) +
scale_x_datetime(breaks = dates, labels = month.abb[months_order]) +
labs(title = "Temperature by Datetime", colour = "Year")
To order month differently and sum up the values in couples of years, you've to work a bit with your data before plotting them:
library(dplyr) # work data
library(ggplot2) # plots
library(lubridate) # date
library(readr) # fetch data
# your data
df <- read_csv2("https://raw.githubusercontent.com/gonzalodqa/timeseries/main/temp.csv")
df %>%
mutate(date = make_date(year, month,day)) %>%
# reorder month
group_by(month_2 = factor(as.character(month(date, label = T, locale = Sys.setlocale("LC_TIME", "English"))),
levels = c('Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun')),
# group years as you like
year_2 = ifelse( year(date) %in% (2018:2019), '2018/2019', '2020/2021')) %>%
# you can put whatever aggregation function you need
summarise(val = mean(T42, na.rm = T)) %>%
# plot it!
ggplot(aes(x = month_2, y = val, color = year_2, group = year_2)) +
geom_line() +
ylab('T42') +
xlab('month') +
theme_light()
A slightly different solution without the all dates to 2020 trick.
library(tidyverse)
library(lubridate)
df <- read_csv2("https://raw.githubusercontent.com/gonzalodqa/timeseries/main/temp.csv")
df <- df |>
filter(year %in% c(2018, 2019, 2020)) %>%
mutate(year = factor(year),
month = ifelse(month<10, paste0(0,month), month),
day = paste0(0, day),
month_day = paste0(month, "-", day))
df |> ggplot(aes(x=month_day, y=T42, group=year, col=year)) +
geom_line() +
scale_x_discrete(breaks = c("01-01", "02-01", "03-01", "04-01", "05-01", "06-01", "07-01", "08-01", "09-01", "10-01", "11-01", "12-01"))
I have a monthly temporal series with sales in this format (so there's no month or year column):
ts(data = Datos, start = c(2015,1), end = c(2020,12), frequency = 12)
How can I plot a multi-boxplot by month?
If you want to use the boxplots to display the variations for a specific month across the given five years period you can try:
library(tidyverse)
library(tsibble)
ts(data = sample(100), start = c(2015,1), end = c(2020,12), frequency = 12) %>%
as_tsibble() %>%
mutate(month = as.factor(month(index))) %>%
ggplot(aes(month, value)) +
geom_boxplot()
I have crime data of the years 2018-2020. Each row represents one crime. For the sake of this example let's assume that there are two variables crimetype (e.g. theft, robbery) and date (when the crime was committed).
Some sample data:
data <- data.frame(date= sample(seq(as.Date('2018/01/01'), as.Date('2020/12/31'), by="day"),10000, replace=T),
crimetype = sample(c("A", "B", "C"), 100000, replace=T))
My goal is to create a lineplot for, let's say, type "A" crimes. On the x-axis there should be the date (from january 1st to december 31st), on the y-axis there should be the number of crimes per day. However, as I want the three lines (one for each year) to be shown on top of each other, so that I can compare them, there should be no year on the x-axis. Or it should not be displayed at least.
^ . . . . . .
| . . .
| . . .
n | . 2018
| - - -
| - - - - - - - - 2019
| = = =
| = = = = = = = = 2020
|
------------------------------------->
Jan-1 Dec-31
I was trying to create a new date-variable with all the dates in the same year (here 2020).
data <- data %>% mutate(daymonth = substr(date, 5, length(date)),
date_new = as.Date(paste("2020", daymonth, sep="")),
daymonth = NULL)
Is there a better way to do this and how can I plot the graph?
data_plot <- data %>% filter(crimetype == 'A')
ggplot(data = data_plot, aes(x = date_new, y = ?, color=format(date, "%Y")) + geom_line()
For working with dates have a look at the lubridate package which I use here for extracting the year. Also you can get rid of the year by using format(date, "%d-%m"). The following approach is a bit of a hack. To use a date axis but still get rid of the year I set the year for all dates to 2018. The question of which variable to plot ... simply count the obs to get the number of crimes by date. Finally. I set the breaks of the date axis to 1 month. Adjust this as you like. Try this:
library(ggplot2)
library(dplyr)
library(lubridate)
data <- data.frame(date= sample(seq(as.Date('2018/01/01'), as.Date('2020/12/31'), by="day"),10000, replace=T),
crimetype = sample(c("A", "B", "C"), 100000, replace=T))
data_plot <- data %>%
mutate(
year = lubridate::year(date),
year = factor(year),
# A hack. Set year to 2018. Allows me to use a date axis
date_foo = as.Date(paste(2018, format(date, "%m-%d"), sep = "-"))) %>%
filter(crimetype == 'A') %>%
count(date, date_foo, year, crimetype)
ggplot(data = data_plot, aes(x = date_foo, y = n, color = year, group = year)) +
geom_line() +
scale_x_date(date_breaks = "1 month", date_labels = "%d-%m")
#> Warning: Removed 1 row(s) containing missing values (geom_path).
Created on 2020-03-28 by the reprex package (v0.3.0)
I made a dataframe with columns year,month,temp,upper and lower
upper and lower are the max temperature by year and lower is the minimum
I have two questions:
first is why for some values in the end of dataframe the upper and lower are not correctly computed but in the rest of the dataframe they are fine?
And why am I getting weird axes when I am using ggplot
the dataframe is this
as you can see upper and lower for 2017 is wrong
Year Month Temp upper lower
1 1880 Jan -.29 -.29 -.09
2 1880 Feb -.18 -.29 -.09
3 1880 Mar -.11 -.29 -.09
......
1655 2017 Nov .84 .96 1.12
1656 2017 Dec .88 .96 1.12
the code is:
newDF <- df %>%
group_by(Year) %>%
mutate(upper = max(Temp), # identify max value for month day
lower = min(Temp) # identify min value for month day
) %>%
ungroup()
p <- ggplot(newDF, aes(Month, Temp)) +
geom_linerange(newDF, mapping=aes(x=Year, ymin=lower, ymax=upper), colour = "wheat2", alpha=.1)
print(p)
the graph seems fine but the axis are messed up
I think you're very close -- it's just the second part that needs a tweak. ggplot can work with a date field as the x axis, but the Month field is text (and it doesn't include the Year). Here I make a new column called date that combines them. lubridate is a handy package for that, since it does some smart parsing of date formats.
# Fake data
library(dplyr)
df <- data_frame(
Year = rep(1880:2017, each = 12),
Month = rep(month.abb, times = (2017-1880+1)),
Temp = rnorm(n = 1656, mean = 0, sd = 1)
)
newDF = df %>%
# This line adds a date field based on Year and Month
mutate(date = lubridate::ymd(paste(Year, Month, 1))) %>%
group_by(Year) %>%
mutate(upper = max(Temp), # identify max value for month day
lower = min(Temp), # identify min value for month day
) %>%
ungroup()
library(ggplot2)
p <- ggplot(newDF, aes(date, Temp)) +
geom_linerange(newDF, mapping=aes(x=Year, ymin=lower, ymax=upper), colour = "wheat2", alpha=.1)
print(p)
I have a data frame with data for max 2 years period on different objects:
ISBN Date Quantity
3457 2004-06-15 10
3457 2004-08-16 6
3457 2004-08-19 10
3457 2005-04-19 7
3457 2005-04-20 12
9885 2013-01-15 10
9885 2013-03-16 6
9855 2013-08-19 10
9885 2014-09-19 7
9885 2014-09-20 12
How can I plot Jan to Dec for the 1st year, continued by Jan to Dec for the 2nd year?
I guess the idea is to normalize the years (to have 1st, 2nd), but not the months. (here's an example)
Number of Items Sold over 2 Years Period Since Release
I'd use the lubridate package for something like this. Note I am calling for dataframe df because you didn't give it a name.
So for example:
library(lubridate)
First format the date like so:
df$Date <- ymd(df$Date)
Then extract the month and the year:
df$Month <- month(df$Date, label=TRUE, abbr=TRUE)
df$Year <- year(df$Date)
From there you can plot your results with ggplot2:
library(ggplot2)
ggplot(df, aes(x=Month, y=Quantity, colour=Year)) +
geom_point()
Note your question could be asked better here as you haven't provided a reproducible example.
You could try:
data <- df %>%
group_by(ISBN) %>%
arrange(Date) %>%
mutate(Year = year(Date),
Month = month(Date, label = TRUE),
Rank = paste(sapply(cumsum(Year != lag(Year,default=0)), toOrdinal), "Year")) %>%
group_by(Rank, Month, add = TRUE) %>%
summarise(Sum = sum(Quantity))
ggplot(data = data, aes(x = Month, y = Sum,
group = factor(ISBN),
colour = factor(ISBN))) +
geom_line(stat = "identity") +
facet_grid(. ~ Rank) +
scale_colour_discrete(name = "ISBN") +
theme(panel.margin = unit(0, "lines"),
axis.text.x = element_text(angle = 90))
Aussming the following df:
df <- data.frame(
ISBN = sample(c(3457, 9885), 1000, replace = TRUE),
Date = sample(seq(as.Date('2004/01/01'),
as.Date('2011/12/31'), by = "month"),
1000, replace = TRUE),
Quantity = sample(1:12, 1000, replace = TRUE)
)
This would produce: