mjs
mjs

Reputation: 250

How to add name for each column to geom_tile plot

To my geom_tile plot I need columns names, here 6, above the first row and below the subtitle enter image description here I tried adding it by

 geom_text(x=10, y=10, label="testTEXT") +

but it doesn't show. Perhaps the coordinates are wrong (I tested various value)?

Here the full code:

library(tidyverse)
s1 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s2 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s3 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s4 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s5 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s6 = sample(c("p1","p12","p3","p14","p5","p13"),6)
groupsNo = 6
BLUB = c(s1,s2,s3,s4,s5,s6)
df <- cbind.data.frame(items = BLUB, 
                       x = rep((1:groupsNo),each = length(s1)), 
                       y = rep(1:length(s1), groupsNo),
                       color = BLUB)

p1 <- ggplot(df,aes(x = x, y = y,fill=factor(color))) + 
  geom_line(aes(group = items,color=factor(color)), size = 2) + 
  geom_tile(width = 0.6, height = 0.6, color = 'black') + 
  theme_void() + 
  geom_text(aes(label = items), size=5) +
  # geom_text(x=100, y=0, label="testTEXT") +
  theme(legend.position = "none") +
  labs(
    y = "", x = "",
    title = "Plot for random data",
    subtitle = "text",
    caption = "text")
print(p1)

Upvotes: 0

Views: 1049

Answers (2)

WaltS
WaltS

Reputation: 5530

The basic problem with your plot is the use of theme_void() which blanks almost all plot labels. You have to override this by specifying the theme for axis text and then blanking the y axis.

You can position the x axis labels at the top by specifying position = "top" in the scale_x_continuous function. You'll also need to explicitly specify the breaks to have labels for all columns. The revised code would be:

  p1 <- ggplot(df,aes(x = x, y = y, fill=factor(color))) + 
  geom_line(aes(group = items,color=factor(color)), size = 2) + 
  geom_tile(width = 0.6, height = 0.6, color = 'black') + 
  theme_void() + 
  theme(axis.text = element_text(size = 12, color = "black",
                                 inherit.blank = FALSE))+
  theme(axis.text.y = element_blank()) +
  scale_x_continuous(position = "top", breaks = df$x ) +
#
# If x were a character value rather than a numeric one,
# replace   scale_x_continuous(..)   with 
# scale_x_discrete( position = "top") +
# 
  geom_text(aes(label = items), size=5) +
  theme(legend.position = "none") +
  labs(
    y = "", x = "",
    title = "Plot for random data",
    subtitle = "text",
    caption = "text")
print(p1)

enter image description here

Upvotes: 2

alan ocallaghan
alan ocallaghan

Reputation: 3038

Try this:

library(tidyverse)
s1 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s2 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s3 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s4 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s5 = sample(c("p1","p12","p3","p14","p5","p13"),6)
s6 = sample(c("p1","p12","p3","p14","p5","p13"),6)
groupsNo = 6
BLUB = c(s1,s2,s3,s4,s5,s6)
df <- cbind.data.frame(items = BLUB, 
                       x = rep((1:groupsNo),each = length(s1)), 
                       y = rep(1:length(s1), groupsNo),
                       color = BLUB)

ggplot(df,aes(x = x, y = y,fill=factor(color))) + 
  geom_line(aes(group = items,color=factor(color)), size = 2) + 
  geom_tile(width = 0.6, height = 0.6, color = 'black') + 
  theme_void() + 
  geom_text(aes(label = items), size=5) +
  annotate(geom="text", x=unique(df$x), y=max(df$y)+0.5, label="testTEXT") +
  theme(legend.position = "none") +
  labs(
    y = "", x = "",
    title = "Plot for random data",
    subtitle = "text",
    caption = "text")

Upvotes: 1

Related Questions