ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Length 0 geom_segment representation depends on viewing through rstudio or other (shiny, exporting to file)

Open wvictor14 opened this issue 6 months ago • 1 comments

When xend = x and yend = y, the plot that I see in rstudio shows a "point" for 0-length segments plotted by geom_segment. This is desirable to my use-case:

Image

However, I noticed in shiny, length 0 segments are not shown at all:

Image

I'm using RStudio Server 2024.12.1 Build 563.

I can see if exported to png there is also no length 0 segments being displayed.

I see some potentially relevant issues but none that describe this inconsistency: #5140 #3894

I'm not sure what the intended behaviour is, my use-case is to prefer a representation of 0-length segments as points, but I can understand why that might not be the intended geom_segment behaviour. For example, I can get around this by using geom_point for those cases. I think that the plot in rstudio viewer should be the same as shiny output, exported through ggsave, etc. though.

library(ggplot2)
library(dplyr)
library(tibble)
library(shiny)
library(bslib)
ggplot2::theme_set(theme_bw())
df <- tibble(
  x = c(1, 1, 1),
  xend = c(2, 5, 1),
  y = c('len1', 'len4', 'len0')
)
plot_example <- df |> 
  ggplot(aes(x = x, y = y)) +
  geom_segment(aes(xend = xend, yend = y), linewidth = 7, lineend = 'round')

# see length 0 is displayed as a "point"
plot_example 

# length 0 is not displayed at all
ui <- page_sidebar(
  plotOutput('plot')
)
shinyApp(
  ui, 
  function(input, output) {
    output$plot <- renderPlot({
      plot_example 
    })
  }
)

# export png does not show length 0 segments either
ggsave('plot_example.png', plot_example)

wvictor14 avatar May 05 '25 18:05 wvictor14