Skip to content

log10 transformation gives the wrong scales with geom_bar #4751

Closed
@nicolerg

Description

@nicolerg

Hello,

When there is more than one entry per x value for geom_bar, scale_y_continuous(trans="log10") (or equivalently scale_y_log10()) labels the y-axis incorrectly. In this example, the max value should be 5e3, but the axis label goes up to 1e7.

Expected behavior with simple data:

df = data.frame(x=1:5, y=c(2, 20, 200, 400, 5000))

ggplot(df, aes(x=factor(x), y=y)) +
  geom_bar(stat="identity") +
  theme_classic() +
  scale_y_continuous(trans="log10") 

simple

Bug with more complex data:

df = data.frame(x=rep(1:5, 2),
                y=rep(c(1, 10, 100, 200, 2500),2), 
                group=rep(letters[1:5], each=2))

ggplot(df, aes(x=factor(x), y=y)) +
  geom_bar(stat="identity") +
  theme_classic() +
  scale_y_continuous(trans="log10") 

log10

It is also a bit odd that the log10 transformation seems to be applied per entry, i.e. there is a total value of 2 for x==1, but it shows up as 0 because log10(1) + log10(1) = 0, whereas I would expect it to show log10(1+1).

This is with ggplot2_3.3.5.

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