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

时间:2019-11-05 03:26:05

标签: 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), 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"))

This is how my plot looks like

This is how my dataset looks like

1 个答案:

答案 0 :(得分:1)

您的数据采用宽(或矩阵)形式,适用于基数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"))

enter image description here