Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R, ggplot2: How do I increase space between specific geom_tile bars to group them in pairs?

I am using ggplot and geom_tile to plot several pairs of colour-coded, categorical time series. The plot is looking fine except that I want to increase the space between each pair of data to improve readability. So each two bars from the same session (e.g. session.1.a and session.1.b) should be close as they are now but I want to increase the space between e.g. session.1.b and session.2.a, and between session.2.b and session.3.a. How could I do this? Any help appreciated =]

This is how a simplified plot looks like: enter image description here

A simplified dataset looks like this:

mydata <- read.table(header=TRUE, text="
    dataID  time    value
                     session.1.a    0   g
                     session.1.a    1   a
                     session.1.a    2   g
                     session.1.a    3   i
                     session.1.a    4   g
                     session.1.a    5   g
                     session.1.a    6   i
                     session.1.a    7   a
                     session.1.a    8   g
                     session.1.a    9   a
                     session.1.a    10  a
                     session.1.a    11  a
                     session.1.a    12  a
                     session.1.a    13  g
                     session.1.a    14  g
                     session.1.a    15  a
                     session.1.a    16  a
                     session.1.a    17  a
                     session.1.a    18  a
                     session.1.a    19  g
                     session.1.a    20  g
                     session.1.a    21  a
                     session.1.a    22  g
                     session.1.a    23  a
                     session.1.a    24  a
                     session.1.a    25  a
                     session.1.a    26  a
                     session.1.a    27  a
                     session.1.a    28  a
                     session.1.a    29  a
                     session.1.a    30  a
                     session.1.a    31  a
                     session.1.a    32  a
                     session.1.a    33  a
                     session.1.a    34  a
                     session.1.b    0   b
                     session.1.b    1   b
                     session.1.b    2   b
                     session.1.b    3   j
                     session.1.b    4   j
                     session.1.b    5   j
                     session.1.b    6   j
                     session.1.b    7   b
                     session.1.b    8   h
                     session.1.b    9   h
                     session.1.b    10  b
                     session.1.b    11  b
                     session.1.b    12  b
                     session.1.b    13  b
                     session.1.b    14  h
                     session.1.b    15  b
                     session.1.b    16  b
                     session.1.b    17  b
                     session.1.b    18  b
                     session.1.b    19  b
                     session.1.b    20  h
                     session.1.b    21  b
                     session.1.b    22  h
                     session.1.b    23  b
                     session.1.b    24  b
                     session.1.b    25  b
                     session.1.b    26  b
                     session.1.b    27  b
                     session.1.b    28  b
                     session.1.b    29  b
                     session.1.b    30  b
                     session.1.b    31  b
                     session.1.b    32  b
                     session.1.b    33  b
                     session.1.b    34  b
                     session.2.a    0   a
                     session.2.a    1   a
                     session.2.a    2   g
                     session.2.a    3   g
                     session.2.a    4   g
                     session.2.a    5   a
                     session.2.a    6   g
                     session.2.a    7   a
                     session.2.a    8   a
                     session.2.a    9   a
                     session.2.a    10  a
                     session.2.a    11  a
                     session.2.a    12  a
                     session.2.a    13  a
                     session.2.a    14  a
                     session.2.a    15  g
                     session.2.a    16  a
                     session.2.a    17  a
                     session.2.a    18  a
                     session.2.a    19  a
                     session.2.a    20  g
                     session.2.a    21  a
                     session.2.a    22  a
                     session.2.a    23  a
                     session.2.a    24  a
                     session.2.a    25  a
                     session.2.b    0   j
                     session.2.b    1   b
                     session.2.b    2   j
                     session.2.b    3   h
                     session.2.b    4   h
                     session.2.b    5   h
                     session.2.b    6   h
                     session.2.b    7   NA
                     session.2.b    8   b
                     session.2.b    9   b
                     session.2.b    10  b
                     session.2.b    11  b
                     session.2.b    12  b
                     session.2.b    13  b
                     session.2.b    14  b
                     session.2.b    15  b
                     session.2.b    16  b
                     session.2.b    17  b
                     session.2.b    18  b
                     session.2.b    19  b
                     session.2.b    20  h
                     session.2.b    21  b
                     session.2.b    22  b
                     session.2.b    23  b
                     session.2.b    24  b
                     session.2.b    25  b
                     session.3.a    0   i
                     session.3.a    1   a
                     session.3.a    2   a
                     session.3.a    3   a
                     session.3.a    4   a
                     session.3.a    5   g
                     session.3.a    6   a
                     session.3.a    7   a
                     session.3.a    8   a
                     session.3.a    9   a
                     session.3.a    10  a
                     session.3.a    11  a
                     session.3.a    12  a
                     session.3.a    13  a
                     session.3.a    14  a
                     session.3.a    15  i
                     session.3.a    16  i
                     session.3.a    17  g
                     session.3.a    18  g
                     session.3.a    19  g
                     session.3.a    20  g
                     session.3.a    21  a
                     session.3.a    22  a
                     session.3.a    23  a
                     session.3.a    24  a
                     session.3.a    25  g
                     session.3.a    26  a
                     session.3.a    27  a
                     session.3.a    28  a
                     session.3.a    29  a
                     session.3.a    30  a
                     session.3.a    31  a
                     session.3.a    32  g
                     session.3.a    33  g
                     session.3.a    34  a
                     session.3.a    35  a
                     session.3.a    36  a
                     session.3.a    37  a
                     session.3.a    38  g
                     session.3.a    39  a
                     session.3.a    40  a
                     session.3.a    41  a
                     session.3.b    0   j
                     session.3.b    1   b
                     session.3.b    2   b
                     session.3.b    3   b
                     session.3.b    4   j
                     session.3.b    5   h
                     session.3.b    6   b
                     session.3.b    7   b
                     session.3.b    8   b
                     session.3.b    9   b
                     session.3.b    10  b
                     session.3.b    11  b
                     session.3.b    12  b
                     session.3.b    13  b
                     session.3.b    14  b
                     session.3.b    15  b
                     session.3.b    16  b
                     session.3.b    17  b
                     session.3.b    18  b
                     session.3.b    19  h
                     session.3.b    20  h
                     session.3.b    21  b
                     session.3.b    22  b
                     session.3.b    23  b
                     session.3.b    24  h
                     session.3.b    25  b
                     session.3.b    26  b
                     session.3.b    27  b
                     session.3.b    28  b
                     session.3.b    29  b
                     session.3.b    30  b
                     session.3.b    31  b
                     session.3.b    32  b
                     session.3.b    33  h
                     session.3.b    34  b
                     session.3.b    35  b
                     session.3.b    36  b
                     session.3.b    37  b
                     session.3.b    38  b
                     session.3.b    39  b
                     session.3.b    40  b
                     session.3.b    41  b
                     ")

desiredLevels=c("a",
                "b",
                "c",
                "d",
                "e",
                "f",
                "g",
                "h",
                "i",
                "j",
                "NA")

desiredLabels=c("a",
                "b",
                "c",
                "d",
                "e",
                "f",
                "g",
                "h",
                "i",
                "j",
                "NA")

mydata$value = factor(mydata$value, levels = desiredLevels, labels = desiredLabels)

The code generating the plot is this:

  desiredBreaks=c("a",
                  "b",
                  "c",
                  "d",
                  "e",
                  "f",
                  "g",
                  "h",
                  "i",
                  "j",
                  "NA")

desiredColours = c("#009E73", 
                   "#009E73", 
                   "#F0E442", 
                   "#F0E442", 
                   "#CC79A7", 
                   "#CC79A7", 
                   "#D55E00", 
                   "#D55E00", 
                   "#56B4E9", 
                   "#56B4E9", 
                   "white")

ts.plot = ggplot(mydata, aes(x = time, y = dataID, fill = value))  
ts.plot = ts.plot + geom_tile(stat="identity", width=1, height=.9)
ts.plot = ts.plot + scale_fill_manual(drop = FALSE, 
                                      name = "categories",
                                      breaks = desiredBreaks, 
                                      values = desiredColours)


time.labels = c(0, rep("",4), 5,rep("",4), 10, rep("",4), 15,rep("",4), 20, 
                rep("",4), 25,rep("",4), 30,rep("",4), 35,rep("",4), 40, rep("",4), 45)
time.breaks = seq(from=0,to = 45,1)


ts.plot = ts.plot + scale_x_continuous(limits=c(-1, 45), expand = c(0,1), breaks = time.breaks, labels = time.labels)

ts.plot = ts.plot +  labs(title = "main title", 
                          x = "\ntime (s)",
                          y = "") 

ts.plot = ts.plot +  theme(plot.title = element_text(size = rel(2)),         
                           axis.text.x = element_text(size  = 24, colour = "black"),
                           axis.text.y = element_text(size  = 24, colour="black"),
                           axis.title.x = element_text(size  = 24),
                           legend.text = element_text(size  = 24),
                           legend.direction = "vertical",
                           legend.position = "right",
                           legend.key=element_rect(size=1, colour = "black"),
                           legend.key.size = unit(1, "lines"),
                           legend.title = element_text(size  = 24),
                           axis.ticks.y = element_line(size=2),
                           panel.grid.minor = element_blank()) 

ts.plot = ts.plot + guides(fill=guide_legend(ncol=1), title.position="top" )

ts.plot
like image 697
moe Avatar asked Dec 19 '15 14:12

moe


People also ask

How to increase the space between grouped bars in ggplot2?

For grouped bars, there is no space between bars within each group by default. However, you can add some space between bars within a group, by making the width smaller and setting the value for position_dodge to be larger than width.

What is geoms in R?

Geoms. A layer combines data, aesthetic mapping, a geom (geometric object), a stat (statistical transformation), and a position adjustment. Typically, you will create layers using a geom_ function, overriding the default position and stat if needed.


1 Answers

Try this:

labels <- levels(mydata$dataID)
# the numbers in the first vector here define the positions/distances of the tiles:
mydata$aux <- rep(c(1,2,4,5,7,8), times = table(mydata$dataID))


ts.plot = ggplot(mydata, aes(x = time, y = aux, fill = value))  
ts.plot = ts.plot + geom_tile(stat="identity", width=1, height=.9) + scale_y_discrete(breaks = c(1,2,4,5,7,8), labels = labels, limits = c(1,2,4,5,7,8))
ts.plot

enter image description here

like image 60
DatamineR Avatar answered Nov 15 '22 00:11

DatamineR