ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

position_dogev for points

Open smouksassi opened this issue 1 year ago • 2 comments

ggstance has been deprecated and recently was removed from CRAN @lionel- kindly offered that he will make a last ditch edits to re instate it on CRAN but it is bound to disappear.

One of the last things that we still cannot do in ggplot2 is being able to dodge point vertically for various reasons: the geom_point has no orientation that can force the position to work in the alternative direction

is there a way to enable vertical dodging for points ? or to force geom_point to have flipped aes ? https://github.com/tidyverse/ggplot2/blob/main/R/position-dodge.R#L97

library(ggplot2)
library(ggstance)
#> 
#> Attaching package: 'ggstance'
#> The following objects are masked from 'package:ggplot2':
#> 
#>     geom_errorbarh, GeomErrorbarh
library(patchwork)
df <- data.frame(
  trt = c(1,1,2,2),
  resp = c(1, 5, 3, 4),
  group = factor(c(1, 2, 1, 2)),
  upper = c(1.1, 5.3, 3.3, 4.2),
  lower = c(0.8, 4.6, 2.4, 3.6)
)

 
 ggplot(df, aes( resp,trt, colour = group))+
   geom_linerange(aes(xmin = lower, xmax = upper),
                   position = position_dodge(width=0.2),orientation ="y")+
   geom_point(aes(group=interaction(group,trt)),position = position_dodge(width=0.2)) |
 ggplot(df, aes( resp,trt, colour = group))+
   geom_linerange(aes(xmin = lower, xmax = upper),
                  position = position_dodge(width=0.2),orientation ="y")+
   geom_point(aes(group=interaction(group,trt)),position = position_dodgev(height=0.2))

Created on 2024-03-26 with reprex v2.1.0

smouksassi avatar Mar 26 '24 12:03 smouksassi

Does geom_pointrange() work? Or do you have a more complex use case that requires a separate point geom?

library(ggplot2)

df <- data.frame(
  trt = c(1,1,2,2),
  resp = c(1, 5, 3, 4),
  group = factor(c(1, 2, 1, 2)),
  upper = c(1.1, 5.3, 3.3, 4.2),
  lower = c(0.8, 4.6, 2.4, 3.6)
)

ggplot(df, aes( resp,trt, colour = group))+
  geom_pointrange(
    aes(xmin = lower, xmax = upper),
    position = position_dodge(width = 0.2),
    orientation ="y"
  ) 

Created on 2024-03-26 with reprex v2.1.0

karawoo avatar Mar 26 '24 19:03 karawoo

Hi yes indeed my use case is more complex where I build a plot layer by layer and where user can add a point that should match the positon of a linerange. I tried to provide a minimum reprex above. also applies to geom text

library(ggplot2) library(patchwork) library(ggstance) df <- data.frame( trt = c(1,1,2,2), resp = c(1, 5, 3, 4), group = factor(c(1, 2, 1, 2)), upper = c(1.1, 5.3, 3.3, 4.2), lower = c(0.8, 4.6, 2.4, 3.6) )

ggplot(df, aes( resp,trt, colour = group))+ geom_linerange(aes(xmin = lower, xmax = upper), position = position_dodge(width=0.2),orientation ="y")+ geom_text(aes(x=upper,group=interaction(group,trt),label=group),position = position_dodge(width=0.2)) | ggplot(df, aes( resp,trt, colour = group))+ geom_linerange(aes(xmin = lower, xmax = upper), position = position_dodge(width=0.2),orientation ="y")+ geom_text(aes(x=upper,group=interaction(group,trt),label=group),position = position_dodgev(height=0.2))

smouksassi avatar Mar 27 '24 05:03 smouksassi