There are a few questions around how to create a 'type = b' look with ggplot (e.g. here and here)
The currently best solution is simply creating bigger dots in the color of the underlying background - which is admittedly very straight forward.
I am looking for a solution with geom_segment
- because the current solution does not allow for additional 'fanciness', in particular: error bands. See graph at the bottom. (I know that I can use lines for the confidence intervals, and this looks good, but the question is regarding the below)
I have made a simple function using basic trigonometry in order to calculate x and y for the segments in geom_segment
(see below - thanks to @moody_mudskipper to give the main ideas how to approach this). Now, this only works with coord_equal
(see below). My question is, how can I obtain the axis ratio from the plot which is created in order to use this ratio for calculation of the x/y segments?
library(ggplot2)
# function to get data frame with x and y parameters for geom_segment
get_segments <- function(df, x, y, param){
# hyp = hypotenuse of right triangle between x and y of dot plot
# sin_plot and cos_plot = sine and cosine of right triangle of dot plot
# diff_..._seg = hypotenuse of segment added to main dot plot
x <- df[[deparse(substitute(x))]]
y <- df[[deparse(substitute(y))]]
hyp <-sqrt(diff(x)^2 + diff(y)^2)
sin_plot <- diff(y) / hyp
cos_plot <- diff(x) / hyp
diff_x1_seg <- param * cos_plot
diff_x2_seg <- (hyp-param) * cos_plot
diff_y1_seg <- param * sin_plot
diff_y2_seg <- (hyp-param) * sin_plot
x1 <- c(head(x,-1) + diff_x1_seg)
x2 <- c(head(x,-1) + diff_x2_seg)
y1 <- c(head(y,-1) + diff_y1_seg)
y2 <- c(head(y,-1) + diff_y2_seg)
plot_data <- data.frame(x1,x2,y1,y2)
plot_data$x1 <- ifelse(plot_data$x1 > plot_data$x2, NA, x1)
plot_data
}
# Using the function on sample data
plot_data <-
get_segments(pressure, x = temperature, y = pressure, 15)
# Making the plot
p1 <- ggplot(pressure, aes(temperature, pressure)) +
geom_point() +
geom_segment(data = plot_data, mapping = aes(x = x1, xend = x2, y = y1, yend = y2))
Plot without coord_equal
- does not really work
p1
#> Warning: Removed 11 rows containing missing values (geom_segment).
Plot with coord_equal
- gives the right segments
p1 + coord_equal()
#> Warning: Removed 11 rows containing missing values (geom_segment).
Example what could be nice:
ggplot(pressure, aes(temperature, pressure)) +
geom_ribbon(aes(ymin = pressure - 50, ymax = pressure + 50), alpha = 0.2) +
geom_point() +
geom_segment(data = plot_data, mapping = aes(x = x1, xend = x2, y = y1, yend = y2))
#> Warning: Removed 11 rows containing missing values (geom_segment).
P.S. I know that I could just plot the ribbon above the plot, but this gets less nice when using different grey values. And again, this question is more about how to get the axis ratio...
Created on 2019-04-25 by the reprex package (v0.2.1)