【问题标题】:Adding a horizontal line to a scatterplot in ggplot2在ggplot2中的散点图中添加一条水平线
【发布时间】:2019-08-15 04:57:57
【问题描述】:

从这里开始 How to add the results of applying a function to an existing data frame?

library (tidyverse)
library (epitools)


# here's my made up data

DISEASE = c("Marco Polio","Marco Polio","Marco Polio","Marco Polio","Marco Polio",
            "Mumps","Mumps","Mumps","Mumps","Mumps",
            "Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox")
YEAR = c(2011, 2012, 2013, 2014, 2015,
         2011, 2012, 2013, 2014, 2015,
         2011, 2012, 2013, 2014, 2015)
VALUE = c(82,89,79,51,51,
          79,91,69,89,78,
          71,69,95,61,87)
AREA =c("A", "B","C")

DATA = data.frame(DISEASE, YEAR, VALUE,AREA)


DATA %>%
    mutate(POPN = case_when(
        AREA == "A" ~ 2.5,
        AREA == "B" ~ 3,
        AREA == "C" ~ 7,
        TRUE ~ 0)) %>%
    group_by(DISEASE,AREA,POPN) %>%
    count(AREA) %>%
    mutate(res = list(pois.byar(n, POPN))) %>%
    unnest()

给我我需要的东西。

我想做的是将其绘制在散点图上,水平线等于我计算的比率之一。而不是手动添加。

我认为这可能有效

DATA%>%filter(DISEASE== "Chicky Pox")%>%
  ggplot(aes(x=AREA, y=rate)) +geom_point() +
  geom_hline(yintercept=20, linetype="dashed", color = "red")

它的作用。 它在 20 处给了我一条线。 但是我怎样才能让它给我一条等于区域 A 的速率值的线(例如)。 这样您就可以快速查看哪些费率高于或低于 A 的费率。

再次。抱歉,这很简单。不过来晚了……

【问题讨论】:

    标签: r ggplot2 geom-hline


    【解决方案1】:

    您可以在aes() 中放置一些简单的子集条件,这意味着只要条件不太复杂,这非常简单:

    DATA %>%
        filter(DISEASE== "Chicky Pox") %>%
        ggplot(aes(x=AREA, y=rate)) +
        geom_point() +
        geom_hline(aes(yintercept=rate[AREA == "A"]), 
                   linetype="dashed", color = "red")
    

    【讨论】:

    • 再次感谢!这又是一个“轰轰烈烈”的时刻。
    猜你喜欢
    • 2017-01-28
    • 2019-05-15
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 2016-03-25
    相关资源
    最近更新 更多