-2

I need to remove a certain legend entry but retain it's values for plotting. My dataframe (rdplot15) looks like this:

enter image description here

N= Unknown U= Upstream D= Downstream

I need the 0s that come along with the "null" in the direction column for plotting but I do not want the "null" to show up in the legend

My code for plotting is as follows

ggplot(rdplot15,aes(x=Date,y=Detections))+geom_line(aes(col=direction),size=1)+
  theme_bw(base_size = 16,base_family = 'serif')+
  theme(panel.grid.major = element_blank(),panel.grid.minor=element_blank())+
  scale_x_datetime(date_breaks = '1 month',date_labels = "%b",
                   limits = c(as.POSIXct('2015-01-01'),as.POSIXct("2015-12-01")))+
  ggtitle("RM 2015")+
  theme(plot.title = element_text(hjust = 0.5))+
  scale_color_manual(name= "Direction",
                     values = c("#D55E00","#009E73","#0072B2"),
                     labels=c("Downstream","Unknown","Upstream"))+
  scale_y_continuous(breaks = seq(0,10,2),expand = c(0.01,0),limits = c(0,10))

Any ideas of how to retain 0s in detections but omit the word null from the legend plot?

Reece
  • 13
  • 3
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please do not post data as images. We cannot copy/paste those values into R for testing when you do that. – MrFlick Jun 07 '21 at 03:28

1 Answers1

1

This is one potential solution:

library(tidyverse)

dat1 <- tibble(Date = seq.Date(from = as.Date("2015-03-20"), 
                               to = as.Date("2015-03-30"),
                               length.out = 30),
               direction = rep(c("N", "null", "U"), 10),
               Detections = sample(x = c(1:5),
                                   size = 30,
                                   replace = TRUE))
dat2 <- dat1 %>% 
  mutate(Detections = replace(Detections, direction == "null", 0))

  ggplot() +
  geom_line(data = dat2 %>% filter(direction != "null"),
            aes(x = Date, y = Detections,
                col = direction), size = 1) +
  geom_line(data = dat2 %>% filter(direction == "null"),
            aes(x = Date, y = Detections),
            show.legend = FALSE, col = "#009E73", size = 1) +
  theme_bw(base_size = 16, base_family = "serif") +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  ggtitle("RM 2015")+
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_color_manual(name = "Direction",
                     values = c("#D55E00","#0072B2"),
                     labels = c("Downstream","Upstream")) +
  scale_y_continuous(breaks = seq(0,10,2),
                     expand = c(0.01,0),
                     limits = c(0,10))

example_1.png

Basically, plot one set of lines (direction == "U" or "N"), then plot the "null" line (direction == "null") but leave it out of the legend. I left out the scale_x_datetime() in my example, but it shouldn't cause any issues to include it like in your original code.

Edit

I'm afraid I still don't understand what you are trying to achieve. Is this any better?

library(tidyverse)

set.seed(1)

dat1 <- tibble(Date = seq.Date(from = as.Date("2015-03-20"), 
                               to = as.Date("2015-03-31"),
                               length.out = 30),
               direction = rep(c("N", "null", "U"), 10),
               Detections = sample(x = c(1:5),
                                   size = 30,
                                   replace = TRUE))
dat2 <- dat1 %>% 
  mutate(Detections = Detections - 1) %>% 
  mutate(Detections = replace(Detections, direction == "null", 0))

ggplot() +
  geom_line(data = dat2 %>% filter(direction != "null"),
            aes(x = Date, y = Detections,
                col = direction), size = 1) +
  geom_line(data = dat2 %>% filter(direction == "null"),
            aes(x = Date, y = Detections),
            show.legend = FALSE, col = "#009E73", size = 1) +
  theme_bw(base_size = 16, base_family = "serif") +
  ggtitle("RM 2015") +
  scale_color_manual(name = "Direction",
                     values = c("#D55E00","#0072B2"),
                     labels = c("Downstream","Upstream")) +
  scale_y_continuous(breaks = seq(0, 10, 2),
                     expand = c(0.01, 0),
                     limits = c(0, 10)) +
  scale_x_date(date_breaks = "1 day", date_labels = "%d-%m-%Y",
               limits = c(as.Date("2015-03-20"), as.Date("2015-03-31"))) +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        plot.title = element_text(hjust = 0.5),
        axis.text.x = element_text(angle = 45, hjust = 1, vjust = 0.95),
        axis.title.x = element_blank())

example_2.png

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • Thnaks for the help and sorry for not including a reproducible example...Although this solution still will not work for my problem because the days with directions always start at one when I need them to start at zero...Basically I am wanting to plot the total number of directional movements each day, so there will be days throughout the year when there are 0 detections, which is why I need to retain the zeros but there is no associated direction when there are 0 detections... and I dont want that to show up on the legend. – Reece Jun 07 '21 at 11:52
  • I still don't understand sorry. I have edited my question based on your comment but I'm not sure if it is what you are trying to do. Perhaps it would be best to create another question using a minimal reproducible example and include more detailed instructions regarding the expected output. – jared_mamrot Jun 07 '21 at 23:48