Plotting time series with different length - data-visualization

I have two time series (ts-variables) with different time length. It’s yearly data and they are stored as separate ts.objects. The first series starts in 1936 and the other starts in 1943 and both ends in 2012. Problem: I cannot find the R-script (commands) for plotting these series in one, single, nice figure which includes all observations. Great if someone can help.
Regards

If the data are on a comparable scale then you just need to set the x-axis limits to the range of the entire data, e.g.
set.seed(2) ## reproducible
dat1 <- data.frame(Year = seq(1936, 2012, by = 1), Y = runif(77))
dat2 <- data.frame(Year = seq(1943, 2012, by = 1), Y = runif(70))
ylim <- range(dat1$Y, dat2$Y)
xlim <- range(dat1$Year, dat2$Year)
plot(Y ~ Year, data = dat1, type = "l", col = "red", xlim = xlim,
ylim = ylim)
lines(Y ~ Year, data = dat2, type = "l", col = "blue")
I'll leave you to prettify the plot.
Just noticed you said ts variables, so the following will work too
ts1 <- ts(runif(77), start = 1936, freq = 1)
ts2 <- ts(runif(70), start = 1943, freq = 1)
xlim <- c(1936, 2012)
ylim <- range(ts1, ts2)
plot(ts1, ylim = ylim, xlim = xlim, col = "red")
lines(ts2, col = "blue")
With your data, replace runif(n) with the real data for the two time series.

Related

R: How to plot the last row of a dataframe?

This must be very easy, but I cannot get a plot of the last/any row of a dataframe.
A = data.frame(a = rnorm(50), b = rnorm(50), c = rnorm(50))
barplot(A[nrow(A),1:3])
I get the error message:
Error in barplot.default(A[nrow(A), 1:3]) :
'height' must be a vector or a matrix
A solution using ggplot would be very welcome!
imported ggplot2 library and the dataset you gave me. used the tail command to get only the last row. Then had to melt() the data to get it into the right format, then plotted in ggplot2
library(ggplot2)
library(reshap2)
A = data.frame(a = rnorm(50), b = rnorm(50), c = rnorm(50))
A_tail <- tail(A, 1)
tailmelt <- melt(A_tail)
ggplot(data = tailmelt, aes( x = factor(variable), y = value, fill = variable ) ) +
geom_bar( stat = 'identity' )

Why is my y value not displayed in the time series (bar plot)?

So I've created a time series event. I'd like to plot my NS data on the y axes. Unfortunately it's not working and I can't figure out why. I'd like to plot it as a bar plot. The x value input should be my newly created DateTime. Any notes?
library(ggplot2)
library(scales)
Versuch3 <- data.frame(date = c("1.1.2015", "2.1.2015", "3.1.2015"),
time = c("06:12:03", "08:23:45", "15:40:32"),
NS = c("0.3", "0.4", "0.6"),
status = c("0", "1", "0"))
Versuch3$DateTime <- paste(Versuch3$date,Versuch3$time,sep =" ")
Versuch3$DateTime <- as.POSIXct(strptime(Versuch3$DateTime, "%d.%m.%Y %H:%M:%S"))
Versuch3$NS <- as.integer(Versuch3$NS)
dtLimits <- c(as.POSIXct("2014-12-01 00:00:00"), as.POSIXct("2015-07-01 00:00:00"))
#plot
ggplot(data = Versuch3, aes(x=DateTime, y=NS)) +
geom_bar(stat="identity", aes(fill=status), width = 3) +
scale_x_datetime(date_breaks = "1 month",
labels = date_format("%d.%m.%Y %H:%M:%S"),
limits = dtLimits) +
theme(axis.text.x = element_text(angle = 90, vjust = .5))

Plot two columns of data with different number of data points

Hi, I have two columns of data. They are over the same time period but column one generates data every 1000ms, and column 2 generates data every 500ms. How can i plot them on the same graph looking of equal length. The x-axis doesnt have to be "Time". Thank you.
plt.rcParams['figure.figsize'] = [40,20]
x = df['Time']
y1 = df['Engine RPM']
y2 = df['FMS RPM']
plt.plot(x,y1,color='r', label='column1',linewidth=2)
plt.plot(x,y2,color='b', label='column2',linewidth=2)
I can have both lines looking equal using the following code, but on seperate graphs.
x = np.linspace(0, 100,100)
x2 = np.linspace(0,200,200)
f, ((ax1, ax2)) = plt.subplots(2)
y1 = df['Engine RPM']
y2 = df1['FMS RPM']
ax1.plot(x,y1, label = 'column1')
ax2.plot(x2,y2, label = 'column2')
Try this:
x = np.linspace(0, 100,100)
x2 = np.linspace(0,200,200)
f, ax = plt.subplots(1,1)
ax2 = ax1.twiny()
ax.plot(x,y1,color='r', label='column1',linewidth=2)
ax2.plot(x,y2,color='b', label='column2',linewidth=2)

geom_vline() dateRangeInput()

I have set up a line graph in shiny. The x axis has dates covering 2014 to current date.
I have set up various vertical lines using geom_vline() to highlight points in the data.
I'm using dateRangeInput() so the user can choose the start/end date range to look at on the graph.
One of my vertical lines is in Feb 2014. If the user uses the dateRangeInput() to look at dates from say Jan 2016 the vertical line for Feb 2014 is still showing on the graph. This is also causing the x axis to go from 2014 even though the data line goes from Jan 2016 to current date.
Is there a way to stop this vertical line showing on the graph when it's outside of the dataRangeInput()? Maybe there's an argument in geom_vline() to deal with this?
library(shiny)
library(tidyr)
library(dplyr)
library(ggplot2)
d <- seq(as.Date("2014-01-01"),Sys.Date(),by="day")
df <- data.frame(date = d , number = seq(1,length(d),by=1))
lines <- data.frame(x = as.Date(c("2014-02-07","2017-10-31", "2017-08-01")),
y = c(2500,5000,7500),
lbl = c("label 1", "label 2", "label 3"))
#UI
ui <- fluidPage(
#date range select:
dateRangeInput(inputId = "date", label = "choose date range",
start = min(df$date), end = max(df$date),
min = min(df$date), max = max(df$date)),
#graph:
plotOutput("line")
)
#SERVER:
server <- function(input, output) {
data <- reactive({ subset(df, date >= input$date[1] & date <= input$date[2])
})
#graph:
output$line <- renderPlot({
my_graph <- ggplot(data(), aes(date, number )) + geom_line() +
geom_vline(data = lines, aes(xintercept = x, color = factor(x) )) +
geom_label(data = lines, aes(x = x, y = y,
label = lbl, colour = factor(x),
fontface = "bold" )) +
scale_color_manual(values=c("#CC0000", "#6699FF", "#99FF66")) +
guides(colour = "none", size = "none")
return(my_graph)
})
}
shinyApp(ui = ui, server = server)
As mentioned by Aimée in a different thread:
In a nutshell, ggplot2 will always plot all of the data that you provide and the axis limits are based on that unless you specify otherwise. So because you are telling it to plot the line & label, they will appear on the plot even though the rest of the data doesn't extend that far.
You can resolve this by telling ggplot2 what you want the limits of your x axis to be, using the coord_cartesian function.
# Set the upper and lower limit for the x axis
dateRange <- c(input$date[1], input$date[2])
my_graph <- ggplot(df, aes(date, number)) + geom_line() +
geom_vline(data = lines, aes(xintercept = x, color = factor(x) )) +
geom_label(data = lines, aes(x = x, y = y,
label = lbl, colour = factor(x),
fontface = "bold" )) +
scale_color_manual(values=c("#CC0000", "#6699FF", "#99FF66")) +
guides(colour = "none", size = "none") +
coord_cartesian(xlim = dateRange)

How to plot 4-D data embedded in a dataframe in Julia using a subplots approach?

I have a Julia DataFrame where the first 4 columns are dimensions and the 5th one contains the actual data.
I would like to plot it using a subplots approach where the two main plot axis concern the first two dimensions and each subplot then is a contour plot over the remaining two dimensions.
I am almost there with the above code:
using DataFrames,Plots
# plotlyjs() # doesn't work with plotlyjs backend
pyplot()
X = [1,2,3,4]
Y = [0.1,0.15,0.2]
I = [2,4,6,8,10,12,14]
J = [10,20,30,40,50,60]
df = DataFrame(X=Int64[], Y=Float64[], I=Float64[], J=Float64[], V=Float64[] )
[push!(df,[x,y,i,j,(5*x+20*y+2)*(0.2*i^2+0.5*j^2+3*i*j+2*i^2*j+1)]) for x in X, y in Y, i in I, j in J]
minvalue = minimum(df[:V])
maxvalue = maximum(df[:V])
function toDict(df, dimCols, valueCol)
toReturn = Dict()
for r in eachrow(df)
keyValues = []
[push!(keyValues,r[d]) for d in dimCols]
toReturn[(keyValues...)] = r[valueCol]
end
return toReturn
end
dict = toDict(df, [:X,:Y,:I,:J], :V )
M = [dict[(x,y,i,j)] for j in J, i in I, y in Y, x in X ]
yL = length(Y)
xL = length(X)
plot(contour(M[:,:,3,1], ylabel="y = $(string(Y[3]))", zlims=(minvalue,maxvalue)), contour(M[:,:,3,2]), contour(M[:,:,3,3]), contour(M[:,:,3,4]),
contour(M[:,:,2,1], ylabel="y = $(string(Y[2]))", zlims=(minvalue,maxvalue)), contour(M[:,:,2,2]), contour(M[:,:,2,3]), contour(M[:,:,2,4]),
contour(M[:,:,1,1], ylabel="y = $(string(Y[1]))", xlabel="x = $(string(X[1]))"), contour(M[:,:,1,2], xlabel="x = $(string(X[2]))"), contour(M[:,:,1,3], xlabel="x = $(string(X[3]))"), contour(M[:,:,3,4], xlabel="x = $(string(X[4]))"),
layout=(yL,xL) )
This produces:
I remain however with the following concerns:
How do I automatize the creation of each subplot in the subplot call ? Do I need to write a macro ?
I would like each subplot to have the same limits in the z axis, but zlims seems not to work. Is zlims not yet supported ?
How do I hide the legend on the z axis on each subplot and plot it instead apart (best would be on the right side of the main/total plot) ?
EDIT:
For the first point I don't need a macro, I can create the subplots in a for loop, add them in a array and pass the array to the plot() call using the ellipsis operator:
plots = []
for y in length(Y):-1:1
for x in 1:length(X)
xlabel = y == 1 ? "x = $(string(X[x]))" : ""
ylabel = x==1 ? "y = $(string(Y[y]))" : ""
println("$y - $x")
plot = contour(I,J,M[:,:,y,x], xlabel=xlabel, ylabel=ylabel, zlims=(minvalue,maxvalue))
push!(plots,plot)
end
end
plot(plots..., layout=(yL,xL))