1

My question is similar to this one, but I cannot make that answer work for my plot.

I am making a timeline using geom_linerange for a group of names (1:20), each of which is associated with a sponsor (B:E). I want to "facet" the graph so the names/timelines are grouped by sponsor. So far I can get a combined plot if I create a "Combo" factor, which contains the sponsor+name. However, if I try to facet, then each sponsor gets all the names.

Here is my (a modified subset) of my data set (I am using lubridate for the dates...):

structure(list(Sponsor = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("B", "C", "D", "E"), class = "factor"), Last = structure(1:20, .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"), class = "factor"), Grant = c(0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0.5, 0, 1, 1, 0), Start = structure(c(844128000, 904003200, 1001289600, 1314835200, 1064188800, 1107561600, 1138838400, 1264896000, 1222819200, 1042502400, 1343779200, 904521600, 950832000, 1009929600, 1081209600, 1171929600, 821664000, 865209600, 979603200, 1209600000), class = c("POSIXct", "POSIXt"), tzone = "UTC"), End = structure(c(929232000, 946684800, 1088035200, NA, 1109721600, 1324512000, 1232496000, NA, NA, 1101859200, 1388448000, 993859200, 1006819200, 1103500800, 1139529600, 1235952000, 919036800, 1030665600, 1047254400, 1272585600), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Combo = c("B_1", "B_2", "B_3", "B_4", "B_5", "B_6", "B_7", "B_8", "C_9", "C_10", "C_11", "D_12", "D_13", "D_14", "D_15", "D_16", "E_17", "E_18", "E_19", "E_20")), .Names = c("Sponsor", "Last", "Grant", "Start", "End", "Combo"), row.names = c(NA, 20L), class = "data.frame") 

Here is the command to generate the non-faceted plot which groups things in order, but not subdivided:

library(ggplot2) require(lubridate) YearLine = ymd(19960101) + years(seq(0,18)) ggplot(testdat,aes(Combo, Start, ymin=Start,ymax=End,color=as.factor(Grant),xticks)) + xlab("Sponsor") + geom_linerange(size=4,alpha=.7) + geom_point(size=4,shape=18) + coord_flip() + scale_colour_brewer(palette="Spectral") + scale_x_discrete(labels=testdat$Sponsor) + geom_hline(yintercept = as.numeric(YearLine),alpha=0.6,col="indianred1",linetype="dotted") + annotate("text", x = testdat$Combo, y = testdat$Start, label = testdat$Last, hjust=0,size=2.5) 

And here is the plot that this generates. This is sort of what I want (in that it is not redundant) but I would like it subdivided by Sponsor:

Plot example

If I add + facet_grid(Sponsor ~ .,scale="free_x",space="free_x") (or free_y), then it facets the panels by Sponsor as I was hoping, but I still get all the names listed, even if they are not relevant to that "Sponsor" B through E:

Faceted attempt

1 Answer 1

1

The main problem you had was using annotate where you needed to use geom_text:

ggplot(testdat,aes(Combo, Start, ymin=Start,ymax=End,color=as.factor(Grant),xticks)) + xlab("Sponsor") + geom_linerange(size=4,alpha=.7) + geom_point(size=4,shape=18) + coord_flip() + scale_colour_brewer(palette="Spectral") + scale_x_discrete(labels=testdat$Sponsor) + geom_hline(yintercept = as.numeric(YearLine),alpha=0.6,col="indianred1",linetype="dotted") + geom_text(aes(x=Combo, y=Start, label = testdat$Last), colour="black") + facet_grid(Sponsor ~ .,scale="free_x",space="free_x") 

annotate doesn't really follow the existing aesthetic mappings you've defined for the rest of the plot, which is why it wasn't affected by your faceting.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I tried with/without the annotate line in there... but in your plot, I still see all the names on the y-axis even for the Sponsors where there are no names. For example in panel E at the bottom, I don't want it to show lines for 1-16 on the y (actually x-flipped) axis... I am hoping that the scale parameter will remove those lines. I'll try some more variations without annotate in there...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.