1
votes

I am trying to compare the durations of different components in a cycle, over several time points (weeks). Ideally, the plot should look like this:

Cycle time with data labels as required Cycle time with data labels as required

I am able to generate the plot below (code follows), but am unable to add data labels to the chart.

Plot with geom_path() that requires data labels

Plot with geom_path() that requires data labels

I plot this from the following:

  1. df1: cumulative mean durations per week (geom_path() and geom_point() read this)
week variable value

23   Step 1   0.14

24   Step 1   0.21

23   Step 2   0.25

24   Step 2   0.35

23   Step 3   0.53

24   Step 3   0.65
  1. df2: mean duration per week (data labels come from this)
week variable value

23   Step 1   0.14

24   Step 1   0.21

23   Step 2   0.11

24   Step 2   0.14

23   Step 3   0.28

24   Step 3   0.30

My ggplot code is:

ggplot(df1, aes(x=value, y=week))+
  geom_path(mapping=NULL, data=df1,stat="identity", lineend="butt")+
  geom_point(aes(colour=variable, size=0.5))+
  labs(title="Average cycle time, by components by week",
       x="Number of days",
       y="Week in the year")+
  theme(axis.text=element_text(size=9),
        axis.title=element_text(size=12,face="bold"))+
  #annotate(geom="text",df2, aes(label=value)),
  #         hjust=1, vjust=-0.1)

If I try the annotate line, I get a blank plot, with the following error:

Error in is.finite(x) : default method not implemented for type 'list'

I feel it might have something to do with how I specified df1 and df2?

Any thoughts on how to add these data labels?

2

2 Answers

0
votes

Instead of using annotate (which has no data argument, hence the error) I would suggest to use geom_text. However, instead of using both dfs my prefered approach would be to join both datasets:

library(ggplot2)
library(dplyr)

df1 <- read.table(text = "week variable value
23   Step_1   0.14
24   Step_1   0.21
23   Step_2   0.25
24   Step_2   0.35
23   Step_3   0.53
24   Step_3   0.65", header = TRUE)

df2 <- read.table(text = "week variable value
23   Step_1   0.14
24   Step_1   0.21
23   Step_2   0.11
24   Step_2   0.14
23   Step_3   0.28
24   Step_3   0.30", header = TRUE)

df3 <- df1 %>% 
  left_join(df2, by = c("week", "variable"), suffix = c("", "_lab"))
  
ggplot(df3, aes(x=value, y=week))+
  geom_path(mapping=NULL, data=df1,stat="identity", lineend="butt")+
  geom_point(aes(colour=variable, size=0.5))+
  labs(title="Average cycle time, by components by week",
       x="Number of days",
       y="Week in the year")+
  theme(axis.text=element_text(size=9),
        axis.title=element_text(size=12,face="bold"))+
  geom_text(aes(label=value_lab),
           hjust=1, vjust=-0.1)

Just in case. If you want to stick with both dfs:

ggplot(df1, aes(x=value, y=week))+
  geom_path(mapping=NULL, data=df1,stat="identity", lineend="butt")+
  geom_point(aes(colour=variable, size=0.5))+
  labs(title="Average cycle time, by components by week",
       x="Number of days",
       y="Week in the year")+
  theme(axis.text=element_text(size=9),
        axis.title=element_text(size=12,face="bold"))+
  geom_text(data = df2, aes(label=value),
           hjust=1, vjust=-0.1)
0
votes

Your data.frame, and you add the first step:

df = data.frame(week=c(23,24,23,24,23,24),
variable=rep(c("Step 1","Step 2","Step 3"),each=2),
value=c(0.14,0.21,0.11,0.14,0.28,0.30))

df$week = factor(df$week)

df = rbind(data.frame(week=unique(df$week),variable="Step 0",value=0),df)

What you have on the second data frame is correct. You can also make the plot using two data frames. Below I use the function cumsum() to calculate the cumulative value on the fly.

You can use geom_text() or annotate() but when your labels are close, you need to repel them so I used ggrepel below:

library(ggrepel)

ggplot(df,aes(x=cumsum(value),y=week)) + 
geom_line(aes(group=week),col="black") +
geom_point(aes(col=variable)) +
geom_text_repel(data=subset(df,variable!="Step 0"),
aes(x=cumsum(value)-value/2,y=week,label=value,group=week))
theme_minimal()

enter image description here