Adding Vertical Line in Plot Ggplot

Adding Vertical Lines for Points in ggplot

I think you were on the right track with using geom_segement() I created a reproducible example below with some fake data.

library(ggplot2)

set.seed(43)
Dates<-seq.Date(as.Date("2019/01/01"), as.Date("2019/12/31"), by="day")
Exits<-rnorm(length(Dates), 140000, 5000)
Enter<-rnorm(length(Dates), 140000, 8000)
Policy.Date<-c(as.Date("2019/02/07"), as.Date("2019/5/31"), as.Date("2019/8/12"), as.Date("2019/10/22"))
tmp<-c(1:4)
Policy<-paste("Policy", tmp, sep=" ")

DF.Exit<-data.frame(date=Dates, count=Exits)
DF.Exit[,"flujo"]<-"Exit"
DF.Enter<-data.frame(date=Dates, count=Enter)
DF.Enter[,"flujo"]<-"Enter"

Flow<-rbind(DF.Enter, DF.Exit)
policies<-data.frame(type_events=Policy, dates=Policy.Date)

chart7<-ggplot()+
geom_line(data=Flow,
aes(x=date, y=count,
color=flujo)) +
geom_point(data = policies,
aes(x=dates, y=200000, color=type_events))+
geom_segment(data=policies,
aes(x=dates, xend=dates, y=0, yend=200000))+
scale_x_date(date_minor_breaks = "1 month",
date_labels = "%Y (%b)")+
annotate("text", x=policies$date, y=210000, label=policies$type_events)+
labs(color="Type of Flow")+
ggtitle("Number of Exits and Entrances, by Month, 2017-2021")+
xlab("Date")+
ylab("Number or People")

chart7

Here is what the result looks like Example Chart

Add vertical lines connecting point to a horizontal line in a plot in R

You can add vertical lines with type = "h", and then add the points separately

plot(y=lag[2:N],x=1:(N-1), xlab="lag",ylab="Autocorrelation",ylim=c(-1,1), col="black", type = "h") 
points(y=lag[2:N],x=1:(N-1), xlab="lag", ylim=c(-1,1), pch=16,col="red", type = "p")

Adding vertical line in plot ggplot

You can add vertical line with geom_vline(). In your case:

+ geom_vline(xintercept=2)

If you want to see also number 0.5 on your y axis, add scale_y_continuous() and set limits= and breaks=

+ scale_y_continuous(breaks=c(0.5,1,2,3,4,5),limits=c(0.5,6))

ggplot - adding vertical lines

You can try this:

# convert index to date-time format; this makes x-axis continuous rather than
# categorical, so you don't have to specify the group for geom_line.
RSI_data$index <- as.POSIXct(as.character(RSI_data$index))

ggplot(RSI_data,
aes(x = index, y = EURUSD.Close)) +
geom_line() +
geom_vline(data = subset(RSI_data, longSignal == 1), # filter data source
aes(xintercept = index),
size = 2, colour = "red")

plot

Include vertical line in ggplot based on value in other column in ggplot

To add a red line for any week that has a protest equal to 1, you can use the following code

ggplot(berlin_week, aes(x=week, y=count)) +
geom_line() +
geom_vline(xintercept = berlin_week$week[berlin_week$protest == 1], color = "red")

example1

how can i add vertical line in the plot (R ggplot2)

You use xintercept = vline inside aes(). aes() is for mapping columns from your data to graphical aesthetics, so ggplot is looking in your data for a column named vline, which doesn't exist.

vline isn't in your data frame, so don't put it inside aes(). geom_vline(xintercept = vline) should work fine.

df %>% 
ggplot(aes(x = mdate, y = mvalue)) +
geom_line() +
geom_vline(xintercept = vline)

Sample Image

In R ggplot2 ,how to draw vertical lines automatically

Here is a solution, that my help you.

library(tidyverse)

plot_data %>%
ggplot(aes(x=mperiod,y=amount))+
geom_bar(stat='identity')+
geom_vline(aes(xintercept = parse_number(mperiod) + .5))

Sample Image

Is there a way to draw a vertical line using geom_abline?

Instead of working with slopes, maybe you can achieve what you want with angles using geom_spoke.
The angle inclination of a line is theta = atan(slope). The opposite angle would be theta + pi.

For the horizontal and vertical lines, would you pass a slope of 0 and Inf, respectively.

EDIT: I made a poor attempt at showing how you could build the animation, but I was unable to make it transition smoothly. Hopefully you will be able to build on this.

library(tidyverse)
library(gganimate)

# Seed
set.seed(24601)

# Given the following slopes
slopes <- runif(12,-2, 2)

# What are the angles?
df = data.frame(
x = 0, # Draw from zero
y = 0, # Draw from zero
angle1 = atan(slopes), # First segment, actual angle
angle2 = atan(slopes) + pi, # Second segment, opposite angle
radius = 5, # Make sufficiently long lines
cases =c(rep("a", 2), rep("b", 2), rep("c", 2), rep("d", 2), rep("e", 2), rep("f", 2)),
axes = c("primary", "secondary")
)

# Add horizontal and vertical lines
hline_data = data.frame(x = 0, y = 0, angle1 = atan(0), angle2 = atan(0) + pi, radius = 5, cases = "reset", axes = "primary")
vline_data = data.frame(x = 0, y = 0, angle1 = atan(Inf), angle2 = atan(Inf) + pi, radius = 5, cases = "reset", axes = "primary")

df = rbind(df, vline_data, hline_data)

df %>%
ggplot(aes(x = x, y = y, radius = radius, col = cases, linetype = axes)) +
geom_spoke(aes(angle = angle1)) +
geom_spoke(aes(angle = angle2)) +
coord_cartesian(ylim=c(-1, 1), xlim = c(-1, 1)) + # Zoom in without removing data like scale_*_continous does
theme(legend.position = NULL, aspect.ratio = 1)

Sample Image

# Poor attempt at building the animation
animation_list = list(
df %>% filter(cases == "a") %>% mutate(event = 1),
df %>% filter(cases == "reset") %>% mutate(event = 2),
df %>% filter(cases == "b") %>% mutate(event = 3),
df %>% filter(cases == "reset") %>% mutate(event = 4),
df %>% filter(cases == "c") %>% mutate(event = 5),
df %>% filter(cases == "reset") %>% mutate(event = 6),
df %>% filter(cases == "d") %>% mutate(event = 7),
df %>% filter(cases == "reset") %>% mutate(event = 8),
df %>% filter(cases == "e") %>% mutate(event = 9),
df %>% filter(cases == "reset") %>% mutate(event = 10),
df %>% filter(cases == "f") %>% mutate(event = 11),
df %>% filter(cases == "reset") %>% mutate(event = 12)
)

animation_data = bind_rows(animation_list) %>%
mutate(cases = factor(cases)) # To make ggplot respect the order

animation_data %>%
ggplot(aes(x = x, y = y, radius = radius, linetype = axes, group = event)) +
geom_spoke(aes(angle = angle1)) +
geom_spoke(aes(angle = angle2)) +
coord_cartesian(ylim=c(-1, 1), xlim = c(-1, 1)) + # Zoom in without removing data like scale_*_continous does
theme(legend.position = NULL, aspect.ratio = 1) +
transition_states(event) +
labs(title = "{closest_state}")

Sample Image

Created on 2021-07-09 by the reprex package (v2.0.0)



Related Topics



Leave a reply



Submit