Sumner18
Sumner18

Reputation: 175

big dotted geom_line, but dots closer together

I am curious if it is possible to increase the size of dots on a dotted line within geom_line but keep the dots closer together. The R code below produces a basic reproducible example of what I see, and then I include another chart showing what I would like to see.

library(dplyr)
library(ggplot2)
set.seed(5223)
myDF <- data.frame(x=rnorm(20,0,1),
                   y=runif(20,0,20))
myDF <- myDF %>% 
  mutate(From8to12 = y>=8 & y<=12)

ggplot(myDF,aes(x=x,y=y,col=From8to12)) +
  geom_point() +
  geom_hline(yintercept=8,lty="dotted") +
  geom_hline(yintercept=12,lty="dotted",size=1.5)

Unedited Image

Unedited image

(Manually) Edited Image (In Paint)

I would like to make the dots bigger, but closer together. Is this possible? I've found nothing online.

Edited Image

Upvotes: 4

Views: 2892

Answers (2)

tjebo
tjebo

Reputation: 23757

Edit I thought it wasn't possible without some severe hacking of the underlying draw functions - @StupidWolf's answer proved me wrong. My suggestion was to draw every single point with geom_point and shape = 15 (filled square). It is then a matter of your final plot size, what parameters you chose (i.e., the distance between the 'dots' and their size)

P.S. It's impressive that you have actually managed to produce your image in paint.

library(tidyverse)
set.seed(5223)
myDF <- data.frame(x = rnorm(20, 0, 1), y = runif(20, 0, 20))

dot_dis <- 0.05
x_line <- seq(min(myDF$x), max(myDF$x), dot_dis)
y_line <- 12

ggplot() +
  geom_point(aes(x, y), data = myDF) +
  geom_point(aes(x_line, y_line), shape = 15, size = 1.5)

Created on 2020-02-18 by the reprex package (v0.3.0)

Upvotes: 1

StupidWolf
StupidWolf

Reputation: 46908

It's something like lty in base R plot, and one way is to specify specifically how long should the dash and gap be:

ggplot(myDF,aes(x=x,y=y,col=From8to12)) +
  geom_point() +
  geom_hline(yintercept=8,lty="dotted") +
  geom_hline(yintercept=12,lty="11",size=1.5)

the "11" means length of 1 for dash, length of 1 for gap, and it will replicate this. you can see more about it here

enter image description here

Upvotes: 10

Related Questions