0

This is my first post, so please let me know if I can add anything to make things easier. My problem is that I can't take the mean of my values, even though they are numeric. summarydoes show the mean, but I can't plot it with ggplot. This first part tells me the values are numeric.

  > class(Stockholm1$hornsgatan)
   [1] "numeric"

Now I try to make my plot, showing mean over time.

 > ggplot(Stockholm1,aes(date,mean("hornsgatan")), na.rm = TRUE)+ geom_line()+ labs(title = "Hornsgatan Mean", x = "Date", y = "PM10")
Warning messages:
1: In mean.default("hornsgatan") :
  argument is not numeric or logical: returning NA
2: In mean.default("hornsgatan") :
  argument is not numeric or logical: returning NA

And I get this error. Does anyone know what I am doing wrong?

B.Bos
  • 3
  • 2
  • Can you edit your question to include the result of `dput(head(Stockholm))`? See this post for help: https://stackoverflow.com/a/5963610/3022126 – Phil Jan 17 '18 at 10:30
  • Is `Stockholm1` deliberate (i.e. the `1` is missing in your data set above)? – Phil Jan 17 '18 at 10:31
  • Ah, they're both basically the same dataset. I'll edit it for clarity. Thank you. – B.Bos Jan 17 '18 at 10:34
  • You probably want something akin to: `ggplot(Stockholm1, aes(date, hornsgatan)) + stat_summary("line", fun.y = "mean")`. You are unlikely to a good complete answer without [a minimal reproducible example](http://stackoverflow.com/questions/5963269) – Axeman Jan 17 '18 at 10:48

1 Answers1

0

I can not replicate your dataset because I do not know the data contained in it.

so I can suggest you change your code this way:

ggplot(Stockholm1,aes(date,mean(hornsgatan)), na.rm = TRUE) + 
  geom_line() + 
  labs(title = "Hornsgatan Mean", x = "Date", y = "PM10")

Test in this way, should work

Axeman
  • 32,068
  • 8
  • 81
  • 94
Lorenzo Benassi
  • 621
  • 1
  • 8
  • 31
  • It doesn't give me an error anymore, just an empty plot. Any idea what could cause that? I would share the data, but I don't have the rights. – B.Bos Jan 17 '18 at 10:35
  • Technically it's a new question. Anyhow to solve this problem I should know how the dataset is composed – Lorenzo Benassi Jan 17 '18 at 10:40
  • @B.Bos accept my reponse and after post another specific question with the result of `dput(head(Stockholm1))` as suggested by Phil – Lorenzo Benassi Jan 17 '18 at 10:46
  • No reason to use `$` in `aes` here, I think (it is a bad practice to teach as it can fail in unexpected ways). Anyway, this will result in a single value which is likely not what needs to plotted here. Consider e.g. `ggplot(mtcars, aes(hp, mean(mpg))) + geom_point()`. – Axeman Jan 17 '18 at 10:47
  • Yes, but I expect it will not do what OP expects it to do (i.e. create a flat line). Also see my comment on the question. – Axeman Jan 17 '18 at 11:02