为ggplot中的两条水平线添加单个图例

时间:2019-12-06 15:48:07

标签: r ggplot2 plot simulation montecarlo

我对ggplot2经验很少。我正在尝试使用以下代码绘制覆盖率和同类群组的大小:

library("reshape2")
library("ggplot2")
library(latex2exp)
CP1 <-c(0.953,0.942,0.947,0.958)
CP2 <- c(0.937,0.952,0.955,0.957)
cohort <- c(500,1000,5000,10000)
mdata <- data.frame(rate1=CP1,rate2=CP2,cohort.size=cohort)

mydata <- melt(mdata,id='cohort.size',value.name="CP")
ggplot(mydata , aes(x=cohort.size, y=CP)) +
  geom_line(size=1,aes(colour=variable)) +
  geom_point( size=4, shape=0)+ coord_cartesian(ylim = c(0,1)) +
  scale_x_continuous(breaks=c(500,1000,5000,10000))+
  scale_color_discrete(labels = unname(TeX(c(" $\\r_1$", "$\\r_2$")))) +
  geom_hline(yintercept =c(0.936,0.964) ,linetype="dashed") + 

  theme(legend.title = element_blank(), axis.title.x = element_text(color="#993333", size=14, face="bold"), 
        axis.title.y = element_text(color="#993333", size=14, face="bold"),
        plot.title = element_text(color="#993333", size=14, face="bold"),
        legend.position = c(.85, .85),
        legend.justification = c("right", "top"),
        legend.box.just = "right",
        legend.margin = margin(6, 6, 6, 6),legend.text=element_text(size=20)) + xlab("Cohort Size") + ylab("Coverage Proability")+

  annotate("text",
           x = 8700,
           y = 0.68, 
           label =expression(bold(paste("MN=57% \n AB=38% \n XYZ=5%" ))),parse = TRUE,size=5)

我有三个问题: 1.运行代码时,我得到警告;我该如何解决。 2.有两条水平的黑色虚线,我只想用一个图例来代表'95%CL'。 3.我觉得代码太多了,有没有更简单的方法可以仅使用ggplot2编写它。

谢谢!

1 个答案:

答案 0 :(得分:2)

我无法安装 private static Connection getOracleConnection() throws Exception { String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@myhost.com:1521:mydb"; String username = "myuser"; String password = "mypasswd"; Class.forName(driver); Connection connection = DriverManager.getConnection(url, username, password); return connection; } 。没有此软件包,您只需尝试一下,我认为所有三个问题都可以解决:

latex2exp

enter image description here