Search code examples
rggplot2aeslegend

how to get geom_point and legend onto line plot in R?


This is my R-script, I've been trying to include a legend onto the line plot but it isn't working? Any guidance? I also can't seem to get the geom_point() working either (I've taken the code for it out below).

library(ggsignif)
library(readxl)
library(svglite)
library(tidyverse)
library(ggplot2)
library(tidyr)
library(dplyr)
url <-'https://static-content.springer.com/esm/art%3A10.1038%2Fs41586-020-2850-3/MediaObjects/41586_2020_2850_MOESM10_ESM.xlsx'
temp <-tempfile()
download.file(url, temp, mode='wb')
myData <- read_excel(path=temp, sheet = "ExFig.5f")
names(myData) <- NULL
view(myData)
Time_post_inj <- (myData[1])
Time_post_inj <- Time_post_inj[-c(1),]
dose_450_ug <- (myData[2])
dose_450_ug <- dose_450_ug[-c(1),]
dose_150_ug <- (myData[4])
dose_150_ug <- dose_150_ug[-c(1),]
dose_100_ug <- (myData[6])
dose_100_ug <- dose_100_ug[-c(1),]
dose_50_ug <- (myData[8])
dose_50_ug <- dose_50_ug[-c(1),]
colnames(Time_post_inj) <-c("Time_Post_Injection")
colnames(dose_450_ug) <-c("dose_450_µg")
colnames(dose_150_ug) <-c("dose_150_µg")
colnames(dose_100_ug) <-c("dose_100_µg")
colnames(dose_50_ug) <-c("dose_50_µg")
Newdata <-data.frame(Time_post_inj, dose_450_ug, dose_150_ug, dose_100_ug, dose_50_ug)
Newdata$Time_Post_Injection <-as.numeric(Newdata$Time_Post_Injection)
Newdata$dose_450_µg <-as.numeric(Newdata$dose_450_µg)
Newdata$dose_150_µg <-as.numeric(Newdata$dose_150_µg)
Newdata$dose_100_µg <-as.numeric(Newdata$dose_100_µg)
Newdata$dose_50_µg <-as.numeric(Newdata$dose_50_µg)
str(Newdata)
ggplot(data=Newdata, aes(x=Time_Post_Injection, y=hCD4_occupancy, group = 1)) + geom_line(aes(y=dose_450_µg)) + geom_line(aes(y=dose_150_µg)) + geom_line(aes(y=dose_100_µg)) + geom_line(aes(y=dose_50_µg))
Newdata
tidyr::pivot_longer(Time_Post_Injection, names_to = "DOSE", values_to = "VALUE") %>% 
ggplot2::ggplot(aes(Time_Post_Injection, VALUE, group = DOSE, color = DOSE)) + ggplot2::geom_line()

Solution

  • The following is a full reprex, meaning that if you copy and paste, it will reproduce the plot exactly as below. You can see I have simplified your parsing considerably too; this starts with the url and produces the plot with a lot less data wrangling:

    library(ggplot2) # Only load packages you really need
    
    # This format is a handy way of keeping a long string on a single page
    url <- paste0("https://static-content.springer.com/esm/art%3A10.",
                 "1038%2Fs41586-020-2850-3/MediaObjects/41586_2020",
                 "_2850_MOESM10_ESM.xlsx")
    
    temp <- tempfile()
    
    download.file(url, temp, mode = 'wb')
    
    # Instead of loading an entire library to use one function, we can
    # access read_excel by doing readxl::read_excel
    myData  <- readxl::read_excel(temp, sheet = "ExFig.5f")
    
    # This single line subsets the data frame to chop out the first row
    # and the empty columns. It also converts all columns to numeric
    NewData <- as.data.frame(lapply(myData[-1, -c(3, 5, 7)], as.numeric))
    names(NewData) <-c("Time_Post_Injection", "dose_450_ug", 
                       "dose_150_ug", "dose_100_ug", "dose_50_ug")
    
    
    # This switches your data to long format, which helps ggplot to work
    # We put all the values in one column and have the dosages as labels
    # in another column instead of having multiple columns. This allows us
    # to map Color to the dosages.
    NewData <- cbind(NewData[1], stack(NewData[-1]))
    
    # Now we just tell ggplot to map colours to ind 
    ggplot(NewData, aes(x = Time_Post_Injection, y = values, color = ind)) +
      geom_line() +
      geom_point() +
      scale_color_discrete(name = "Dose") +
      labs(x = "Time Pist Injection") +
      theme_bw()
    

    Created on 2020-11-11 by the reprex package (v0.3.0)