R, ggplot plot with only one variable

hello, I'm a beginner with R, maybe my problem is simple but I can't figure it out.
I have a data frame with three columns and many rows, the first shows the number of rows, the second the variable of interest and the third the date (day, month, year), obviously already in order. I would like to plot the variable on the Y axis and observe how it varies over time. it's possible?
I tried converting the first column into characters and using it on the X axis, but it gave me an error, I tried various geometries but either it gave an error or an unreadable graph came out.
d=read.table("W.txt", sep="\t")
as.character(d$V1)
ggplot(df,aes(x=V1,Y=V2))+ geom_point()

Hi, welcome to the forum.

I think we need to see a sample of your data. A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here.

In the meantime, here is an example based on your description. The cc column entries are actual dates. It is likely that you will need to convert your data to a date format. We will see when we get your sample data

library(ggplot2)
library(lubridate)

dat1 <- data.frame(aa = 1:10, bb = sample(3:7, 10, replace = TRUE), 
                   cc = seq(ymd("2023/01/01"), by = "day", length = 10))


ggplot(dat1, aes(x = cc, y = bb)) + geom_point()


Hi, I'll try to give an example, this is a part of my data frame, I've deleted the date column to make it easier:
360,14.9798832996408
361,5.86790881561615
362,12.5591318443865
363,21.0621489252143
364, NA
365,27.3879482258911
366,45.6641124184921
367,38.1810074760037
368,55.5306367158065
369,36.7689992100591
370,38.0658460735665
This is the simple code I use:
d=read.table("Pe.txt", sep="\t")
as.character(d$V1)
plot(d, xlab="N", ylab="p") + geom_point()
N and p are the headers of the columns, in the first column there are the days indicated only by numbers and in the second my variable. while this is the error:
Error in plot.window(...) : values of 'ylim' must be finite
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
It should be simple but I can't figure out where I'm going wrong

Here you are mixing base R plot() with ggplot2 functions, you can't do that, you have to choose one ploting system or the order, they can't be mixed.

You would get a better and more accurate plot by using the date variable instead of the row number, to help us help you, could you please turn this into a proper reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

As andresrcs has said we really need a reprex but we also need the data in usable format.

As I suggested before: A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here.

Here is a very simple example of how to do it.

dat <- data.frame(xx = 1:10, yy = letters[1:10])

dput(dat)

This give us

structure(list(xx = 1:10, yy = c("a", "b", "c", "d", "e", "f", 
      "g", "h", "i", "j")), class = "data.frame", row.names = c(NA,  -10L))

We can then copy it into R

my_dat  <- structure(list(xx = 1:10, yy = c("a", "b", "c", "d", "e", "f", 
      "g", "h", "i", "j")), class = "data.frame", row.names = c(NA,  -10L))

and we have an exact copy of your data.

1 Like