0

so I have a histogram, but I want to add either a scalarbar or textinput box so that a vertical line will appear at the designated frequency. I'm not sure how to do this. I've tried using abline but I get an error saying the plot hasn't been created.

Below is my code to the histogram.

---
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")
handavidbang
  • 593
  • 1
  • 6
  • 19

1 Answers1

0

I don't know if i understood correctly what you want, but a vertical line in ggplot2 can be made with geom_vline().

A vertical line in the average Age, for example, is included like this:

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

ggplotly(p1, tooltip = "text")
Bruno Pinheiro
  • 964
  • 8
  • 20