【问题标题】:how to get geom_point and legend onto line plot in R?如何将 geom_point 和图例放到 R 中的线图上?
【发布时间】:2020-11-11 23:03:14
【问题描述】:

这是我的 R 脚本,我一直在尝试将图例添加到线条图上,但它不起作用?有什么指导吗?我似乎也无法让 geom_point() 工作(我已经在下面取出了它的代码)。

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()

【问题讨论】:

    标签: r ggplot2 aes legend


    【解决方案1】:

    以下是完整的reprex,这意味着如果您复制和粘贴,它将完全复制如下图。你可以看到我也大大简化了你的解析;这从 url 开始,并产生更少数据争吵的情节:

    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()
    

    reprex package (v0.3.0) 于 2020 年 11 月 11 日创建

    【讨论】:

    • 谢谢!请问您能否添加一些#notes,以便我可以理解每一步代码的作用? (我很欣赏这是微不足道的,但我希望能够作为一个新的 R 用户来理解这个过程)
    • @user14620771 查看我更新中的 cmets。这不是微不足道的。我刚刚使用了一些陈旧的快捷方式来使用更少的代码获得NewData,并且需要一段时间才能在绘图/建模时意识到切换到长格式数据的好处(以及学习各种方法)。我在这里展示的方法实际上并不是切换到长格式的“最佳”方法,但它可以防止您必须安装 ' 加载额外的依赖项。
    【解决方案2】:

    您好,主要问题是您没有将数据转换为易于处理的格式

    library(dplyr)
    library(tidyr)
    library(ggplot2)
    
    Newdata %>% 
      # get data in easy to handle format
      tidyr::pivot_longer(-Time_Post_Injection, names_to = "DOSE", values_to = "VALUE") %>% 
      # plot and use the new DOSE column as group and color so you do not need one geom per line! (you can change geom_line() to geom_point also())
      ggplot2::ggplot(aes(Time_Post_Injection, VALUE, group = DOSE, color = DOSE)) +
      ggplot2::geom_line()
    

    【讨论】:

    • 谢谢!但是,我似乎得到这个错误信息出现? tidyr::pivot_longer 中的错误(-Time_Post_Injection, names_to = "DOSE", : object 'Time_Post_Injection' not found
    • 我想你已经安装了最新的“tidyr”库 - 请使用你的第一个列的名称,它应该是“Time_Post_Injection”(当我运行 str(Newdata) 时它会这样说)。我使用了您的代码和您使用的数据,所以您可能已经覆盖/操纵了一些? (只需再次从顶部运行整个代码)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 2022-11-27
    • 2017-11-29
    • 1970-01-01
    相关资源
    最近更新 更多