R scripting - cannot coerce type 'closure' to vector of type 'character'
1
0
Entering edit mode
20 months ago
Adyasha • 0

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 • 5.5k views
ADD COMMENT
0
Entering edit mode

What error are problem are you having?

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
3
Entering edit mode
20 months ago
seidel 11k

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.

ADD COMMENT
0
Entering edit mode

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.

ADD REPLY
2
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 2822 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6