0

I have this dataset:

at the beginning I had,

Date      Action  Daily.Contact
1/1/2012  All        4
1/1/2012  Email      2
1/1/2012  Text       1
2/2/2012  Phone Call 1
2/2/2012  All        7
2/2/2012  Email      2
2/2/2012  Text       2
2/2/2012  Phone Call 2
3/3/2012  All        4
3/3/2012  Email      2
3/3/2012  Text       1
3/3/2012  Phone Call 1
4/3/2012  All        9
4/3/2012  Email      4
4/3/2012  Text       4
4/3/2012  Phone Call 1

After some data manipulation I ended up with:

Date      All Email Text Phone Call
1/1/2012  4   2     1     1
2/2/2012  7   2     2     3
3/3/2012  4   2     1     1
4/4/2012  9   3     4     2

How do I create a line graph showing 3 lines for Email, Text, Phone call?

So far I have:

plot(as.Date(df$Date), df$Daily.Contact, 
 type = 'l', lwd = 2, xlab = "Date", ylab = "Daily.Contact")   

2 Answers2

0

You may try

Data

library(dplyr)

df <- read.table(text = "Date      All Email Text 'Phone Call'
1/1/2012  4   2     1     1
2/2/2012  7   2     2     3
3/3/2012  4   2     1     1
4/4/2012  9   3     4     2", header = T) %>%
  mutate(Date = as.Date(Date, format = "%d/%m/%Y"))

Check min, max of Daily Contact for ylim

max(df[,3:5])
[1] 4
min(df[,3:5])
[1] 1

Then,

plot(df$Date, df$Email, type = "l", lwd = 2, xlab = "Date", ylab = "Daily Contact", col = "red", ylim = c(1,4))
lines(df$Date, df$Text,  lwd = 2, col = "steelblue")
lines(df$Date, df$Phone.Call,  lwd = 2, col = "brown")

enter image description here

Park
  • 14,771
  • 6
  • 10
  • 29
0

You could use matplot: Plots a matrix of points

df$Date <- as.Date(df$Date, format = "%d/%m/%Y")

matplot(df$Date, df[-1], type = 'l', lwd = 2, xlab = 'Date', ylab = 'Daily Contact')

enter image description here

Onyambu
  • 67,392
  • 3
  • 24
  • 53