-5

enter image description hereI have the following data; please can any one help me to plot it, I have tried to use a lot of different commands but none has given me a perfect graph

year   x   y 
2012   4   5
2014   7    9 
2017   4    3

enter image description here this picture i need to make as it

  • Aaah the elusive "perfect graph"...;-) Can you please provide details what "commands" you have tried so far, and what your expected output plot is supposed to look like? Without that we are left guessing. – Maurits Evers Jun 24 '18 at 21:59
  • require(graphics) some of them ts.plot(ldeaths, mdeaths, fdeaths, gpars=list(xlab="year", ylab="deaths", lty=c(1:3))) # } – Maryam Fasfous Jun 24 '18 at 22:05
  • 3
    Don't post code in comments; please [edit](https://stackoverflow.com/posts/51014087/edit) your question to include those details. If you post code, you need to make sure that it is reproducible including matching sample data. We don't know anything about `ldeaths`, `mdeaths` etc. Please spend some time reviewing how to provide a [minimal reproducible example/attempt](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), including sample data; this will significantly increase the quality of your post. – Maurits Evers Jun 24 '18 at 22:12

3 Answers3

1

Based on your comments you might be looking for:

library(tidyverse)

plot1 <- df %>% gather(key = measure, value = value, -year) %>%
ggplot(aes(x = year, y = value, color = measure))+
geom_point()+
geom_line()+
facet_wrap(~measure)

plot1

enter image description here

The biggest points here are gather and facet_wrap. I recommend the following two links:

https://ggplot2.tidyverse.org/reference/facet_grid.html

https://ggplot2.tidyverse.org/reference/facet_wrap.html

AndS.
  • 7,748
  • 2
  • 12
  • 17
0

You need to convert year column type to Date. This is a tidyverse style solution

library(tidyverse)

mydf %>% 
  rename("col1" = x, "col2" = y) %>% 
  mutate(year = paste0(year, "-01-01")) %>% 
  mutate(year = as.Date(year)) %>% 
  ggplot() + 
  geom_line(aes(x = year, y = col1), color = "red", size = 2) + 
  geom_line(aes(x = year, y = col2), color = "blue", size = 2) +
  theme_minimal()

which returns this

ggplot output

Selcuk Akbas
  • 711
  • 1
  • 8
  • 20
0

Using the data shown reproducibly in the Note below use matplot. No packages are used.

matplot(dd[[1]], dd[-1], pch = c("x", "y"), type = "o", xlab = "year", ylab = "value")

Note

dd <- structure(list(year = c(2012L, 2014L, 2017L), x = c(4L, 7L, 4L), 
y = c(5L, 9L, 3L)), class = "data.frame", row.names = c(NA, -3L))

screenshot

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341