I'm fairly new to R. While executing below line of code I've encountered an error (Error in seq.int(0, to0 - from, by) : 'to' must be a finite number) at the end of line ggplot2, details below. Not sure what causes this. Although I saw another post on a similar error however, it doesn't seem to be fully relevant to my code structure. Appreciate all inputs. Thanks.
setwd(("C:/Users/xx")
library(ggplot2)
library(forecast)
library(dplyr)
library(colortools)
Master <- read.csv("master.csv")
class(Master$TIMESTAMP)
Master$TIMESTAMP_posix <- as.POSIXct(Master$TIMESTAMP, format = "%D/%M/%Y %H:%M:%S")
class(Master$TIMESTAMP_posix)
(time_plot_2 <- ggplot(Master, aes(x = TIMESTAMP_posix, y = VWC_CS7)) +
geom_line() +
scale_x_datetime(date_labels = "%Y", date_breaks = "1 day") +
theme_classic())
Related
I'm trying to create a plot that shows a couple of variables (integers) on a single x axis (date), and having some issues getting the base created.
I keep getting the error message
"Error in as.matrix(x) : argument "x" is missing, with no default".
Here is all my code, if you can help it would be fantastic!
avail <- avail %>%
mutate(Date = as.Date(avail$Date, format = "%a %d-%b-%Y"))
avail <- format(avail, format="%d-%m-%Y")
avail
names(avail)[2] <- "Available Jobs"
df <- data.frame("Date" = avail$Date,
"Available Jobs" = avail$`Available Jobs`,
"Jobs" = job$Jobs.Added,
"Views" = view$Views)
This is the part that gives error messages:
ggplot(df, aes(x=df$Date))+
geom_line(aes(y=df$Available.Jobs), size=2, color=scale.default())+
geom_line(aes(y=df$Views), size=2, color=scale.default())+
scale_x_continuous(
name= "Available Jobs",
sec.axis = sec_axis(~.*coeff, name = "Views"))+
ggtitle("September views against available jobs")
"it doesn't work without specifying the dataset": well, with respect, then you're doing something wrong. It turns out you're probably doing several things wrong...
You haven't given us a reprodicible example, so I can't be 100% sure of what you want, but here's a close as I can get.
First, create some data and load libraries
library(tidyverse)
library(lubridate)
# For reproducibility
set.seed(123)
df <- tibble(
Date=seq(ymd("2022-09-01"), ymd("2022-09-30"), "1 day"),
`Available Jobs`=runif(30, 100, 200),
Jobs=runif(30, 50, 150),
Views=runif(30, 200, 300)
)
Now attempt to create a plot. I've commented where I've either corrected errors or made assumptions.
df %>% ggplot(aes(x=Date)) +
# Note the correction to the name of the y variable
# color=scale.default() is the source of the "as.matrix()" error
geom_line(aes(y=`Available Jobs`), size=2, color="blue") +
geom_line(aes(y=Views), size=2, color="red") +
# Why label the x axis with the name of the y variable? Is this change correct?
scale_y_continuous(
name= "Available Jobs",
# Removing color=scale.default() introduces object 'coeff' not found here
# sec.axis = sec_axis(~.*coeff, name = "Views")
) +
ggtitle("September views against available jobs")
[See? No references to df inside the pipe.] This gives:
Another problem may be that your data is (probably) not tidy - because you have information in the names of your columns. ggplot (and the rest of the tidyverse) expects tidy data, so you are attempting to fight against the tidyverse's expectations. One way to make your data tidy and produce a similar plot might be:
df %>%
pivot_longer(
c(`Available Jobs`, Views),
names_to="Metric",
values_to="Value"
) %>%
ggplot() +
geom_line(aes(x=Date, y=Value, colour=Metric))
I've omitted some of the formatting of the plot to focus on the key issue: making your data tidy.
I've had to make guesses and assumptions in providing this answer, principally because you didn't provide a minimal reproducible example.
I am trying to make a data vs. Time graph for some Methane emissions data I have. The code so far looks like this:
CH4 <- as.numeric(Aeris_2_Data$CH4)
Aeris_2_Data$Date.Time <- as.POSIXct(Aeris_2_Data$Time_Stamp, tz = "", "%m/%d/%Y %H:%M:%S")
ggplot(Aeris_2_Data, aes(x = Aeris_2_Data$Date.Time, y = as.numeric(CH4)) + geom_point() + labs(x = "Time", y = "CH4 [ppm]") + ggtitle("Methane Over Time")
My data looks like this:
head(Aeris_2_Data) and this: an extension of head
I am trying to map CH4 over time as you can probably see from the small code fragment I've managed so far. but I keep getting the error:
Error in seq.int(0, to0 - from, by) : 'to' must be a finite number
Everything seems to match the ggplot info I remember and also found online. What is going wrong? My guess is to do with the formatting of the time data, which is in the format %m/%d/%Y %H:%M:%Sand stored as a character in the csv file I am pulling from. How do I properly format that to change it? Thanks in advance.
There are two errors in your code :
date format is "%m/%d/%Y %H:%M" and not "%m/%d/%Y %H:%M:%S"
one ) is missing after aes()
Additionnaly as mentioned is the comments you should better use Date.Time and transform CH4 as numeric directly into the data.frame
The code should be:
Aeris_2_Data$CH4 <- as.numeric(Aeris_2_Data$CH4)
Aeris_2_Data$Date.Time <- as.POSIXct(Aeris_2_Data$Time_Stamp, tz = "", "%m/%d/%Y %H:%M")
ggplot(Aeris_2_Data, aes(x = Date.Time, y = as.numeric(CH4))) + geom_point() + labs(x = "Time", y = "CH4 [ppm]") + ggtitle("Methane Over Time")
I have a CSV file called gdata.csv, with data like:
id,date,totKm,eLiter,euros,liters,km
1,24-04-2010,23678,1.180,42.00,35.59,450
2,16-05-2010,24058,1.200,43.00,35.83,380
3,27-05-2010,24488,1.160,44.00,37.93,430
4,12-06-2010,24960,1.180,45.00,38.14,472
With ggplot2
I just want to plot date and eliter in a line char with ggplot2, with this code:
x_date <- as.Date(gdata$date, format = "%d-%m-%Y")
ggplot(eliter, aes(x_date, eliter)) + geom_line()
But, it returns this error related with the class:
Error: ggplot2 doesn't know how to deal with data of class numeric
I have tried to make a data.frame but it stills returns the error:
d <- data.frame(xdate = x_date, yeliter=gdata$eLiter)
ggplot(d$xdate, aes(d$xdate, d$yeliter)) + geom_line()
Error: ggplot2 doesn't know how to deal with data of class Date
With plot
I have managed to do this with plot() function:
plot(gdata$eLiter~as.Date(gdata$date, "%d-%m-%Y"), type = "s", xlab="Date",ylab="€/Liter", main="€/liter trend", col='blue')
And it works fine! But I can not do it with ggplot.
Could anyone help me?
Thank you very much.
Add + scale_x_date() like this:
Lines <- "id,date,totKm,eLiter,euros,liters,km
1,24-04-2010,23678,1.180,42.00,35.59,450
2,16-05-2010,24058,1.200,43.00,35.83,380
3,27-05-2010,24488,1.160,44.00,37.93,430
4,12-06-2010,24960,1.180,45.00,38.14,472"
DF <- read.csv(text = Lines)
DF$date <- as.Date(DF$date, "%d-%m-%Y")
library(ggplot2)
ggplot(DF, aes(date, eLiter)) +
geom_line() +
scale_x_date()
I am trying to create a graph with the following characteristics:
x-axis: time and date
y-axis: data
here you can download my dataframe: https://my.cloudme.com/josechka/data
I try to produce the graph using:
p <- ggplot(data,aes(x = Date, y = Var,group = 1))
+ geom_line()
+ scale_x_date(labels = date_format("%m/%d/%Y"))
+ scale_y_continuous(limits = c(0, 70000))
p
And I get the result:
Error: Invalid input: date_trans works with objects of class Date only
I am quite new in R and ggplot. What am I doing wrong?
As suggested you have to format the Date column into a Date object.
data$Date<-as.Date(data$Date, format="%d/%m/%Y")
Now you can use your script in order to create the plot:
library("ggplo2")
library("scales")
p <- ggplot(data,aes(x = Date, y = Var,group = 1))
+ geom_line()
+ scale_x_date(labels = date_format("%m/%d/%Y"))
+ scale_y_continuous(limits = c(0, 70000))
p
And this is the resulting plot:
Thanks for the comments. They helped me to find out the solution. Both comments allow to represent my data. However, there is small problem: data from the same day is grouped and it is not possible to see the daily behaviour of the variable. I tested to format the Date column using the next command:
as.POSIXct(data$Date, format="%d/%m/%Y %H:%M:%S")
It worked out. However it is important to have the original data in the format d/m/Y h:m:s. Thanks very much for the comments which help me a lot to solve my problem.
I am having a real hard time with ggplot function!
I try to briefly explain my problem.
I have a dataset of several tweets associated to a time stamp; I would like to plot the data obtaining a graph with time on the x bar and the frequency or the "tweet-rate" per hour on the y axis.
What did I do?
library(ggplot2)
c4l.tweets <- read.csv("/Users/vincenzo/Desktop/Collect %23c4l13 Tweets - Archive.csv")
c4l.tweets$time <- as.POSIXct(strptime(c4l.tweets$time, "%d/%m/%Y %H:%M:%S", tz="CST") - 6*60*60)
library(chron)
c4l.tweets$by.hour <- trunc(c4l.tweets$time, units="hours")
ggplot(count(c4l.tweets, "by.hour"), aes(x=by.hour, y=freq))
+ geom_bar(stat="identity") + xlab("Number") + ylab("Date") + labs(title="tweets by hour")
So basically I truncated the data by the timestamp and used the count function to plot them.
I get the
Error: No layers in plot
and
Error in +geom_bar(stat = "identity") : argument not valid for the operator
But why? what am I doing wrong?
I usually have this problem each time i try to plot something via ggplot, what do I do wrong?
Thank you!
Vincenzo