Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wvictor14 opened this issue May 5, 2025 · 1 comment

Comments

@wvictor14
Copy link

wvictor14 commented May 5, 2025

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 wvictor14 changed the title Length 0 geom_segments inconsistent depending on output Length 0 geom_segment representation depends on viewing through rstudio or other (shiny, exporting to file) May 5, 2025
@teunbrand
Copy link
Collaborator

Thanks for the report! I agree that this can be frustrating, but the display of 0-length segments is at the discretion of the graphics devices, over which ggplot2 has little control. If you want consistency, then perhaps lineend = "butt" is a better choice than the round line ends.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants