precipitation histogram and line graph - r

I'm trying to make a monthly precipitation histogram in r using ggplot2 this is the data ideally columns 1 would be the x axis, column 2 would be histogram and column 3 would be line graph
Month MonthlyPrecipitation 30YearNormalPrecipitation
January 49.75 67.1
February 8.75 53.6
March 27 64.2
April 55.5 77.7
May 62.25 89.2
June 171.75 84.7
July 50.75 83.6
August 37.25 77.6
September 75.75 92.6
October 99.25 86.3
November 37.25 90.7
December 43.25 78.9

I've created fake data to recreate your example. Month a-l represents January through December. Here you first create a bar plot (I'm assuming that's what you meant by histogram. A true histogram of your data would be flat, given each item in your first variable is unique), then add the line graph. You have to include group = 1 at the end or it will return an error. And in case it wasn't clear, MP is my recreation of monthly precipitation, and NP is 30YearNormalPrecipitation.
set.seed(100)
Month <- c(letters[1:12])
MP <- rnorm(12, 50, 5)
NP <- rnorm(12, 80, 5)
df <- data.frame(Month, MP, NP)
ggplot(df, aes(x=Month, y = MP)) + geom_bar(stat = 'identity', alpha = 0.75) +
geom_line(aes(y = NP), colour="blue", group = 1)

Related

CREATE A TIME SERIES PLOT in r with ggplot

I have problems with coding of BIG DATA.
view(data)
Year
Month
Deaths
1998
1
200
1998
2
40
1998
3
185
1998
4
402
1998
5
20
1998
6
48
1998
7
290
1998
8
15
1998
9
252
1998
10
409
1998
11
233
1998
12
122
My data goes until 2014. I would like to create a time series. In the x-Axis only some years are available in 5 year step. In the y axis the deaths of all month during the 2000 years are shown. I don't know how can I code that?
I am not sure if it is right because I didn't have any data. I have this from a programming book
data$date = as.Date(paste(data$Year, data$Month,1), format = "%Y %m %d")
ggplot(data,
aes(
x = date,
y = Deaths,
)) +
geom_line() +
ggtitle("Time series") +
xlab("Year") +
ylab("Deaths")
Update if you want a month break, you can use
scale_x_date(date_breaks = "year", date_labels = "%Y", date_minor_breaks = "month")

How to plot monthly data having in the x-axis months and Years R studio

I have a dataframe where column 1 are Months, column 2 are Years and column 3 are precipitation values.
I want to plot the precipitation values for EACH month and EACH year.
My data goes from at January 1961 to February 2019.
¿How can I plot that?
Here is my data:
If I use this:
plot(YearAn,PPMensual,type="l",col="red",xlab="años", ylab="PP media anual")
I get this:
Which is wrong because it puts all the monthly values in every single year! What Im looking for is an x axis that looks like "JAN-1961, FEB1961....until FEB-2019"
It can be done easily using ggplot/tidyverse packages.
First lets load the the packages (ggplot is part of tidyverse) and create a sample data:
library(tidyverse)
set.seed(123)
df <- data.frame(month = rep(c(1:12), 2),
year = rep(c("1961", "1962"),
each = 12),
ppmensual = rnorm(24, 5, 2))
Now we can plot the data (df):
df %>%
ggplot(aes(month, ppmensual,
group = year,
color = year)) +
geom_line()
Using lubridate and ggplot2 but with no grouping:
Setup
library(lubridate) #for graphic
library(ggplot2) # for make_date()
df <- tibble(month = rep(month.name, 40),
year = rep(c(1961:2000), each = 12),
PP = runif(12*40) * runif(12*40) * 10) # PP data is random here
print(df, n = 20)
month year PP
<chr> <int> <dbl>
1 January 1961 5.42
2 February 1961 0.855
3 March 1961 5.89
4 April 1961 1.37
5 May 1961 0.0894
6 June 1961 2.63
7 July 1961 1.89
8 August 1961 0.148
9 September 1961 0.142
10 October 1961 3.49
11 November 1961 1.92
12 December 1961 1.51
13 January 1962 5.60
14 February 1962 1.69
15 March 1962 1.14
16 April 1962 1.81
17 May 1962 8.11
18 June 1962 0.879
19 July 1962 4.85
20 August 1962 6.96
# … with 460 more rows
Graph
df %>%
ggplot(aes(x = make_date(year, factor(month)), y = PP)) +
geom_line() +
xlab("años")

How do you graph two variables from a .csv file as two lines in ggplot2?

I have imported a .csv file and have plotted my data points on a line graph. But I am attempting to compare male and female life expectancies, but I can't figure out how to plot one line for males and one for females.
Here is an example of part of my data (ALE stands for average life expectancy).
Year Sex ALE
1900 Female 48.3
1900 Male 46.6
1901 Female 50.6
1901 Male 48
1902 Female 53.4
1902 Male 50.2
1903 Female 52
1903 Male 49.5
1904 Female 49.1
1904 Male 46.6
1905 Female 50.2
1905 Male 47.6
This is the code I have thus far. Ultimately I will put my work into a .rmd file.
library(ggplot2) # call up ggplot2
library(RColorBrewer) # call up rcolorbrewer palette
options(scipen = 999) # remove scientific notation
sex <- read.csv("~/Big Data IBP/Life expectancy_sex.csv") # data focusing on life expectancy comparing sex. #male v. female
# run test to see how year of death has changed over the years
ggplot(data = sex, aes(x = Year, y = ALE)) +
geom_line(linetype = "solid", col = "Blue", size = 0.5, arrow = arrow()) +
labs(
title = "Average Life Expectancy Based on Sex",
subtitle = "1900-2014", x = "Year", y = "Age at Death"
)
The issue is I want to have one line for males and one for females to compare the 2 lines on one graph. But the actual result I have is one line on the graph.
You need to map group, color and linetype to aes
library(ggplot2) # call up ggplot2
options(scipen = 999) # remove scientific notation
df <- read.table(text = "Year Sex ALE
1900 Female 48.3
1900 Male 46.6
1901 Female 50.6
1901 Male 48
1902 Female 53.4
1902 Male 50.2
1903 Female 52
1903 Male 49.5
1904 Female 49.1
1904 Male 46.6
1905 Female 50.2
1905 Male 47.6",
header = TRUE, stringsAsFactors = FALSE)
# run test to see how year of death has changed over the years
ggplot(data = df, aes(x = Year, y = ALE,
group = Sex, color = Sex, linetype = Sex)) +
geom_line(size = 0.5, arrow = arrow()) +
labs(
title = "Average Life Expectancy Based on Sex",
subtitle = "1900-2014", x = "Year", y = "Age at Death"
) +
scale_color_brewer(palette = "Dark2") +
theme_classic(base_size = 16)
Created on 2019-04-15 by the reprex package (v0.2.1)

how to plot a expenditure vs year in r

I have a dataset which has about 100,000 datapoints.
I want to plot two columns.
Y axis - Year
X axis - Sales
Sample Data:
Sales Year
22 2016
10 2016
3.99 2017
8.99 2017
12.99 2017
8.00 2016
12.00 2017
5.00 2016
22 2017
50 2016
53 2017
Im using the following code
plot(subset_4$SALES ~ subset_4$YEAR)
But the plot doesn't look great. Is there any nicer way of doing this?
Update: plot(subset_4$SALES ~ subset_4$WEEKS)
You can try ggplot2 library
df <- data.frame(sales, year)
ggplot(df, aes(x = sales, y = year, color = year)) +
geom_point() +
xlab("Sales") +
ylab("Year")

plot data with different dates

i have some trouble with the plots of my dataset.
This is an extract of my dataset.
Date Month Year Value 1
30/05/96 May 1996 1835
06/12/96 December 1996 1770
18/03/97 March 1997 1640
27/06/97 June 1997 1379
30/09/97 September 1997 1195
24/11/97 November 1997 1335
13/03/98 March 1998 1790
07/05/98 May 1998 349
14/07/98 July 1998 1179
27/10/98 October 1998 665
What I would like to do is a plot with Value 1 (y) against the mount (x) for every year. In other words, a plot with 3 lines that show the variation of Value 1 every month in th different years.
I do the following:
plot(x[Year==1996,4], xaxt="n")
par(new=T)
plot(x[Year==1997,4], xaxt="n")
axis(1, at=1:length(x$Month), labels=x$Month)
The problem is that the first value of 1996 refers to may, and the first value of 1997 refers to march. Due to that, the values plotted are mixed and don't correspond to their month anymore.
Is there a way to plot all these values in the same graph keeping the original correspondence of the data?
df <- read.table(text="Date Month Year Value1
30/05/96 May 1996 1835
06/12/96 December 1996 1770
18/03/97 March 1997 1640
27/06/97 June 1997 1379
30/09/97 September 1997 1195
24/11/97 November 1997 1335
13/03/98 March 1998 1790
07/05/98 May 1998 349
14/07/98 July 1998 1179
27/10/98 October 1998 665", header=T, as.is=T)
df$Month <- factor(df$Month, levels=month.name, ordered=T)
library(ggplot2)
ggplot(df) + geom_line(aes(Month, Value1, group=Year)) +
facet_grid(Year~.)
And a lattice alternative using #Michele df. I show here the 2 alternative (with and without faceting)
library(lattice)
library(gridExtra)
p1 <- xyplot(Value1~Month,groups=Year,data=df,
type=c('p','l'),auto.key=list(columns=3,lines=TRUE))
p2 <- xyplot(Value1~Month|Year,groups=Year,data=df,layout= c(1,3),
type=c('p','l'),auto.key=list(columns=3,lines=TRUE))
grid.arrange(p1,p2)
Create a numeric value for your months:
x$MonthNum <- sapply(x$Month, function(x) which(x==month.name))
Then plot using those numeric values, but label the axes with words.
plot(NA, xaxt="n", xlab="Month", xlim=c(0,13),
ylim=c(.96*min(x$Value),1.04*max(x$Value)), type="l")
z <- sapply(1996:1998, function(y) with(x[x$Year==y,], lines(MonthNum, Value1)))
axis(1, at=1:12, labels=month.name)
And some labels, if you want to identify years:
xlabpos <- tapply(x$MonthNum, x$Year, max)
ylabpos <- mapply(function(mon, year) x$Value1[x$MonthNum==mon & x$Year==year],
xlabpos, dimnames(xlabpos)[[1]])
text(x=xlabpos+.5, y=ylabpos, labels=dimnames(xlabpos)[[1]])
One could also obtain something similar to the ggplot example using layout:
par(mar=c(2,4,1,1))
layout(matrix(1:3))
z <- sapply(1996:1998, function(y) {
with(x[x$Year==y,], plot(Value1 ~ MonthNum, xaxt="n", xlab="Month", ylab=y,
xlim=c(0,13), ylim=c(.96*min(x$Value),1.04*max(x$Value)), type="l"))
axis(1, at=1:12, labels=month.name)
})

Resources