This is a test version of Biostars. For the public version, visit https://www.biostars.org.
R scripting - cannot coerce type 'closure' to vector of type 'character'

I have a dataset like this

Date    location    count
18-01-2023  A   38.457
18-01-2023  B   0
24-01-2023  C   0
24-01-2023  A   0
24-01-2023  C   0
27-01-2023  B   35.22
27-01-2023  A   0
27-01-2023  B   0
01-02-2023  A   0
01-02-2023  B   38.78
01-02-2023  C   0
01-02-2023  D   0
03-02-2023  B   0
03-02-2023  A   0
03-02-2023  C   35.789
03-02-2023  D   0

I want to create a plot where X axis is Date and Y axis is count and 4 different lines will be plotted with different colours for different location A, B, C and D.

demo image :

enter image description here

I have tried this command

library(lubridate)
library(dplyr)
library(ggplot2)

df<-all_data

df |>
  dplyr::mutate(
    Date=dmy(Date),
    count=as.numeric(count))|>
  ggplot(aes(x=Date, y=count, color=Location,group=Location)) +
  geom_line()+
  geom_point()

but its not working .

can anybody please help .

linux ggplot r

What error are problem are you having?

Error in as.character(x) : cannot coerce type 'closure' to vector of type 'character'

Please provide reproducible example data: dput(head(df)), check the structure of your data, str(df). There is a typo on column name, too location vs Location.

1 answer

Your code works for me, as long as you (1) fix the column heading to make sure Location is capitalized, and (2) make sure your data frame is actually 3 columns. If it is only a single column, I get the error you get.

Your data should be in 3 columns. You can check:

> dim(df)
[1] 16  3

This is what I get if I read in your data as typed above after I've converted the spaces to tabs so that I get 3 columns. If I read it in as is, I get only a single column because there are multiple spaces separating each field.

If you are reading your data into R and it is space delimited, you can convert it to tab delimited using sed on the command line:

sed 's/ \+/\t/g' test_data.txt > test_data_tabbed.txt

OR, if your data is already in R, but is not a 3 column data frame you can convert it to one as follows (ugly, but works):

# before conversion
dim(df)
[1] 16  1

# split each row element by multiple spaces for a list of elements
foo <- apply(df, 1, function(x){strsplit(x, " +")})
# deconstruct compound list to a simpler list
foo <- lapply(foo, unlist)
# convert list of elements to matrix
df <- do.call(rbind, foo)
# convert matrix to datframe
df <- as.data.frame(df)
colnames(df) <- c("Date", "Location", "count")

# after conversion
dim(df)
[1] 16  3

With the data in 3 columns, the plot works as expected.

Hello, thank you for your suggestion. I tried them, still however I am having this error with the code -

Error in seq.int(0, to0 - from, by) : 'to' must be a finite number In addition: The warning message: There was 1 warning in dplyr::mutate(). ℹ In argument: Date = dmy(Date). Caused by warning: ! All formats failed to parse. No formats found.

What are the dimensions of your data? dim(df) ? What does your data look like? What is the result of head(df) ?

Log in to answer this question.