Layale
Layale

Reputation: 163

Plotting a line using different colors or line types R

I'm facing data visualization issues. Here is an extract of my data :

head(Dataset) 

Project    Date             Budget   Project Status
PR1      September 2015     0.2        Ongoing
PR1      October 2015       0.5        Ongoing
PR1      November 2015      0.9        Ongoing
PR1      December 2015      1.2        Ongoing 
PR1      January 2016       1.8        Suspended
PR1      February 2016      1.8        Suspended
PR1      March 2016         1.8        Abandoned   
PR2      August 2015        1.5        Ongoing
PR2      September 2015     1.9        Ongoing
PR2      October 2015       2.3        Ongoing
PR2      December 2015      2.5        Ongoing
PR2      January 2016       2.8        Ended

I would like to plot the budget evolution for each project according to the status modification. I tried the following code

 g1 <- ggplot(Dataset, aes(Dataset$Date, Dataset$Budget))+
 geom_line(color=Dataset$Project, linetype=Dataset$Status)

But I got this error :

Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line

Can you advise me?

Thanks a lot !

Upvotes: 0

Views: 812

Answers (1)

Andrew Lavers
Andrew Lavers

Reputation: 4378

Note below tidied up naming in the data values (Year, Month, Project Status)

Dataset <- read.table(text="
Project  Month     Year      Budget     Project_Status
PR1      September 2015     0.2        Ongoing
PR1      October 2015       0.5        Ongoing
PR1      November 2015      0.9        Ongoing
PR1      December 2015      1.2        Ongoing 
PR1      January 2016       1.8        Suspended
PR1      February 2016      1.8        Suspended
PR1      March 2016         1.8        Abandoned   
PR2      August 2015        1.5        Ongoing
PR2      September 2015     1.9        Ongoing
PR2      October 2015       2.3        Ongoing
PR2      December 2015      2.5        Ongoing
PR2      January 2016       2.8        Ended
", header=TRUE)

library(lubridate)

# Make date a true date type, using lubridate conversions
Dataset$Date = dmy(paste("1", Dataset$Month, Dataset$Year))

# Plot with the dataset sepecified once (cleaner)
g1 <- ggplot(Dataset, aes(x=Date, y=Budget)) +
  # draw line for the budget coloring by project
  geom_line(aes(color=Project))  +
# draw a point overlay for the stautus at that point in time
  geom_point(aes(shape=Project_Status))
print(g1)

enter image description here

Upvotes: 3

Related Questions