Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 我想将图例添加到我的ggplot中,并在图例中给出两个单独的标题,一个用于绿线,另一个用于红线_R_Ggplot2_Tidyverse - Fatal编程技术网

R 我想将图例添加到我的ggplot中,并在图例中给出两个单独的标题,一个用于绿线,另一个用于红线

R 我想将图例添加到我的ggplot中,并在图例中给出两个单独的标题,一个用于绿线,另一个用于红线,r,ggplot2,tidyverse,R,Ggplot2,Tidyverse,我只是想在我的图表中添加图例。我尝试过各种方法,但迄今为止还没有成功。有一条红色实线、两条红色虚线、一条绿色实线和两条绿色虚线以及圆圈。对于红线,我想给它们一个单独的标题,对于绿线,我想在图例中给它们一个单独的标题 VPCplot <- ggplot(Tab, aes(x=TIME, y=DV))+ geom_point (size=2.5, colour='black',shape=1)+ geom_line(data=VPC1, aes(x=TIME, y=ObservedMean),

我只是想在我的图表中添加图例。我尝试过各种方法,但迄今为止还没有成功。有一条红色实线、两条红色虚线、一条绿色实线和两条绿色虚线以及圆圈。对于红线,我想给它们一个单独的标题,对于绿线,我想在图例中给它们一个单独的标题

VPCplot <- ggplot(Tab, aes(x=TIME, y=DV))+ geom_point (size=2.5, colour='black',shape=1)+
geom_line(data=VPC1, aes(x=TIME, y=ObservedMean), color='red', size=1)+
geom_line(data=VPC1, aes(x=TIME, y=observedpercentileLL), color='red', size=1, linetype = "dashed")+
geom_line(data=VPC1, aes(x=TIME, y=observedpercentileUL), color='red', size=1, linetype = "dashed")+
geom_line(data=VPC1, aes(x=TIME, y=SimulationMean), color='green', size=1)+
geom_line(data=VPC1, aes(x=TIME, y=simulatedpercentileLL), color='green', size=1, linetype = 
"dashed")+
geom_line(data=VPC1, aes(x=TIME, y=simulatedpercentileUL), color='green', size=1, linetype = 
"dashed")+
xlab("Time (Hours)") +
ylab("THC Concentration (ng/mL)")+
ggtitle("VPC_Two Compartment PK Model")

VPCplot + theme(panel.background = element_rect(fill = "white"),
             plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm"),
             plot.background = element_rect(fill = "grey90",colour = "black",size = 5)) +
theme(axis.text = element_text(colour = "red", size = rel(1)),
    axis.title = element_text(size = 14,face = "bold"),
    axis.title.x= element_text(size = 18),  
    axis.title.y= element_text(size = 18),
    plot.title = element_text(hjust = 0.5,size = 18,face = "bold"))
VPCplot您的数据是宽(或矩阵)形式,对于base R很有效,但对于ggplot,最好将其转换为long

由于我没有您的数据,我模拟了下面的一些数据,并对转换发生的位置进行了评论

理想情况下,您应该执行dput(Tab)和dput(VPC1),将这些数据帧作为代码的一部分,以便其他人可以尝试给您提供建议

library(dplyr)
library(tidyr)

#simulate some data
TIME = 1:50
SHIFT = 0.2
Tab = data.frame(TIME=sample(TIME,100,replace=T))
Tab$DV = 1/Tab$TIME + rnorm(100,0,0.2)
VPC1 = data.frame(
       TIME = TIME,
       SimulationMean = 1/TIME,
       simulatedpercentileUL = 1/TIME+0.05,
       simulatedpercentileLL = 1/TIME-0.05,
       ObservedMean = 1/TIME + SHIFT,
       observedpercentileUL = 1/TIME + SHIFT +0.05,
       observedpercentileLL = 1/TIME + SHIFT -0.05
)

# here we convert it into long, and time is the identifying variable
# we also introduce a class observed or simulated so that
# the UL,LL and mean lines will have same colour
VPC1 <- VPC1 %>% pivot_longer(-TIME) %>% 
mutate(type=ifelse(grepl("Observed",name,ignore.case=TRUE),"Observed","Simulated"))

# create separate data frames for UL, LL and the means (VPC1)
UL <- VPC1 %>% filter( grepl("percentileUL",name))
LL <- VPC1 %>% filter( grepl("percentileLL",name))
VPC1 <- VPC1 %>% filter( !grepl("percentile",name))

# plot, very similar to what you have before
VPCplot <- ggplot(Tab, aes(x=TIME, y=DV))+ 
geom_point (size=2.5, colour='black',shape=1)+
geom_line(data=VPC1, aes(x=TIME, y=value,col=type))+
geom_line(data=UL, aes(x=TIME, y=value,col=type),linetype="dotted")+
geom_line(data=LL, aes(x=TIME, y=value,col=type),linetype="dotted")+
### specify the colors here
scale_color_manual(name="Type",values=c("red","green"))
库(dplyr)
图书馆(tidyr)
#模拟一些数据
时间=1:50
移位=0.2
Tab=data.frame(时间=sample(时间,100,替换=T))
Tab$DV=1/Tab$TIME+rnorm(100,0,0.2)
VPC1=数据帧(
时间=时间,
模拟平均值=1/次,
模拟百分位数=1/次+0.05,
模拟百分位数=1/次-0.05,
观测平均值=1/次+班次,
观察百分位数=1/次+班次+0.05,
观察百分位数=1/次+移位-0.05
)
#这里我们将其转换为long,时间是识别变量
#我们还引入了一个观察或模拟的类,以便
#UL、LL和平均线的颜色相同
VPC1%pivot\u更长(-TIME)%%>%
mutate(type=ifelse(grepl(“观察到的”,name,ignore.case=TRUE),“观察到的”,“模拟的”))
#为UL、LL和平均值(VPC1)创建单独的数据帧
UL%过滤器(grepl(“百分位数”,名称))
LL%过滤器(grepl(“百分位数”,名称))
VPC1%过滤器(!grepl(“百分位数”,名称))
#情节,和你以前的非常相似

VPCplot请不要发布您数据的图像,我们无法加载这些图像以进行尝试。