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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:10:26+00:00 2026-06-14T00:10:26+00:00

Let’s say I have a data.frame like this: molten <- data.frame( Var1 = factor(

  • 0

Let’s say I have a data.frame like this:

molten <- data.frame(
  Var1 = factor(
    rep(c("A", "B", "C", "D"), 5),
    levels = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")
  ),
  Var2 = rep(6:10, each = 4L),
  value = c(
    -0.468920099229389, 0.996105987531978, -0.527496444770932, -0.767851702991822,
    -0.36077954422072, -0.145335912847538, 0.114951323188032, 0.644232124274217,
    0.971443502096584, 0.774515290180507, -0.436252398260595, -0.111174676975868,
    1.16095688943808, 0.44677656465583, -0.708779168274131, 0.460296447139761,
    -0.475304748445917, -0.481548436194392, -1.66560630161765, -2.06055347675196
  ),
  na = rep(c(FALSE, TRUE, FALSE, TRUE, FALSE), c(8L, 1L, 7L, 1L, 3L)),
  row.names = c(
    51L, 52L, 53L, 54L, 61L, 62L, 63L, 64L, 71L, 72L, 73L, 74L,
    81L, 82L, 83L, 84L, 91L, 92L, 93L, 94L
  )
)

head(molten)

  Var1 Var2      value    na
1    A    1 -0.2413015 FALSE
2    B    1  1.5077282 FALSE
3    C    1 -1.0798806 TRUE
4    D    1  2.0723791 FALSE

Now, I want to plot a tile (or raster) plot using ggplot and mark those tiles which have na=TRUE. Currently I plot the marks as points:

g <- ggplot( molten ) +
  geom_raster( aes( x = Var1, y = Var2, fill = value )  ) + 
  scale_fill_gradient2( low = "blue", high = "red", na.value="black", name = "" ) +
  geom_point( aes( x = Var1, y = Var2, size= as.numeric(na) ) )

tiles with points

However, I don’t like this plot very much for two reasons:

  1. There is still a point drawn even if molten$na = FALSE. Sure I could specify data=molten[ molten$na, ], but actually this should be possible without specifying another data set.
  2. I don’t like the points, but would rather like to have frames around or stripes through the tiles. But I have no idea how to achieve this. If I would use geom_segment() for stripes, how would I specify yendand xend?
  • 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-14T00:10:28+00:00Added an answer on June 14, 2026 at 12:10 am

    Here are two possible approaches:

    In Example 1, I used ifelse and scale_size_manual to control whether a point is plotted in each cell.

    In Example 2, I created a small auxiliary data.frame and used geom_rect to plot a rectangle instead of a dot. For convenience, I converted Var2 to factor. In ggplot2, each step along a discrete/factor axis is length 1.0. This allows easy computation of the values for geom_rect.

    # Using ggplot2 version 0.9.2.1
    library(ggplot2)
    
    # Test dataset from original post has been assigned to 'molten'.
    
    molten$Var2 = factor(molten$Var2)
    
    # Example 1.
    p1 = ggplot(data=molten, aes(x=Var1, y=Var2, fill=value)) +
         geom_raster() +
         scale_fill_gradient2(low="blue", high="red", na.value="black", name="") +
         geom_point(aes(size=ifelse(na, "dot", "no_dot"))) +
         scale_size_manual(values=c(dot=6, no_dot=NA), guide="none") +
         labs(title="Example 1")
    
    ggsave(plot=p1, filename="plot_1.png", height=3, width=3.5) 
    

    enter image description here

    # Example 2.
    # Create auxiliary data.frame.
    frames = molten[molten$na, c("Var1", "Var2")]
    frames$Var1 = as.integer(frames$Var1)
    frames$Var2 = as.integer(frames$Var2)
    
    p2 = ggplot(data=molten) +
         geom_raster(aes(x=Var1, y=Var2, fill=value)) +
         scale_fill_gradient2(low="blue", high="red", na.value="black", name="") +
         geom_rect(data=frames, size=1, fill=NA, colour="black",
           aes(xmin=Var1 - 0.5, xmax=Var1 + 0.5, ymin=Var2 - 0.5, ymax=Var2 + 0.5)) +
         labs(title="Example 2")
    
    ggsave(plot=p2, filename="plot_2.png", height=3, width=3.5) 
    

    enter image description here

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

Sidebar

Related Questions

Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say I have this code: <p dataname=description> Hello this is a description. <a
Let's say I have some text as follows: do this, do that, then this,
Let's say I create an object like this: Person: NSString *name; NSString *phone; NSString
Let's suppose I have an XML file like this: <?xml version=1.0 encoding=ISO-8859-1?> <MIDIFile> <Event>
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I can call a method like this: core::get() . What is the

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.