Как использовать числовой ввод для управления положением вертикальной линии над моей гистограммой? - PullRequest
0 голосов
/ 26 октября 2018

поэтому у меня есть гистограмма, но я хочу добавить либо скалярную строку, либо текстовое поле ввода, чтобы вертикальная линия появлялась с указанной частотой.Я не уверен, как это сделать.Я попытался использовать abline, но я получаю сообщение об ошибке, говорящее, что график не был создан.

Ниже приведен мой код к гистограмме.

---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    social: menu
    source_code: embed
---

```{r setup, include=FALSE}
library(ggplot2)
library(plotly)
library(maps)
knitr::opts_chunk$set(message = FALSE)
```

Column {data-width=600}
-------------------------------------

### Historgram of Victim Age

```{r}
#Loading the data
df <- read.delim2("https://www.chapelhillopendata.org/explore/dataset/police-incident-reports-written/download/?format=csv&refine.date_of_report=2017&timezone=America/New_York&use_labels_for_header=true", sep = ";")

#Converting `Victim.Age` from factor to numeric 
df <- df %>% 
        filter(!is.na(Victim.Age)) %>%
        mutate(
                Victim.Age = as.numeric(levels(Victim.Age))[Victim.Age])
```

```{r}
# provide a custom tooltip to plotly with the county name and actual rate
p <- qplot(df$Victim.Age,
           geom = "histogram",
           binwidth = 0.5,
           main = "Histogram for Victim Age",
           xlab = "Age (Yrs)",
           ylab = "Frequency", 
           fill = I("blue"),
           col = I("red"),
           alpha = I(0.2),
           xlim = c(0, 80))

# just show the text aesthetic in the tooltip
ggplotly(p, tooltip = "text")

1 Ответ

0 голосов
/ 26 октября 2018

Я не знаю, правильно ли я понял, что вы хотите, но вертикальная линия в ggplot2 может быть сделана с помощью geom_vline().

Вертикальная линия в среднем возрасте, например, включена следующим образом:

p1 <- p + geom_vline(aes(xintercept = mean(Victim.Age, na.rm = TRUE)))

ggplotly(p1, tooltip = "text")
...