How to create cumulative precipitation vs. temperature graph in a single plot - r

I have historical data for precip vs. annual temperature. I want to plot them into cool & wet, warm and wet, cool and dry, warm and dry years. Can someone help me with this?
Year Precip annual temperature
1987 821 8.5
1988 441 8
1989 574 7.9
1990 721 12.4
1991 669 10.8
1992 830 10
1993 1105 7.8
1994 772 8
1995 678 6.7
1996 834 8
1997 700 11
1998 786 11.2
1999 612 12
2000 758 10.6
2001 833 11
2002 622 10.6
2003 656 10.7
2004 799 9.9
2005 647 10.8
2006 764 12
2007 952 12.5
2008 943 10.86
2009 610 12.8
2010 766 11
2011 717 11.3
2012 602 9.5
2013 834 10.6
2014 758 11
2015 841 11
2016 630 11.5
2017 737 11.2
Average 742.32 10.36

As Majid suggested, you need to give more detail so you can get better answers. At least, try to use dput() with your dataframe, so we can get a reproducible copy of it. Copying and pasting into Excel is not appropriate for these kind of questions.
In any case, that graph can be easily be done using the ggplot2 package. You graph each year based on its X and Y coordinates and then manually add the lines and the titles for each category. You do need to establish the boundaries between cool/warm and dry/wet, of course.
library(ggplot2)
rain <- read.csv('~/data/rain.csv')
limit_humid <- 800
limit_warm <- 9.5
ggplot(rain, aes(x = temp, y = precip)) +
geom_text(aes(label = year)) +
geom_vline(xintercept = limit_warm) +
geom_hline(yintercept = limit_humid) +
annotate('text', label = 'bold("Cool and wet")', size = 4, parse = T,
x = min(rain$temp), y = max(rain$precip)) +
annotate('text', label = 'bold("Warm and wet")', size = 4, parse = T,
x = max(rain$temp), y = max(rain$precip)) +
annotate('text', label = 'bold("Cool and dry")', size = 4, parse = T,
x = min(rain$temp), y = min(rain$precip)) +
annotate('text', label = 'bold("Warm and wet")', size = 4, parse = T,
x = max(rain$temp), y = min(rain$precip)) +
theme_classic() +
labs(x = 'Average Temperature (°C)',
y = 'Cumulative precipitation (mm)')

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")

Minimize the height of a geom_bar plot

I have a bar plot cutting off the data labels because of height being
bigger that other data points. I want to adjust the height so that the
labels are visible.
season batsman total_runs
<int> <chr> <int>
1 2016 V Kohli 973
2 2018 KS Williamson 747
3 2012 CH Gayle 733
4 2013 MEK Hussey 733
5 2019 DA Warner 727
6 2014 RV Uthappa 660
7 2017 DA Warner 641
8 2010 SR Tendulkar 618
9 2008 SE Marsh 616
10 2011 CH Gayle 608
11 2009 ML Hayden 572
12 2015 DA Warner 562
I have tried ylim but does not work in my case.
season_top_scorer <- match_full%>%
group_by(season,batsman)%>%
summarize(total_runs = sum(batsman_runs))%>%
arrange(season,desc(total_runs))%>%
filter(total_runs == max(total_runs))%>%
arrange(desc(total_runs))%>%
ggplot(aes(x = season,y = total_runs,fill = batsman))+
geom_bar(stat ="identity")+
ggtitle("Highest run scorer each season")+
theme(axis.text.x = element_text(angle = 90, hjust = 1))+
scale_x_discrete(name="Season",
limits = c(2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019))+
geom_text(aes(label= total_runs,vjust= 0 ))+
scale_y_discrete(name = "Total Runs", limits = c(0,250,500,750,1000,1250))
The only problem is with season 2016. The height of bar is too big that
its cutting off the label.Any idea what might solve this problem in the
above code
You should use scale_y_continuous instead of scale_y_discrete.
scale_y_continuous(name = "Total Runs", breaks = c(0, 250, 500, 750, 1000, 1250))

Plot point and line graph in primary and secondary y-axis using ggplot in R

I have the following table. I need to plot "Area" in primary y-axis as points with "Weeks" in x-axis. For the same x-axis I need to plot "SM9_5" in secondary y-axis. I have my code below but does not plot it correct.
Any idea is appreciated.
Thanks.
YEAR Week Area SM9_5 sum percent COUNTY
2002 9-2 250 212.2 250 10.2 125
2002 10-1 300 450.2 550 22.5 125
2002 10-2 100 150.2 650 100.0 125
2002 9-3 50 212.2 250 10.2 15
2002 10-1 30 450.2 550 22.5 15
2002 10-2 10 150.2 650 100.0 15
2003 9-2 12 112.2 12 20.2 150
2003 10-1 15 350.2 27 82.5 150
2003 10-2 16 650.2 43 100.0 150
gg <- gg + geom_point(aes(y = Area, colour = "Area"))
gg <- gg + geom_line(aes(y = SM9_1, colour = "Sep_SM_9-1"))
gg <- gg + scale_y_continuous(sec.axis = sec_axis(~., name = "Soil Moisture"))
gg <- gg + scale_colour_manual(values = c("blue","red"))
gg <- gg + facet_wrap(~COUNTY, 2, scales = "fixed")
gg <- gg + labs(y = "Area",
x = "Weeks",
colour = "Parameter")
plot(gg)
My plot is shown below.

ggplot sub-plots with categorical and numeric in R

I have a the following table and I need to plot this to show (week in x-axis and percent in y-axis). MY following code plots nothing but gives me a message. Can someone help me to fix this?
Any help is appreciated.
dfx1:
Year State Cty Week ac_sum percent
1998 KS Coffey 10-1 79 6.4
1998 KS Coffey 10-3 764 62
1998 KS Coffey 10-4 951 77.2
1998 KS Coffey 10-5 1015 82.4
1998 KS Coffey 11-2 1231 100
1998 KS Crawford 10-3 79 6.1
1998 KS Crawford 10-4 764 15.8
1998 KS Crawford 10-5 951 84.1
1998 KS Crawford 11-2 1015 100
.
.
.
.
gg <- ggplot(dfx1, aes(Week,percent, col=Year))
gg <- gg + geom_line()
gg <- gg + facet_wrap(~Cty, 2, scales = "fixed")
gg <- gg + xlim(c(min(dfx1$Week), max(dfx1$Week)))
plot(gg)
geom_path: Each group consists of only one observation. Do you need to
adjust the group aesthetic?
Is this what you want?
dfx1 <- read.table(text="Year State Cty Week ac_sum percent
1998 KS Coffey 10-1 79 6.4
1998 KS Coffey 10-3 764 62
1998 KS Coffey 10-4 951 77.2
1998 KS Coffey 10-5 1015 82.4
1998 KS Coffey 11-2 1231 100
1998 KS Crawford 10-3 79 6.1
1998 KS Crawford 10-4 764 15.8
1998 KS Crawford 10-5 951 84.1
1998 KS Crawford 11-2 1015 100", header=T)
library(ggplot2)
ggplot(dfx1, aes(Week,percent, col=Year)) +
geom_point() +
facet_wrap(~Cty, 2, scales = "fixed")
ggplot(dfx1, aes(Week,percent, col=Year, group=1)) +
geom_point() + geom_line() +
facet_wrap(~Cty, 2, scales = "fixed")
You can look at other answers like this one to see that you're missing group = Year in your plot. Adding it in will give you what you are looking for:
library(ggplot2)
dfx1$Week <- factor(dfx1$Week, ordered = T)
ggplot(dfx1, aes(Week, percent, col = Year, group = Year)) +
geom_line() +
facet_wrap(~Cty, 2, scales = 'fixed')
With your last line it looks like you're wanting to only show the Weeks that actually have data. You can do that with scales = 'free', like so:
ggplot(dfx1, aes(Week, percent, col = Year, group = Year)) +
geom_line() +
facet_wrap(~Cty, 2, scales = 'free')

How to merge several data frames of equal format for plotting with ggplot in one digramm in R

I have several data frames (i.e t1, t2 and t3) of same format but maybe with different row lengths.
t1
year month avgTemp
2006 1 -0.95
2006 2 1.34
2006 3 3.58
2006 4 9.94
2006 5 14.67
2006 6 18.38
2006 7 23.56
2006 8 16.57
2006 9 18.08
2006 10 13.26
2006 11 8.27
2006 12 4.82
t2
year month avgTemp
2015 1 3.01
2015 2 2.16
2015 3 6.37
2015 4 10.31
2015 5 14.40
2015 6 17.84
2015 7 22.04
2015 8 21.35
2015 9 14.18
2015 10 9.40
2015 11 8.18
2015 12 7.22
and t3
year month avgTemp
2005 7 19.79
2005 8 17.54
2005 9 16.69
2005 10 11.64
2005 11 5.40
2005 12 1.97
Now, when I want to plot those 3 data frames in one diagramm I am doing this:
ggplot() +
geom_line(data=t1, aes(x = t1$month, y = t1$avgTemp, colour = t1$year)) +
geom_line(data=t2, aes(x = t2$month, y = t2$avgTemp, colour = t2$year)) +
geom_line(data=t3, aes(x = t3$month, y = t3$avgTemp, colour = t3$year))
And the output look like this
So far everything is ok, but the plot command is very ugly since I have to put every data frame into a new geom_line.
Is there a more elegant way to achieve this by merging the data frames or so?
Thanks in advance.
You can try something like this:
t <- rbind(t1, t2, t3)
t$year <- as.factor(t$year)
ggplot(t, aes(x = month, y = avgTemp, col = year)) + geom_line()
It should give you the desired plot with three lines for three years.
EDIT: Adding this code option based on the comment below about leaving year as a numeric value:
t <- rbind(t1, t2, t3)
ggplot(t, aes(x = month, y = avgTemp, col = year, group = year)) + geom_line()

Resources