Claudio Moneo
Claudio Moneo

Reputation: 569

Adding truncated line to ggplot

I am trying to plot decision boundaries of a decision tree in ggplot. Unfortunately, when I run this code, I partition data_plot into four (rather than three regions). Note that below data is just a placeholder (doesn't make sense to partition this way).

data_plot <- data.frame(x=runif(100,0,1), y=runif(100,0,1))
cut1 <- 0.5
cut2 <- 0.5

ggplot(data_plot, aes(x=x, y=y)) +
  geom_point() +
  geom_vline(xintercept = cut1) + 
  geom_hline(yintercept = cut2)

Example

Question: How can I restrict the second cut to one of the intervals induced by the first cut. That is, how can I truncate the second line to the interval [cut1, 1] ?

Upvotes: 0

Views: 202

Answers (1)

Quinten
Quinten

Reputation: 41225

If I understand you correctly, you could use geom_segment like this:

data_plot <- data.frame(x=runif(100,0,1), y=runif(100,0,1))
cut1 <- 0.5
cut2 <- 0.5

library(ggplot2)
ggplot(data_plot, aes(x=x, y=y)) +
  geom_point() +
  geom_vline(xintercept = cut1) + 
  geom_segment(x = cut2, xend = 1, y = cut2, yend = cut2, size = 0.2)

Created on 2022-07-09 by the reprex package (v2.0.1)

Upvotes: 2

Related Questions