Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204742

How to get the smooth line for monthly rainfall using ggplot?

$
0
0

I am trying to plot the monthly rainfall data from 1986 to 2016 using ggplot. My dataframe looks like this:

head(df)
  Year Month Station Rainfall   Remarks
1 1986   Jan    stn1      0.0  Observed
2 1986   Feb    stn1     10.4  Observed
3 1986   Mar    stn1     16.5  Estimated
4 1986   Apr    stn1     34.0  Observed
5 1986   May    stn1     27.0  Observed
6 1986   Jun    stn1    159.4  Observed

str(df)
'data.frame':   1488 obs. of  5 variables:
 $ Year    : chr  "1986""1986""1986""1986" ...
 $ Month   : Ord.factor w/ 12 levels "Jan"<"Feb"<"Mar"<..: 1 2 3 4 5 6 7 8 9 10 ...
 $ Station : Factor w/ 4 levels "stn1","stn2",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Rainfall: num  0 10.4 16.5 34 27 ...
 $ Remarks : Factor w/ 2 levels "Estimated","Observed": 2 2 1 2 2 2 2 2 2 2 ...

I tried the following code:

library(ggplot2)
ggplot(df, aes(x=Year, y=Rainfall, col=Station)) + geom_line()

However the above code results in vertical lines plot, while I want to have smooth varying lines.

I want to plot all the four station (stn1 to stn4) such that the color of each line be based on the df$Remarks. Also is it possible to have unique color for each station?

Your help would be appreciated


Viewing all articles
Browse latest Browse all 204742

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>