Conditioning my regression to not show anything below 0

  • Conditioning my regression to not show anything below 0

    Posted by Emmanuel on 13 June 2024 at 15:06

    This was my sample code

    ggplot() +

    geom_smooth(data = cytokines_postop, aes(x = Timing_cytokines, y = as.double(IL-10)), method = “loess”, formula = y ~ x, colour = “red”, fill=”violet”) +

    geom_smooth(data = cytokines_preop, aes(x = Timing_cytokines, y = as.double(IL-10)), method = “loess”, formula = y ~ x, colour = “blue”,fill=”lightblue”) +

    geom_area()+

    ylab(“Cytokine Concentration (pg/dl)”) +

    xlab(“Post-injury timing (days)”) +

    ggtitle(“IL-10 Cytokine Concentration over Time”) +

    theme_light() +

    theme(plot.title = element_text(size = 10))+

    geom_hline(yintercept = 0)

    Emmanuel replied 1 year ago 5 Members · 10 Replies
  • 10 Replies
  • Duncan

    Member
    13 June 2024 at 15:10

    You can filter the data to remove values below zero before plotting in ggplot.

    • This reply was modified 1 year ago by  Duncan.
    • Emmanuel

      Member
      13 June 2024 at 15:12

      There are no values below 0 in the data.. Its just the curve that goes below the line y=0

      • Duncan

        Member
        13 June 2024 at 15:17

        In that case you can set scale limits. e.g. coord_cartesian(ylim = c(lower_limit_value,upper_limit_value))

  • Aashna

    Organizer
    13 June 2024 at 15:14

    You can try adding + ylim(0, max(as.double(cytokines_postop$IL-10)))

    • This reply was modified 1 year ago by  Aashna.
    • Emmanuel

      Member
      13 June 2024 at 15:18

      Because the maximum limit is infinity. The plot disappears in totality

  • Ibrahim

    Moderator
    13 June 2024 at 15:19

    I think the y-axis values are correct. You fit a linear regression model and the values can range from -inf to +inf. Test this by fitting a regular regression model and you will realize some of the confidence interval values are below 0, in which case truncating the line would be inappropriate.

    By “insisting” the values be greater than 0, you are kind of interfering with the regression and saying it should discard the output. The shaded area is your confidence interval and we don’t “truncate” them.

    Best regards.

    • This reply was modified 1 year ago by  Ibrahim.
    • Emmanuel

      Member
      13 June 2024 at 15:28

      But i am looking at the values and surely… I have no negatives. But I understand your assertion.

  • Frank

    Organizer
    13 June 2024 at 15:25

    The other solution is to conditioning the smoothing factor to exclude very low values, using ‘span’,

      geom_smooth(span = 0.3)
    • Ibrahim

      Moderator
      13 June 2024 at 15:40

      So many features in R, and we learn new things every day.

  • Emmanuel

    Member
    13 June 2024 at 16:05

    Thank you all
    I decided to just use
    coord_cartesian(xlim = c(0, NA), ylim = c(0, NA))
    so that i only capture the right upper quadrant

    Regards,

Log in to reply.