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 6253833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:04:24+00:00 2026-05-24T14:04:24+00:00

I am using ggplot’s geom_tile to do 2-D density plots faceted by a factor.

  • 0

I am using ggplot’s geom_tile to do 2-D density plots faceted by a factor. Every facet’s scale goes from the minimum of all the data to the maximum of all the data, but the geom_tile in each facet only extends to the range of the data plotted in that facet.

Example code that demonstrates the problem:

library(ggplot2)

data.unlimited <- data.frame(x=rnorm(500), y=rnorm(500))
data.limited <- subset(data.frame(x=rnorm(500), y=rnorm(500)), x<1 & y<1 & x>-1 & y>-1)

mydata <- rbind(data.frame(groupvar="unlimited", data.unlimited),
                data.frame(groupvar="limited", data.limited))

ggplot(mydata) +
  aes(x=x,y=y) +
  stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE) +
  facet_wrap(~ groupvar)

facet

Run the code, and you will see two facets. One facet shows a density plot of an “unlimited” random normal distribution. The second facet shows a random normal truncated to lie within a 2×2 square about the origin. The geom_tile in the “limited” facet will be confined inside this small box instead of filling the facet.

last_plot() +
  scale_x_continuous(limits=c(-5,5)) + 
  scale_y_continuous(limits=c(-5,5))

specified limits

These last three lines plot the same data with specified x and y limits, and we see that neither facet extends the tile sections to the edge in this case.

Is there any way to force the geom_tile in each facet to extend to the full range of the facet?

  • 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-05-24T14:04:26+00:00Added an answer on May 24, 2026 at 2:04 pm

    I think you’re looking for a combination of scales = "free" and expand = c(0,0):

    ggplot(mydata) +
      aes(x=x,y=y) +
      stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE) +
      facet_wrap(~ groupvar,scales = "free") + 
      scale_x_continuous(expand = c(0,0)) + 
      scale_y_continuous(expand = c(0,0))
    

    enter image description here

    EDIT

    Given the OP’s clarification, here’s one option via simply setting the panel background manually:

    ggplot(mydata) +
      aes(x=x,y=y) +
      stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE) +
      facet_wrap(~ groupvar) + 
      scale_fill_gradient(low = "blue", high = "red") + 
      opts(panel.background = theme_rect(fill = "blue"),panel.grid.major = theme_blank(),
           panel.grid.minor = theme_blank())
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to make a series of plots using ggplot from multiple different
I plotted a facet plot using ggplot and here is the plot The problem
I am using ggplot2 to plot a figure that contains nine facets. Each facet
I'm plotting a group of curves, using facet in ggplot2. I'd like to have
I have a a stacked bar plot using ggplot for which I'm trying to
I am new in R and am trying to so some graphics using ggplot
I'm trying to show an intercept on a line graph using the ggplot vline
I'm fairly new to ggplot. I have made a bumpplot using code posted below.
I'm using the following code to create three sets of plots in the amazing
I am making a dotplot using ggplot with the code and data that is

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.