0
votes

There are a lot of posts on geom_line() but I haven't found a question specific to me needs. I'm using the data as in the post here: Using `geom_line()` with X axis being factors for a simple example. but adding is more dates for a different year (2018). Here's the data:

hist <- data.frame(date=Sys.Date() + 0:06,
                   counts=1:7)
hist2 <- data.frame(date=Sys.Date() - 365 + 0:06,
                   counts=1:7)
histdf <- rbind(hist, hist2)
histdf <- transform(histdf, weekday=factor(weekdays(date),
                                       levels=c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')))

gsub("-.*","",histdf$date) -> histdf$year #because I just want the year for now

Using dplyr to group, here's a plot connecting all points for each year:

histdf %>%
     group_by(year) %>%
     ggplot() +
     geom_line(aes(x = weekday, y = counts, color=year, group=year))

image

I what though is connect specific days of the week for each year by different line types. So connecting M/T/W/F with a solid red line for 2018 and blue for 2019. Then a dashed line red line for TH/S/S in 2018 and blue for 2019.

Thanks very much for any help.

2

2 Answers

0
votes

Here is a solution, if I correctly understood your question.
I needed to modify a bit the code to reproduce your initial data.

library(dplyr)
library(ggplot2)

hist <- data.frame(date=Sys.Date() + 0:06,
                   counts=1:7)
hist2 <- data.frame(date=Sys.Date() - 365 + 0:06,
                    counts=1:7)
histdf <- bind_rows(hist, 
                    hist2) %>% 
  mutate(weekday = lubridate::wday(date, 
                                   label = TRUE, 
                                   locale = Sys.setlocale("LC_TIME", "English"), 
                                   abbr = FALSE),
         year = as.factor(lubridate::year(date)))

histdf %>%
  mutate(group2 = case_when(weekday %in% c("Thursday", "Saturday", "Sunday") ~ "A",
                            TRUE ~ "B")) %>% 
  ggplot(aes(x = weekday, 
             y = counts, 
             color = year, 
             group = interaction(year, group2), 
             linetype = group2)) +
  geom_line(size = 1) +
  scale_linetype_manual("Linetype legend title",
                        values = c("A" = "dashed",
                                   "B" = "solid"),
                        labels = c("A" = "TH/S/S",
                                   "B" = "M/T/W/F")) + 
  scale_color_manual("Color legend title",
                     values = c("2018" = "red",
                                "2019" = "blue")) +
  geom_point(size = 5, alpha = 0.5) # only for comprehension, remove it

Created on 2019-09-17 by the reprex package (v0.3.0)

-1
votes

I don't know the answer to all your questions, but in order to show 2 different types of lines for either MTWF or TSS you need an extra grouping variable, say "weekday.group".

histdf <- histdf %>%
  mutate(weekday.group = if_else(weekday == "thursday" | weekday == "saturday" | weekday == "sunday", "TSS", "MTWF" ))

I tried this

ggplot(histdf, aes(x = weekday, y = counts))+
  geom_line(aes(group = year, color = year, linetype = weekday.group))

but R gives an error:

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

which makes sense.

This is the closest i get:

A little different approach though,

-color for the different days

-linetype for year

-geom_point() instead of geom_line()

ggplot(histdf, aes(x=weekday, y=counts, group = year, linetype = year)) +
  geom_point(aes(color = weekday.group), stat="summary", fun.y=sum) +
  stat_summary(fun.y=sum, geom="line")

I borrowed most of the code from the link in your question.