Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8005017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:05:51+00:00 2026-06-04T17:05:51+00:00

Q. How can I prevent values < 0 being shown in a ggplot2 facet

  • 0

Q. How can I prevent values < 0 being shown in a ggplot2 facet plot for just one of three different variables plotted, each of different magnitudes?

I have made the following facet plot.

enter image description here

As you can see, the value plotted on the y-axis is significantly different for each variable, but using scales = "free" resolves that issue.

I want to suppress the values less than zero in the “profit_margin” facet (coloured in blue at the bottom) by either limiting the scale or setting values of less than zero to zero, but I cannot work out how to accomplish this. I could directly zap the values in the data frame but I would prefer to leave the data untouched. I tried using a function in scale_y_continuous() but was unable to make any headway.

Here is the code used to generate the above plot:

require(lubridate)
require(reshape2)
require(ggplot2)
require(scales)

## Create dummy time series data
set.seed(12345)
monthsback <- 12
startdate <- as.Date(paste(year(now()),month(now()),"1",sep = "-")) - months(monthsback)
mydf <- data.frame(mydate = seq(as.Date(startdate), by = "month", length.out = monthsback),
                   sales = runif(monthsback, min = 600, max = 800),
                   profit = runif(monthsback, min = -50, max = 80))
## Add calculation based on data
mydf$profit_margin <- mydf$profit/mydf$sales

## Reshape...
mymelt <- melt(mydf, id = c('mydate'))

## Plot
p <- ggplot(data = mymelt, aes(x = mydate, y = value, fill = variable)) +
     geom_bar(stat = "identity") +
     facet_wrap( ~ variable, ncol = 1, scales = "free")

print(p)

And this was my attempt to use a function and lapply to set sub-zero values to zero:

require(lubridate)
require(reshape2)
require(ggplot2)
require(scales)

## Create dummy time series data
set.seed(12345)
monthsback <- 12
startdate <- as.Date(paste(year(now()),month(now()),"1",sep = "-")) - months(monthsback)
mydf <- data.frame(mydate = seq(as.Date(startdate), by = "month", length.out = monthsback),
                   sales = runif(monthsback, min = 600, max = 800),
                   profit = runif(monthsback, min = -50, max = 80))
## Add calculation based on data
mydf$profit_margin <- mydf$profit/mydf$sales

## Reshape...
mymelt <- melt(mydf, id = c('mydate'))

scales_function <- function(myvar, myvalue){
    mycount <- 1
    newval <- lapply(myvalue, function(myarg) {
        myarg <- ifelse(myvar[mycount] == "profit_margin", ifelse(myarg < 0, 0, myarg), myarg)
    }
                  )
    return(newval)
}

## Plot
p <- ggplot(data = mymelt, aes(x = mydate, y = value, fill = variable)) +
     geom_bar(stat = "identity") +
     facet_wrap( ~ variable, ncol = 1, scales = "free") +
     scale_y_continuous(breaks = scales_function(mymelt$variable, mymelt$value))

print(p)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:05:52+00:00Added an answer on June 4, 2026 at 5:05 pm

    You can leave the data untouched, but just plot a subset.

    ggplot(data = subset(mymelt,!((variable == 'profit_margin') & value < 0)), 
           aes(x = mydate, y = value, fill = variable)) +
         geom_bar(stat = "identity") +
         facet_wrap( ~ variable, ncol = 1, scales = "free")
    

    Or replace within the call

    ggplot(data = mymelt, aes(x = mydate, y = replace(value, (variable == 'profit_margin') & value <0, NA), fill = variable)) +
     geom_bar(stat = "identity") +
     facet_wrap( ~ variable, ncol = 1, scales = "free") +
     ylab('value')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If one can prevent subclassing by declaring private constructor in the base class, why
How can I prevent my bitmap from being scaled automatically in an ImageView or
How can I prevent multiple users from being logged in at the same time
Does a List have a property or mechanism by which I can prevent duplicate
How can I prevent the firing of multiple events of the same kind triggered
How can I prevent Google and other search engines from indexing my website?
How can I prevent a user from tabbing into a field (comboxbox)?
How can I prevent double encoding of html entities, or fix them programmatically? I
How can I prevent the following code crashing my program? ::MessageBox(NULL, Lbefore, NULL, MB_OK);
How can I prevent caching on exception? I have this action: caches_page :index ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.