sinhrks / ggfortify

Define fortify and autoplot functions to allow ggplot2 to handle some popular R packages.
Other
527 stars 65 forks source link

no x/y labels with ggdistribution plots #218

Open petzi53 opened 2 years ago

petzi53 commented 2 years ago

Hello,

with the ggdistribution() function x and y labels do not appear.

Code to reproduce the issue:

p <- ggfortify::ggdistribution(dnorm, seq(-3, 3, 0.1), mean = 0, sd = 1)

p <- p +
    labs(
    title = "Title",
    subtitle = "subtitle",
    caption = "name of the author copyright",
    tag = "Plot A",
    x = "x label",
    y = "y label"
) 
p

Plot produced by the code:

xlab-problem


System information

Thanks, Peter

terrytangyuan commented 2 years ago

Thanks for reporting this. Have you tried this on older versions of ggplot2?

petzi53 commented 2 years ago

No, I haven't tried it with older versions of ggplot2. Maybe it is a regression? As you can see in an SO post, it worked several years ago. @alexandra-wall reported a similar issue last year.

terrytangyuan commented 2 years ago

The relevant code looks correct to me https://github.com/sinhrks/ggfortify/blob/912ba6d39feb1537b30997632b1d125500c1058d/R/fortify_stats_density.R#L48-L51

Could you try the following?

ggfortify::ggdistribution(dnorm, seq(-3, 3, 0.1), mean = 0, sd = 1, xlab="x axis", ylab="y axis")
petzi53 commented 2 years ago

It reports an error:

Error in func(x, ...) : 
  unused arguments (xlab = "x axis", ylab = "y axis")
axjadamson commented 2 years ago

Just to say, I'm having the same issue and get the same error as petzi53

axjadamson commented 2 years ago

Okay I fixed it and created a pull request.

terrytangyuan commented 2 years ago

Fixed by https://github.com/sinhrks/ggfortify/pull/222. Thanks! @axjadamson

@petzi53 Please try out the GitHub version.

axjadamson commented 2 years ago

No worries! I hope it works okay. xlabs and ylabs can be defined within the ggdistribution function now.

petzi53 commented 2 years ago

The following code now works perfectly: ggfortify::ggdistribution(dnorm, seq(-3, 3, 0.1), mean = 0, sd = 1, xlab="x axis", ylab="y axis")

My former trial where I used the labs() function from {ggplot2} still does not work. It does not display "x label" or "y label". But if you use

p <– ggfortify::ggdistribution(dnorm, seq(-3, 3, 0.1), mean = 0, sd = 1, xlab="x axis", ylab="y axis")

and then the ggplot additions:

p <- p +
    labs(
    title = "Title",
    subtitle = "subtitle",
    caption = "name of the author copyright",
    tag = "Plot A",
    x = "x label",
    y = "y label"
) 
p

it ignores the ggplot xlabs and ylabs ("x label" resp. "y label") and uses the former defined "x axis", ylab="y axis" for the graphic.


System information OS Platform and Distribution:macOS Monterey 12.3.1 ggfortify installed from (e.g. CRAN or Github): This time: GitHub ggfortify version: 0.4.14 ggplot2: 3.3.5 R version 4.1.3 (2022-03-10)

terrytangyuan commented 2 years ago

@axjadamson Would you like to help take a look at the original use case?

axjadamson commented 2 years ago

I must admit, I only took a quick look at it and could see that some arguments were missing that would allow labels to be specified with the 'ggdistribution' function, so I updated that. I don't have a deeper understanding of why it won't accept further ggplot additions for x and y axis labels. I would guess that it might be something to do with those arguments being renamed within the ggdistribution function and no longer recognising the ggplot arguments? But I don't know I'm afraid.

terrytangyuan commented 2 years ago

No problem. Let's keep this open.