Working script no longer working after reopening R?

This may sound weird, but I was happy with my script and line graph, and saved it and closed it. This is how it looked:

I decided to go back to edit the label "Year and week" to only "Year", but now I can't get the second blue line to work at all with the exact same script? I'm very confused.

I get this warning message:
1: Removed 3 rows containing missing values (geom_path).
2: Removed 1026 rows containing missing values (geom_path).

Previously it only removed 3 values which were fine, but now it's removing literally all the rows as there are 1026 obs. in this data. It looks like this now with the exact same script:

This is my script:

library(openxlsx)
library(ggplot2)
library(tidyverse)
Vekt <- read.xlsx("../data/Vektny.xlsx")
colors <- c("Fresh salmon" = "darkred", "Frozen" = "steelblue")
Vekt %>% 
  mutate(Week = as.Date(paste0(Week, 1), "%YW%W%w")) %>% 
  ggplot(aes(x = Week)) +
  geom_line(aes(y = Fresh, group = 1, color = "Fresh salmon")) + 
  geom_line(aes(y = Frozen, group = 1, color = "Frozen salmon"), linetype = "twodash") +
  scale_x_date(date_labels = '%Y') +
  scale_color_manual(values=colors) +
  ggtitle("Salmon export") + theme(plot.title = element_text(size = 12, hjust = 0.5)) +
  labs(x = "Year",
       y = "Ton",
       color = "Type")

Anyone know what the issue might be? I'm new to this so it might be very obvious :sweat_smile:

There is nothing evident in your code, we are going to need sample data (on a copy paste friendly format) that reproduces the issue or a link to the xlsx file you are using.

This is how it looks. It's basically year 2000-2019 with 52/53 weeks in every year that adds up to 1026 weeks. Thing is it was working just fine before so I'm not sure how the data would've changed.

'data.frame':	1026 obs. of  3 variables:
 $ Week  : chr  "2000W01" "2000W02" "2000W03" "2000W04" ...
 $ Fresh : num  3728 4054 4043 3730 3831 ...
 $ Frozen: num  383 216 633 393 453 265 344 542 490 738 ...

Maybe you performed some data cleaning step in your previous R session that you have forgot to include in your script and that is why isn't working now, but to be able to help you we are going to need sample data that reproduces your issue, not a str() print, please try to provide a REPRoducible EXample (reprex)

Hi @adnben!

Since the Frozen Salmon line is the one that’s missing, it makes sense to start looking for typos in the code that produces that line:

geom_line(aes(y = Frozen, group = 1, color = "Frozen salmon"), linetype = "twodash")

What I’m noticing there is that you’ve mapped color to “Frozen salmon”, but your named vector of colors is:

colors <- c("Fresh salmon" = "darkred", "Frozen" = "steelblue")

(“Frozen” instead of “Frozen salmon”)

As @andresrcs suggested, it seems likely that you had a slightly different color vector active in your last session (maybe due to working on the plot).

You're absolutely right, can't believe I missed that, thank you!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.