0

I have data as like this

data1  data2  data3  data78 data89
10     47     85      52    78
8      78     22            4
4      44     44            5
       77     77            3
              11            7

and I need to plot x axis as positions(1-5) and y axis as the counts.. example (I did it on excel) enter image description here

I want to do it the same in R .I tried with line function and it shows error as like this "Error in structure(.Call(C_tukeyline, as.double(xy$x[ok]), as.double(xy$y[ok]), : insufficient observations"

user90
  • 381
  • 2
  • 10

2 Answers2

2

Using ggplot2 you can get the data in long format and plot.

library(dplyr)
library(ggplot2)

df %>%
  tidyr::pivot_longer(cols = everything(), 
               values_drop_na = TRUE) %>%
  group_by(name) %>%
  mutate(id = row_number()) %>%
  ggplot() + aes(id, value, color = name) + geom_line(size = 2)

enter image description here

data

df <- structure(list(data1 = c(10L, 8L, 4L, 77L, 11L), data2 = c(47L, 
78L, 44L, 77L, NA), data3 = c(85L, 22L, 44L, NA, 7L), data78 = c(52L, 
NA, NA, 3L, NA), data89 = c(78L, 4L, 5L, NA, NA)), 
class = "data.frame", row.names = c(NA, -5L))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
2

Just use matplot.

matplot(d, type="l")
legend("topright", names(d), col=seq(d), lty=seq(d), horiz=T, cex=.7, bty="n")

enter image description here


Data:

d <- structure(list(data1 = c(10L, 8L, 4L, 77L, 11L), data2 = c(47L, 
78L, 44L, 77L, NA), data3 = c(85L, 22L, 44L, NA, 7L), data78 = c(52L, 
NA, NA, 3L, NA), data89 = c(78L, 4L, 5L, NA, NA)), class = "data.frame", row.names = c(NA, 
-5L))
jay.sf
  • 60,139
  • 8
  • 53
  • 110