Skip to content
Prev 393596 / 398503 Next

Reg: ggplot error

Hallo

I am not familiar with any of packages you use (except of MASS and ggplot2) and the code is too complicated without any hint where the error could come from and what is the message you get. I wonder if anybody would like to go through your whole code.

1. data seems to be read correctly
ICUData <- read.csv(file = "ICUData.csv", stringsAsFactors = TRUE)
ICUData.neuro <- ICUData[ICUData$surgery == "neuro",]

2. ggplot(ICUData, aes(x=ICUData.neuro$LOS)) +
  geom_histogram(aes(y=after_stat(density)), binwidth = 5, 
                 fill = "darkgrey")

gives me error

Error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (500): x
Run `]8;;rstudio:run:rlang::last_error()rlang::last_error()]8;;` to see where the error occurred.

which, I believe, resulted from using whole ICUData in ggplot but only ICUData.neuro as aes value. Compare:

ggplot(ICUData.neuro, aes(x=ICUData.neuro$LOS)) +
  geom_histogram(aes(y=after_stat(density)), binwidth = 5,
                 fill = "darkgrey")

with

ggplot(ICUData, aes(x=ICUData.neuro$LOS)) +
  geom_histogram(aes(y=after_stat(density)), binwidth = 5,
                 fill = "darkgrey")

Cheers
Petr

And I do not provide private consulting so keep your posts to R help. Others may have much more insightful answers.


From: Upananda Pani <upananda.pani at gmail.com> 
Sent: Wednesday, January 11, 2023 2:43 PM
To: PIKAL Petr <petr.pikal at precheza.cz>
Subject: Re: [R] Reg: ggplot error

Hi Respected Member,

Please find attached.

Regards,
Upananda Pani
On Wed, Jan 11, 2023 at 6:07 PM PIKAL Petr <mailto:petr.pikal at precheza.cz> wrote:
Hi

Attachments are mostly removed from emails so they probably will not reach
r-help.

You said you get an error, which is the first place you should look at. It
can navigate you to the source of the error if you read it carefully.

Anyway, if your code is complicated it is difficult to understand and
decipher. So think about simplification and maybe you will find the error
source yourself during this simplification.

Cheers
Petr
http://www.R-project.org/posting-guide.html