如何将补充类型类型添加到ggplot中 [英] How to add Supplement type type into the ggplot

查看:227
本文介绍了如何将补充类型类型添加到ggplot中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将每行的信息添加到绘图中,例如,红色的虚线指的是男性
twodash线的绿色的90%CI,指的是女性的90%CI
黑色的longdash线表示所有人口的90%CI

I was wondering how I can add the information of each line into the Plot, For example, dashed line in red color refers to 90%CI for male twodash line in green color refers to 90%CI for female longdash line in black color refers to 90%CI for all population

ggplot(mtcars, aes(mpg, disp)) + 
      geom_point(aes(colour=factor(vs), 
      fill = factor(vs)), shape=21, size = 4) + 
      scale_fill_manual(values=c("blue", "pink")) + 
      scale_colour_manual(values=c("black", "black"))+
      geom_hline(yintercept=200, linetype="dashed", color = "darkred")+
      geom_hline(yintercept=250, linetype="dashed", color = "darkred")+
      geom_hline(yintercept=210, linetype="twodash", color = "green")+
      geom_hline(yintercept=215, linetype="twodash", color = "green")+
      geom_hline(yintercept=279, linetype="longdash", color = "black")+
      geom_hline(yintercept=280, linetype="longdash", color = "black")


推荐答案

Axeman的建议是恰当的。用想要的 yintercept s构建一个data.frame,并将其用于行的分组。一次调用 geom_hline 所需的 aes 将会生成图表和一个有意义的图例。

The advice from Axeman is apt. Build a data.frame with the wanted yintercepts and a factor for the grouping of the lines. A single call to geom_hline will the needed aes will generate the plot and a meaningful legend.

library(ggplot2)

lt <- data.frame(yint = c(200, 250, 210, 215, 279, 280),
                 grp  = factor(c(1, 1, 2, 2, 3, 3),
                               levels = 1:3,
                               labels = c("Group 1", "Group 2", "Group 3")))


ggplot(mtcars, aes(mpg, disp)) + 
      geom_point(aes(colour=factor(vs), 
      fill = factor(vs)), shape=21, size = 4) + 
      scale_fill_manual(values=c("blue", "pink")) + 
      scale_colour_manual(values=c("black", "black"))+
      geom_hline(data = lt,
                 mapping = aes(yintercept = yint, linetype = grp))

这篇关于如何将补充类型类型添加到ggplot中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆