Problems with drawing graphs in R - r

I am making a plot of means in R and my data collection started in October and ends in August the following year. the problem is when I draw my graph the default graph that I get has the months in alphabetical order(i.e. april,august,december etc..) instead of the order that I had enter them(i.e. ocboer, november,december etc..). how do I change that?

You can make your month into a factor variable and then plot the factor on the X axis.
factorisedMonth <- factor(oldMonth,levels=month.name)
R has a built in month.name that orders the factor properly if it's the standard english names in full

Related

plotly and week number line graph

I am trying to graph the weekly evolution of the number of downloads.
I have data similar data for all o week year and plotly cut de line.
example:
Downloads_weekly=Downloads.groupby(['Week_Number']).agg({'Daily Installs' : 'sum'})
fig1=px.line(Downloads_weekly.reset_index() , x='Week_Number' , y= 'Daily Installs')
fig1.update_xaxes(
tickformat="%Y-%W"
)
fig1
I have the exact same issue- Im still trying to figure it out. What I think is that Plotly is converting the dates to months, anyway it becomes a big mess. The way I solve the problem is by hacking around it, I create my Year-Week columns
df['Year-Week'] = df['Date'].dt.strftime('%Y.%U')
And then I sort the data frame, and plot that
df.sort_values(['Year-Week'], ascending=True, inplace=True)

Converting dataset into Time Series

mydatasetI have this data set which consists of two attributes i.e Year(2016,2017,2018) and Month(JAN TO DEC). The data set contains the average sales value for all the months for the years 2016, 2017 & 2018. Now when I import this data set, it shows that the data set is a "data.frame" . However I want it to be in "ts" . Then I ran this command
data.ts<- as.ts(myData)
to convert my data into "ts". The result is as follows:
class(data.ts)
[1] "mts" "ts" "matrix"
Now, I want my data set to be in "ts" only, meaning when I run the command class(data.ts). It should show "ts" only. How can I convert my data in "ts" only? And does this "mts" and "matrix" matters or not?
Also, when I plot my data using the command
plot(data.ts)
It shows a plot in which Time is on x-axis while Year and Sales are on y-axis. On the other hand, I want to plot a graph which shows the Year in x axis and Sales values of Months on y-axis.
How do I arrange my data such that when I import the dataset, it is already in ts? Or is there any other way to do it? Also, how to arrange the dataset that it shows the Year on x axis by default. I'm really confused as all the videos that I have seen on YouTube has their data already in "ts". Also, their plot shows Year on x-axis. Hope I have made myself clear. Any help would be appreciated.
How can I plot the graph such that Year is on x axis?
Reorder the data in a single variable:
data=as.matrix(data)
data= as.data.frame(t(data))
names(data)=c('x2016','x2017','x2018')
series=c(data$x2016,data$x2017,data$x2018)
Then take just index accordingly to the start point and the frequency of data. In your case looks like monthly from 2016 hence:
data.ts=ts(series ,start=c(2016,1), frequency=12)
plot(data.ts)

Changing x axis label in GGPlot2

I have an r data set which has money spendings spread across months, and also grouped by years.
Year|Mth_Year|Mth_Spend
2004|01-2004|42507163
2004|02-2004|3377592
2006|10-2006|3507636
2006|11-2006|4479139
2006|12-2006|2439603
I need to display the monthly information (grouped year wise), so that some quick comparisons can be done.
I am using the ggplot, geom_bar options to display the monthly spends. Below is the code, I use.
ggplot(data=yearly_spending,aes(x=Year,y=Mth_Spend,fill=Mth_Year))+
geom_bar(stat="identity",color="black",position=position_dodge())+
theme(axis.text.x=element_text(angle=90,hjust=1))
When I use this code, the bar chart is getting displayed. But in X Axis, only the years (2004, 2005 & 2006) are displayed. Can I get the months also displayed above the years. The years can appear horizontally, and while months can be placed vertically.
Thank you for all the suggestions. Using the scales package and lubridate to convert strings into date, I could solve the issue.
ggplot(data=monthly_spend_catwise,aes(x=Mth_Year,y=TotSpending,fill=Type))+
scale_x_date(labels=date_format("%m-%Y"),date_breaks = "1 month")+
theme(axis.text.x=element_text(angle=45,hjust=1,vjust=0.5))+
geom_bar(stat="identity",color="black",position=position_dodge())
I had formatted the dates in Mth_Year (instead of 01-2004, made it into 01-01-2004), using the below code.
spending$Mth_Year <- as.Date(paste("01",spending$Mth_Year,sep="-"),"%d-%m-%y")

Plot occurrences over time in R from a dataset of H:M:S timestamps

I'm trying to plot, essentially, a timeline. I have two lists of timestamps in %H:%M:%S format, and I want to show them as occurrences over an x-axis of continuous time from 0 to 8 minutes.
Everything I can find is either a specialized package for a timeline that bins data by year instead of showing it continuously scatterplot style, or it requires a y-axis. I'm not plotting a function, so I don't have a y-axis, just two lines made up of dots something like this: mock excel plot. I got this by putting in fake values for the missing axis. It's also vertical instead of horizontal, but that's easily fixed.
I'm trying to show all the times in those 8 minutes a person pronounced an S and all the times she deleted it so I can see if she starts deleting more as time goes on.
Edit: here's some of my data as requested.
s deleted s pronounced
0:02:32 0:00:49
0:04:01 0:00:50
0:04:02 0:00:51
0:04:10 0:00:56
0:04:11 0:00:58
0:04:44 0:00:59
0:05:59 0:01:09
0:06:03 0:01:10
0:06:31 0:01:12
0:06:33 0:05:59
0:06:40 0:06:00
0:06:06
0:06:06
0:06:32
0:06:43
0:06:44
0:06:47
0:06:51
0:06:52
0:07:10
0:07:14

Weekly time series plot in R

I am trying to create a plot of weekly data. Though this is not the exact problem I am having it illustrates it well. Basically imagine you want to make a plot of 1,2,....,7 for for 7 weeks from Jan 1 2015. So basically my plot should just be a line that trends upward but instead I get 7 different lines. I tried the code (and some other to no avail). Help would be greatly appreciated.
startDate = "2015-01-01"
endDate = "2015-02-19"
y=c(1,2,3,4,5,6,7)
tsy=ts(y,start=as.Date(startDate),end=as.Date(endDate))
plot(tsy)
You are plotting both the time and y together as individual plots.
Instead use:
plot(y)
lines(y)
Also, create a date column based on the specifics you gave which will be a time series. From here you can add the date on the x-axis to easily see how your variable changes over time.
To make your life easier I think your first step should be to create a (xts) time series object (install/load the xts-package), then it is a piece of cake to plot, subset or do whatever you like with the series.
Build your vector of dates as a sequence with start/end date:
seq( as.Date("2011-07-01"), by=1, len=7)
and your data vector: 1:7
a one-liner builds and plots the above time series object:
plot(as.xts(1:7,order.by=seq( as.Date("2011-07-01"), by=1, len=7)))

Resources