Blake Shurtz
Blake Shurtz

Reputation: 331

Why do I get the weird purple line in ggplot?

my code is below. It should replicate. This is code for downloading the data, wrangling the data, building the loess line and then adding the graph.

library(tidyverse)
library(macleish)
library(lubridate)

mydata <- whately_2015
mydata <- whately_2015 %>% 
  dplyr::select(temperature, when) %>%
  mutate(when = as_date(mydata$when), temperature = temperature * (9/5)+32)

mydata$seasons <- as.character(c(rep("Winter", 11232), rep("Spring", 24624-11232), rep("Summer", 38161-24624), rep("Autumn", 50976-38161), rep("Winter", 52560-50976)))

start_dates <- mdy(c("03/20/2015","06/21/2015", "09/23/2015", "12/21/2015"))
end_dates <- mdy(c("03/19/2015", "06/20/2015", "09/22/2015", "12/20/2015", "12/31/2015"))
Season = c("Spring Equinox- 3/20", "Summer Solstice- 6/21", "Autumn Equinox- 9/23", "Winter Solstice- 12/21")
start_dates_gg <- mdy(c( "01/01/2015", "03/20/2015","06/21/2015", "09/30/2015"))
Season_gg = c("Winter Solstice- 12/21", "Spring Equinox- 3/20", "Summer Solstice- 6/21", "Autumn Equinox- 9/23")

mydata$datenum <- as.numeric(mydata$when)
model10 <- loess(temperature ~ datenum, data=mydata, span = 0.10)
mydata$smoothed10 <- predict(model10)

ggplot(data=mydata, aes(x=when)) +
 geom_line(aes(y=smoothed10, col=seasons)) +
  theme_bw() +
  annotate(geom="text", x=start_dates_gg, y=c(10, 27, 75, 60), label=Season_gg, hjust=0, col = c("#C77Cff","#7CAE00","#00BFC4","#F8766D")) + 
  theme(legend.position="bottom") + 
  labs(title = "Weather and Seasons", x = "Date", y= "Temperature (F)")

I get a weird graph that looks like this. Why the weird purple line running across the screen? The purple line is just meant for the Winter season. enter image description here

Upvotes: 1

Views: 157

Answers (2)

Tung
Tung

Reputation: 28331

You just need to add group = 1 to your geom_line() call to let ggplot know that all the points belong together and should be connected with a single line.

ggplot(data = mydata, aes(x = when)) +
  geom_line(aes(y = smoothed10, col = seasons, group = 1)) + # add group here
  theme_bw() +
  annotate(geom = "text", x = start_dates_gg, y = c(10, 27, 75, 60), 
           label = Season_gg, hjust = 0, col = c("#C77Cff", "#7CAE00", "#00BFC4", "#F8766D")) +
  theme(legend.position = "bottom") +
  labs(title = "Weather and Seasons", x = "Date", y = "Temperature (F)")

Created on 2018-10-23 by the reprex package (v0.2.1.9000)

Upvotes: 2

Marius
Marius

Reputation: 60060

If you just have a colour mapping, by default ggplot will connect all points with the same colour. You can stop that by providing an additional group mapping that specifies which points should be connected, e.g.:

ggplot(data=mydata, aes(x=when)) +
    geom_line(aes(y=smoothed10, col=seasons, group = lubridate::year(when))) +
    theme_bw() +
    annotate(geom="text", x=start_dates_gg, y=c(10, 27, 75, 60), label=Season_gg, 
             hjust=0, col = c("#C77Cff","#7CAE00","#00BFC4","#F8766D")) + 
    theme(legend.position="bottom") + 
    labs(title = "Weather and Seasons", x = "Date", y= "Temperature (F)")

Upvotes: 0

Related Questions