Problem running asdetect package in r with original age data - r

I am trying to run the asdetect package on my time series data. The age data for my time series data starts at -65 years and goes to 13800 years. The dt function by default counts number of rows instead of the actual age. When I customize the interval between tick marks on the x-axis (dt) based on the number of data points divided by the maximum age, it causes the plot to start at 0, which is not accurate. Is there a way to change the x-axis labeling from a time-based scale to the actual age data?
This is what I tried:
detect1 <- asdetect::as_detect(charcoal$influx_area_mm2xcm2xyr, dt=0.5)
or
detect1 <- asdetect::as_detect(charcoal$influx_area_mm2xcm2xyr, dt=38.4)
plot(detect1, type="l", xlab='Time', ylab='Detection Value', ylim=c())

Related

Separating time series of boxplots

I need help to create a time series of boxplots in R. On three separate graphs would work.
I have data which is separated by Treatment (A to D), Wetland (Ann,... Twin (total 9)), and by Time (10, 20, 90).
Example: data (Chl_a_ug_L) is from Wetland Ann, Treatment B and at Time 90.
I am trying to graph the data (Chl_a_ug_L) on the y axis and treatment on the x axis so that each Treatment has 9 boxplots with all locations.
The issue is that the data (Chl_a_ug_L) also needs to be sorted by date collected. I need to separate out the data collected on Time 10, Time 20 and Time 90 to create three separate graphs.
I have:
ggplot(AlgaeData, aes(x=Treatment, y=Chl_a_ug_L, fill=Wetland)) +
geom_boxplot()
This creates the graph I need but groups all Time data into one graph, instead of separating it.

Plot every year as line with months on Xaxis and variable on Y-axis from NetCDF

I have netcdf data with lat,lon,time as dimensions and temperature temp as variable. It has daily temperature data for 10 years.
For single location I can plot time series. But how to plot for every year, Year as hue and Months on Xaxis and temp on Y axis. So i want 10 lines as 10 years on my graph. Every line is an year which represents 12 monthly means or daily data. example is here.
And if possible please tell how to add mean and median of all the years as seperate line among these 10 yearly line plots. example picture image example
I'm tempted to agree with the comment that it would be good to show a little more effort in terms of what you've tried. It would also be good to mention what you've read (in e.g. the xarray documentation: https://xarray.pydata.org/en/stable/), which I believe has many of the components you need.
I'll start by setting up some mock data, like you mention, with four years of daily (random) data.
time = pd.date_range("2000-01-01", "2004-12-31")
base = xr.DataArray(
data=np.ones((time.size, 3, 2)),
dims=("time", "lat", "lon"),
coords={
"time": time,
"lat": [1, 2, 3],
"lon": [0.5, 1.5],
},
)
To make the data a bit more comparable with your example, I'm going to add yearly seasonality (based on day of year), and make every year increase by 0.1.
seasonality = xr.DataArray(
data=np.sin((time.dayofyear / 365.0) * (2 * np.pi)),
coords={"time": time},
dims=["time"],
)
trend = xr.DataArray(
data=(time.year - 2000) * 0.1,
coords={"time": time},
dims=["time"],
)
da = base + seasonality + trend
(You can obviously skip these two parts, in your case, you'd only do an xarray.open_dataset() or xarray.open_dataarray`)
I don't think your example is grouped by month: it's too smooth. So I'm going to group by day of year instead.
Let's start by getting a single locations, then using the dt accessor:
https://xarray.pydata.org/en/stable/time-series.html#datetime-components
In this case, it's also most convenient to store the data as a DataFrame, since it essentially becomes a table (month of dayofyear as the rows, separate years etc as columns). First we select one location, and calculate the minimum and maximum values and store them in a pandas DataFrame:
location = da.isel(lat=0, lon=0)
dataframe = location.groupby(da["time"].dt.dayofyear).min().drop(["lat", "lon"]).to_dataframe(name="min")
dataframe["max"] = location.groupby(da["time"].dt.dayofyear).max().values
Next, grab the year by year data, and add it to the DataFrame:
for year, yearda in location.groupby(location["time"].dt.year):
dataframe[year] = pd.Series(index=yearda["time"].dt.dayofyear, data=yearda.values)
If you want monthly values, add another groupby step:
for year, yearda in location.groupby(location["time"].dt.year):
monthly_mean = yearda.groupby(yearda["time"].dt.month).mean()
dataframe[year] = pd.Series(index=monthly_mean["month"], data=monthly_mean.values)
Note that by turning the data into a pandas Series first, it can add the values appriopriately, based on the values of the index (dayofyear here), even though we don't have 366 values for every year.
Next, plot it:
dataframe.plot()
It will automatically assign hue based on the columns.
(My minimum and maximum coincide with 2000 and 2004 due to the way I setup the mock data, ... you get the idea.)
In terms of styling, options, etc., you might like seaborn better:
https://seaborn.pydata.org/index.html
import seaborn as sns
sns.plot(data=dataframe)
If you want to use different styling, different kind of plots (e.g. the colored zones your example has), you'll have to combine different plot, e.g. as follows:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.fill_between(x=dataframe.index, y1=dataframe["min"], y2=dataframe["max"], alpha=0.5, color="orange")
dataframe.plot(ax=ax)
Note that seaborn, pandas, xarray, etc. all use matplotlib behind the scenes. Many of the plotting functions also accept an ax argument, to draw on top of an existing plot.

how to plot the monthly average temperature over the entire length of the data set

I am a beginner of R.
The question is that you have this data set ready plot the monthly average temperature over the entire length of the data set. That is, on the x-axis plot the months (numbered 1, 2, 3…. 12, 13, 14,…), while on the y-axis plot the monthly averages.
I already have the data set like this
my attempt is
plot(Temp$month,Temp$averagetemp)
the result is
I wonder how to change the code.
You can create a new variable that counts by months:
Temp$month_addative <- Temp$month + (Temp$year-min(Temp$year))*12
plot(Temp$month_additive,Temp$averagetemp)
Assuming 2003 is your lowest year, this will add 12 for every year past 2003 to the month number, creating a cumulative count of months.

How to plot binary data together with continuous data in time series with ggplot2?

I have several data sets containing binary and continuous data respectively.
The data sets includes the datetime for the given observation.
The time step in the datetime column is not the same, so I cannot merge the datasets.
(So far I kept the two datasets apart, especially because the timestep in each dataset is irregular it itself.)
The binary data is in lower frequency than the continous data
Important: I transformed the time to POSIXct format in order to get around the irregular timesteps in the data
I would like to plot the two datasets in one time series plot with ggplot2.
The binary data (0's and 1's) should shade the continuous curve with rectangular surfaces going from y=-Inf to y=Inf.
Does it make sense?
My question: How do I do that?
How to I create a legend and control the colors of the plot?
So far I have the binary data in one plot using geom_step
and the continous data in another plot
I tried multiplot, but it does not seem to work.
The dream situation is, to put multiple plots of different data on top of each other as layers using the POSIXct time as reference somehow!
Not sure I can give some reproducible code..
This is how I transform the time column to POSIXct format:
D$Time <- strptime(D$Time, format="%Y/%m/%d %H:%M:%S")
This is the plot with two binary data sets using geom_step:
ggplot() +
geom_step(data=E, aes(x=Time, y=Set, group=1, col="high window")) +
geom_step(data=D, aes(x=Time, y=Set, group=1)) +
scale_x_datetime(limits=c(as.POSIXct('0015-01-07 08:00:00'), as.POSIXct('0015-01-07 10:00:00'))) +
scale_y_continuous(breaks=seq(0, 1, 1))
I am currently trying to plot the plot above together with a third dataset which is continuous, which means I need another y-axis if I should continue with geom_step...

Plotting multiple frequency polygon lines using ggplot2

I have a dataset with records that have two variables: "time" which are id's of decades, and "latitude" which are geographic latitudes. I have 7 time periods (numbered from 26 to 32).
I want to visualize a potential shift in latitude through time. So what I need ggplot2 to do, is to plot a graph with latitude on the x-axis and the count of records at a certain latitude on the y-axis. I need it do this for the seperate time periods and plot everything in 1 graph.
I understood that I need the function freqpoly from ggplot2, and I got this so far:
qplot(latitude, data = lat_data, geom = "freqpoly", binwidth = 0.25)
This gives me the correct graph of the data, ignoring the time. But how can I implement the time? I tried subsetting the data, but I can't really figure out if this is the best way..
So basically I'm trying to get a graph with 7 lines showing the frequency distribution in each decade in order to look for a latitude shift.
Thanks!!
Without sample data it is hard to answer but try to add color=factor(time) (where time is name of your column with time periods). This will draw lines for each time period in different color.
qplot(latitude, data = lat_data, geom = "freqpoly", binwidth = 0.25,
color=factor(time))

Resources