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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:37:31+00:00 2026-06-03T09:37:31+00:00

I’m using a heatmap to follow the attrition in my experiment and I would

  • 0

I’m using a heatmap to follow the attrition in my experiment and I would like to print some date information exclusively in the two first columns, but for some reason ggplot2 won’t accept that many missing observations.

This is how my plot look at the moment (way to many dates).

Can anyone tell me how I can limit the geom_text to only the two first columns?

See blow for sample code.
heatmap and facet_grid

require(ggplot2)
require(plyr)
require(reshape)

# create sample data
set.seed(666)
dfn <- data.frame(
Referral  = seq(as.Date("2007-01-15"), len= 26, by="23 day"),
VISIT01  = seq(as.Date("2008-06-15"), len= 24, by="15 day")[sample(30, 26)],
VISIT02  = seq(as.Date("2008-12-15"), len= 24, by="15 day")[sample(30, 26)],
VISIT03  = seq(as.Date("2009-01-01"), len= 24, by="15 day")[sample(30, 26)],
VISIT04  = seq(as.Date("2009-03-30"), len= 24, by="60 day")[sample(30, 26)],
VISIT05  = seq(as.Date("2010-11-30"), len= 24, by="6 day")[sample(30, 26)],
VISIT06  = seq(as.Date("2011-01-30"), len= 24, by="6 day")[sample(30, 26)],
Discharge = seq(as.Date("2012-03-30"), len= 24, by="30 day")[sample(30, 26)],
Patient  = factor(1:26, labels = LETTERS),
openCase  = rep(0:1, 100)[sample(100, 26)])

 # set today's data for cases that do not have an Discharge date
 dfn$Discharge[ is.na(dfn$Discharge) ] <- as.Date("2014-01-30")

 mdfn <- melt(dfn, id=c('Patient', 'openCase'), variable_name = "Visit")
 names(mdfn)[4] <- 'Year'

 # order data in mdfn by 'Referral' in dfn
 mdfn$Patient <- factor(mdfn$Patient,levels = (dfn$Patient[order(dfn$Referral)]),
 ordered = TRUE)

mdfn$Year2 <- ifelse(mdfn$Year != 0, 1, 0)
mdfn$Year2[ is.na(mdfn$Year) ] <- 0

mdfn$FWrap <- 'Core Measures'
mdfn$FWrap[mdfn$Visit == 'Referral'] <- 'Referral'
mdfn$FWrap[mdfn$Visit == 'VISIT05' | mdfn$Visit == 'VISIT06'] <- 'Optional Measures'
mdfn$FWrap[mdfn$Visit == 'Discharge'] <- 'Discharge'
mdfn$FWrap <- ordered(mdfn$FWrap, levels = c('Referral', 'Core Measures', 'Optional Measures', 'Discharge'))

base_size <- 9
m2 <- ggplot(mdfn, aes(Visit, Patient)) + 
geom_tile(aes(fill = Year2), colour = "gray") +
scale_fill_gradient(low = "white", high = "steelblue")
m2 + theme_grey(base_size = base_size) + labs(x = "", y = "") +
scale_x_discrete(expand = c    (0, 0)) + scale_y_discrete(expand = c(0, 0)) + 
opts(legend.position = "none", axis.ticks = theme_blank(), 
axis.text.x = theme_text(size = base_size * 0.8, angle = 330, hjust = 0,
colour = "grey50")) + facet_grid(~ FWrap, scales = "free", space="free") + 
geom_text(aes    (label=format(Year, "%b. %d, %Y")), size=3, colour='white')

# this is how I thought I could solve it.
mdfn$Yearlabel <- mdfn$Year
mdfn$Yearlabel[mdfn$Visit != 'Referral' & mdfn$Visit != 'VISIT01'] <- NA
# mdfn[,c(4,5,7,3)]

m2 <- ggplot(mdfn, aes(Visit, Patient)) + 
geom_tile(aes(fill = Year2), colour = "gray") +
scale_fill_gradient(low = "white", high = "steelblue")
m2 + theme_grey(base_size = base_size) + labs(x = "", y = "") +
scale_x_discrete(expand = c    (0, 0)) + scale_y_discrete(expand = c(0, 0)) + 
opts(legend.position = "none", axis.ticks = theme_blank(), 
axis.text.x = theme_text(size = base_size * 0.8, angle = 330, hjust = 0,
colour = "grey50")) + facet_grid(~ FWrap, scales = "free", space="free") + 
geom_text(aes    (label=format(Yearlabel, "%b. %d, %Y")), size=3, colour='white')
  • 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-03T09:37:33+00:00Added an answer on June 3, 2026 at 9:37 am

    Try this,

    p = ggplot(mdfn, aes(Visit, Patient)) + 
      geom_tile(aes(fill = Year2), colour = "gray") +
      scale_fill_gradient(low = "white", high = "steelblue") + 
      theme_grey(base_size = base_size) + 
      labs(x = "", y = "") +
      scale_x_discrete(expand = c(0, 0)) + 
      scale_y_discrete(expand = c(0, 0)) + 
      opts(legend.position = "none", axis.ticks = theme_blank(), 
           axis.text.x = theme_text(size = base_size * 0.8, angle = 330,
             hjust = 0, colour = "grey50")) + 
      facet_grid(~ FWrap, scales = "free", space="free") 
    
    s = subset(mdfn, Visit %in% c("Referral","VISIT01"))
    p + geom_text(aes(label=format(Year, "%b. %d, %Y")), data = s, 
                  size=3, colour = 'white')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
We're building an app, our first using Rails 3, and we're having to build
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm making a simple page using Google Maps API 3. My first. One marker
I am using JSon response to parse title,date content and thumbnail images and place
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.