Skip to content

Problems with axis limits? #3948

@gael-millot

Description

@gael-millot

I believe I have some trouble when setting axis limits. The first is with scale_y_continuous():

# data
library(ggplot2)
ylim <- c(6, 30)
obs1 <- data.frame(
Time = 1:20, 
Group1 = rep(c("G", "H"), times = 10), 
Group2 = rep(c("A", "B"), each = 10)
)

obs1

image

# bars not displayed if 0 is not in the ylim range (test ylim = c(-1, 30))
# The problem is reported with a warning: all the values are removed when 0 is not in the range
p <- ggplot(data = obs1, mapping = aes(y = Time, x = Group2, fill = Group1)) 
p +
geom_bar(stat = "identity", position = position_dodge()) + 
scale_y_continuous(limits = sort(ylim))

image

# bars displayed whatever the ylim range
p + 
geom_bar(stat = "identity", position = position_dodge()) + 
coord_cartesian(ylim = sort(ylim))

image

The reason why I use coord_cartesian(ylim = sort(ylim)) in most of my functions. However, I encountered a new problem:

# y-axis disappearing
p + 
geom_point() + 
coord_cartesian(ylim = sort(ylim)) + 
scale_y_reverse()

image

# y-axis not disappearing
p + 
geom_point() + 
scale_y_continuous(limits = sort(ylim)) + 
scale_y_reverse()

image

Are all these expected ?

Many thanks for your help.

Best.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions