0

I want to plot the Emission of a Pollutant across different years so I used dplyr package to summarize the data but I'm not able to plot it because of the format of the returned dataframe.

I tried to convert the year and emission lists using as.numeric but it refused so what should I do ?

total_emmision_per_year <- SCC %>% group_by(year) %>% summarise(total_emmision_per_year = sum(Emissions)) 
plot(total_emmision_per_year[,1], total_emmision_per_year[,2] , pch = 19)

I get this error:

Error in stripchart.default(x1, ...) : invalid plotting method

zx485
  • 28,498
  • 28
  • 50
  • 59
  • Welcome to SO, Mohy! Please make this question *reproducible*. This includes sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`). Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Aug 12 '19 at 22:08
  • This is because `summarise` returns a tibble and the columns are not vectors, which `plot` expects. So either add `%>% as.data.frame()` to the end of the first line, or use `ggplot(aes(x, y)) + geom_point()`, where x and y are the names of columns 1 and 2, instead. – neilfws Aug 12 '19 at 22:14
  • 1
    To further illustrate the point, see how `mtcars[,1]` (a frame) differs from `as.tibble(mtcars)[,1]` (a tibble). – r2evans Aug 12 '19 at 22:38

1 Answers1

0

As it was said on the comments, the issue is because you have a tibble If you want to extract a vector from a tibble, you can use the pull function from tibble package. The following code might work if you want to plot using the base package. (although I would recommend using ggplot2, as neilfws suggest)

 library(tibble)
 plot(total_emmision_per_year %>% pull(1), total_emmision_per_year %>% pull(2) , pch = 19)
André Costa
  • 377
  • 1
  • 11
  • If the answer was good, you can upvote it. If the answer solved your problem, please mark it as solved. Doing so, you help the other users on this community. Thanks – André Costa Aug 16 '19 at 02:59
  • I did but I'm new so I can't do this right now but I record the upvote. Can you please have a look in this low quality plots problem https://stackoverflow.com/questions/57512755/low-quality-of-line-charts?noredirect=1#comment101501212_57512755 – Mohy Mohamed Aug 16 '19 at 09:03