Creating a graph but getting error message: unexpected symbol in... and can't find 'stat'

I tried creating a basic graph with the code below and I keep the message: Error: unexpected symbol in " geom_bar( aes ( x = Specimen Month"

library(tidyverse)
library(ggplot2)

ggplot(Inmatelist) +
geom_bar( aes ( x = Specimen Month), fill = Test Results (blue)

I do the same code without the "+" and I keep getting the same results.
I tried another code:

ggplot(Inmatelist, aes(x = Specimen Month, y = Test Result, fill=supp)) +
geom_bar(stat="Inmatelist", position=position_dodge()) + scale_fill_brewer(palette="Paired") +
theme_minimal ()
scale_fill_manual(values=c('#102a4c','#f37021'))
labs(x = "Specimen Month", y = "Test Result", title = "HCJ Results by Month")

and I get the error code:

Error: Can't find stat called 'Inmatelist'
Run rlang::last_error() to see where the error occurred.

scale_fill_manual(values=c('#102a4c','#f37021'))
<ggproto object: Class ScaleDiscrete, Scale, gg>
aesthetics: fill
axis_order: function
break_info: function
break_positions: function
breaks: waiver
call: call
clone: function
dimension: function
drop: TRUE
expand: waiver
get_breaks: function
get_breaks_minor: function
get_labels: function
get_limits: function
guide: legend
is_discrete: function
is_empty: function
labels: waiver
limits: NULL
make_sec_title: function
make_title: function
map: function
map_df: function
n.breaks.cache: NULL
na.translate: TRUE
na.value: NA
name: waiver
palette: function
palette.cache: NULL
position: left
range: <ggproto object: Class RangeDiscrete, Range, gg>
range: NULL
reset: function
train: function
super: <ggproto object: Class RangeDiscrete, Range, gg>
rescale: function
reset: function
scale_name: manual
train: function
train_df: function
transform: function
transform_df: function
super: <ggproto object: Class ScaleDiscrete, Scale, gg>
labs(x = "Specimen Month", y = "Test Result", title = "HCJ Results by Month")
$x
[1] "Specimen Month"

$y
[1] "Test Result"

$title
[1] "HCJ Results by Month"

attr(,"class")
[1] "labels"

I have only used R to analyze data in a database. This is the first time I am analyzing data in an excel spreadsheet. I am also still new to R. The data is categorical as well, I don't know if that makes a difference. Test results are positive, negative, or unknown. The months is January, February, etc.

That should not make any difference once the data is in R

You have some mistakes in your syntax but first, I think we need to se some sample data.
See ?dput for a good way to supply some or have a look here for general hints on asking questions.

I cannot add data because it is confidential. Also, that was a few days again and I no longer can do the summary because R could not find the excel sheet.

Why does this affect a summary?

Can you make up some data? The real questions, I think are the data types and the column names. You could make a copy of the data.frame and replace the actual data with randomly generated data.

When I tried to do the summary, it said the file could not be found.

I'll do that and see if I can get a summary.

Don't worry about the summary until we have some data to play with.

What do you get if you do:

str(Inmatelist)

?

Can you paste the output here?
Thanks,
John

I created the mock data and imported it via, "Import Dataset". Then I put the following:

read_table("Inmate List MOCK DATA.xlsx")

Inmatelist<-("Inmate List MOCK DATA.xlsx")

read_table(Inmatelist)
summary(Inmatelist)
str(Inmatelist)

The results are:

read_table(Inmatelist)
! Multiple files in zip: reading ''[Content_Types].xml''

-- Column specification -------------------------------------------------------------------------------------------------
cols(
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> = col_character()
)

A tibble: 1 x 1

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

1 "<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types\"><Default"

read_table(Inmatelist)
! Multiple files in zip: reading ''[Content_Types].xml''

-- Column specification -------------------------------------------------------------------------------------------------
cols(
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> = col_character()
)

A tibble: 1 x 1

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

1 "<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types\"><Default"

summary(Inmatelist)
Length Class Mode
1 character character
read_table(Inmatelist)
! Multiple files in zip: reading ''[Content_Types].xml''

-- Column specification -------------------------------------------------------------------------------------------------
cols(
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> = col_character()
)

A tibble: 1 x 1

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

1 "<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types\"><Default"

summary(Inmatelist)
Length Class Mode
1 character character
str(Inmatelist)
chr "Inmate List MOCK DATA.xlsx"

image

#> # A tibble: 41 x 12
#> SPN Name Location Test.Date Age Gender Test.Result.Date Test.Result
#>
#> 1 1.24e6 Tomm~ Tank E 1/1/2021 53 M 1/2/2021 Negative
#> 2 1.24e6 Jay ~ Tank B 1/2/2021 38 F 1/3/2021 Negative
#> 3 1.24e6 Stan~ Tank C 1/1/2021 76 F 1/2/2021 Positive
#> 4 1.25e6 Phoe~ Tank B 1/2/2021 35 F 1/3/2021 Negative
#> 5 1.25e6 Jack~ Tank D 1/1/2021 52 M 1/2/2021 Negative
#> 6 1.25e6 Iris~ Tank C 1/1/2021 59 M 1/2/2021 Positive
#> 7 1.24e6 Bay ~ Tank B 1/1/2021 42 M 1/2/2021 Negative
#> 8 1.26e6 Clar~ Tank D 1/1/2021 82 F 1/2/2021 Negative
#> 9 1.25e6 Magn~ Tank D 1/1/2021 41 M 1/2/2021 Negative
#> 10 1.24e6 Clar~ Tank D 1/1/2021 64 M 1/2/2021 Negative
#> # ... with 31 more rows, and 4 more variables: Is.Symptomatic? ,
#> # Test.Year , Result.Month , Result.Week

Reprex

Inmatelist<-("Inmate List MOCK DATA.xlsx")

read_table(Inmatelist)
#> Error in read_table(Inmatelist): could not find function "read_table"
summary(Inmatelist)
#> Length Class Mode
#> 1 character character
str(Inmatelist)
#> chr "Inmate List MOCK DATA.xlsx"
Created on 2021-02-10 by the reprex package (v1.0.0)

I was not clear enough.

I would like to see the results for the data.frame Inmatelist created from Inmatelist.xlsx when you do:

str(inmatelist)

I am not sure what this is doing

read_table("Inmate List MOCK DATA.xlsx")

but it is not reading in the file as we want.

Try

library(readxl)
Inmatelist  <-  read_excel("Inmate List MOCK DATA.xlsx")
# then
str(Inmatelist)

It would be better to use a name such as mockdata for the mock data just to keep things separate.

So

library(readxl)
mockdata  <-  read_excel("Inmate List MOCK DATA.xlsx")
# then to see what we get
str(mockdata)

A very simple and very effective way to supply some data is to use the dput() command.

dput(mydata)

and then simply copy the output and paste it here. If you have a very large data set then a sample should be fine. To supply us with 100 rows of your data set do

dput(head(mydata , 100))

where mydata is the name of your dataframe or tibble

1 Like

This is not a valid r object name , the space implies the end of a symbol and the start of another symbol. You can rename your variable to avoid this, or refer to it within backticks so that r understands it is a single symbol.

`Specimen Month`
1 Like

Thank you! I can't believe it. I am utterly amazed that it actually worked.

I did the str(mockdata) and it populated the information and dput(mockdata) populated everything. I tried to reprex the string and it says

dput(mockdata)
#> Error in dput(mockdata): object 'mockdata' not found

I included the screenshot.

I used the ticks but nothing happened. I have ggplot2 loaded in my packages.

ggplot(mockdata, aes(x = Result Month, y = Test Result, fill=supp)) +
geom_bar(stat="mockdata", position=position_dodge()) + scale_fill_brewer(palette="Paired") +
theme_minimal ()
#> Error in ggplot(mockdata, aes(x = Result Month, y = Test Result, fill = supp)): could not find function "ggplot"
scale_fill_manual(values=c('#102a4c','#f37021'))
#> Error in scale_fill_manual(values = c("#102a4c", "#f37021")): could not find function "scale_fill_manual"
labs(x = "Result Month", y = "Test Result", title = "Results by Month")
#> Error in labs(x = "Result Month", y = "Test Result", title = "Results by Month"): could not find function "labs"

I dont know what that means, but my guess is that you have it installed but not loaded to the session.
You should do

library(ggplot2)

Before all your other code.

It's loaded. I had loaded it before running the code.

I think we need to see some sample data.

The reprex isn't showing anything.

dput(mockdata)
#> Error in dput(mockdata): object 'mockdata' not found

Had you used readexcel to make mockdata before attempting to dput it?

Yes, the reprex doesn't work, but when I put dput(mockdata), it works.