1

Data: Height was recorded daily

I want to plot the Height of my Plants (Plant A1 - Z50) in single Plots, and i want to Highlight the current Year.

So i made a Subset of each Plant and a subset for the current year (2018)

Now i need a Plot with the total record an the highlighted Data from 2018

 dput(Plant)
structure(list(Name = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 3L), .Label = c("Plant A1", "Plant B1", "Plant C1"), class = "factor"), 
    Date = structure(c(1L, 4L, 5L, 7L, 1L, 4L, 6L, 1L, 2L, 3L
    ), .Label = c("    2001-01-01", "    2001-01-02", "    2001-01-03", 
    "    2002-01-01", "    2002-02-01", "    2019-01-01", "    2019-12-31"
    ), class = "factor"), Height_cm = c(91, 106.1, 107.4, 145.9, 
    169.1, 192.1, 217.4, 139.8, 140.3, 140.3)), .Names = c("Name", 
"Date", "Height_cm"), class = "data.frame", row.names = c(NA, 
-10L))

Plant_A1 <- filter(Plant, Name == "Plant A1")
Current_Year <- as.numeric("2018")
Plant_A1_Subset <- filter(Plant_A1, format(Plant_A1$Date, '%Y') == Current_Year)


ggplot(data=Plant_A1,aes(x=Plant_A1$Date, y=Plant_A1$Heigth)) + 
  geom_point() + 
  geom_smooth(method="loes", level=0.95, span=1/2, color="red") +
  labs(x="Data", y="Height cm")

Now i don't know how to put my new Subset for 2018(Plant_A1_Subset) into this graph.

morgan121
  • 2,213
  • 1
  • 15
  • 33
  • 1
    @Oliver, I'm vaguely guessing the question is about highlighting whatever is in the var `Current_year`, and less about `geom_smooth`. – deepseefan Aug 11 '19 at 12:47
  • @Melanie perhaps could you clarify what sort of output you're expecting? – deepseefan Aug 11 '19 at 12:54
  • @deepseefan, the question is about highlighting only a subset of your points, given a condition on your dataset. This is similar to the question of performing a `geom_smooth` on a subset of you data, and these can be handled in the same manner. However I've added an answer with the more common and simpler method for this specific case. – Oliver Aug 11 '19 at 12:58

1 Answers1

0

As noted, this question has a duplicate with an answer in this question.

That said here's likely the most common way of handling your problem.

In ggplot2 future calls inherits any arguments passed into aes of the ggplot(aes(...)) function. Thus the plot will always use these arguments in future ggplot functions, unless one manually overwrites the arguments. However we can solve your problem, by simply adding an extra argument in the aes of geom_point. Below I've illustrated a simple way to achieve what you might be looking for.

Specify the aes argument in individual calls

The first method is likely the most intuitive. aes controls the the plotted parameters. As such if you want to add colour to certain points, one way is to let the aes be individual to the geom_point and geom_smooth argument.

library(ggplot2)
library(lubridate) #for month(), year(), day() functions
current_year <- 2018
ggplot(data = Plant_A1, aes(x = Date, y = Heigth)) +
    #Note here, colour set in geom_point
    geom_point(aes(col = ifelse(year(Date) == current_year, "Yes", "No"))) + 
    geom_smooth(method="loess", level=0.95, 
                span=1/2, color="red") +
    labs(x="Data", y="Height cm",
         col = "Current year?") #Specify legend title for colour

Note here that i have used the inheritance of the aes argument. Simply put, the aes will check the names within data, and if it can find it, it will use these as variables. So there is no need to specify data$....

Community
  • 1
  • 1
Oliver
  • 8,169
  • 3
  • 15
  • 37
  • I'm getting an error `Error in as.POSIXlt.default(x, tz = tz(x)) : do not know how to convert 'x' to class “POSIXlt”`. I've submitted an edit `dput(Plant)` to the question so you check if the error is on my end only. – deepseefan Aug 11 '19 at 13:10
  • 1
    I see I've spelled `Date` as `date` in `geom_point`. That is likely the issue. (Updated my answer to correct this error) – Oliver Aug 11 '19 at 13:16
  • No problems. Make sure to upvote and/or mark my answer as 'answering the question', if you believe i have answered provided the best answer. This can be changed if someone else provides a better answer in the future, and improved the visibility of your question for others with similar problems. Best of luck! – Oliver Aug 11 '19 at 17:01