繁体   English   中英

如何将geom_point和legend放到R中的线图上?

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

这是我的 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()

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

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

在此处输入图片说明

以下是一个完整的 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 包(v0.3.0) 于 2020 年 11 月 11 日创建

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM