cyc
cyc

Reputation: 13

how to add colored tick label to ggplot2?

I am new to R and recently generate this bubble heatmap using ggplot2. I would like to add colored ticks to top of my figure. I have 7 different colors on the x axis and would like to add a set of colored squares matching to the text, to top of the chart and underneath the label. Thank you and appreciated any help. Please see the link for the data. Text https://drive.google.com/file/d/13mPQaCmhb6pABQ102srafQPryc6r8hNg/view?usp=sharing

Here is my code,

library(ggplot2)
library(ggcorrplot)
library(reshape2)
bb<-c(1, 1.3, 3)
labels<-c("0.1","0.05", "0.001")
x<-c("Blue", "Magenta", "Yellow", "Brown", "Pink", "Red", "Grey")
 ggplot(df, aes(x= Var2, y = Var1))+
  geom_tile(fill = "white", color = "gray")+
  geom_point(aes(color = cor, size = log.P))+
  labs(color="Correlation")+
  scale_size_continuous(range = c(1,12), name = "P value",
                        guide = guide_legend(override.aes = list(colour = "grey")), breaks=bb, labels=labels)+
  scale_x_discrete(position = "top", limits=x)+ 
  geom_text(aes(label = cor), color = "black", size = 4, vjust=2.5)+
  scale_color_gradient2(low = "blue", mid="white", high = "red")+theme_minimal()+
  theme(axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.x=element_text(size=15, angle = 40, hjust=0.1, vjust = 0.1, color=x),
        axis.text.y=element_text(size=15), 
        legend.text=element_text(size=15),
        legend.title=element_text(size=15),
        legend.key.height = unit(0.7,"cm"))

enter image description here

(there are 7 different color modules on x axis)

Upvotes: 1

Views: 339

Answers (1)

stefan
stefan

Reputation: 123963

One option to add some colored squares beneath your colored labels would be via an additional geom_text layer. As the label I use a filled square with UTF-8 code "\u25A0".

Using some fake random data to mimic your real data:

library(ggplot2)

ggplot(df, aes(x = Var2, y = Var1)) +
  geom_tile(fill = "white", color = "gray") +
  geom_point(aes(color = cor, size = log.P)) +
  labs(color = "Correlation") +
  scale_size_continuous(
    range = c(1, 12), name = "P value",
    guide = guide_legend(override.aes = list(colour = "grey")), breaks = bb, labels = labels
  ) +
  scale_x_discrete(position = "top", limits = x) +
  geom_text(aes(label = cor), color = "black", size = 4, vjust = 2.5) +
  # Add square ticks
  geom_text(data = data.frame(x = x, y = 7.5), aes(x = x, y = y), color = x, label = "\u25A0", size = 6, vjust = 0) +
  scale_color_gradient2(low = "blue", mid = "white", high = "red") +
  theme_minimal() +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.x = element_text(size = 15, angle = 40, hjust = 0.1, vjust = 0.1, color = x),
    axis.text.y = element_text(size = 15),
    legend.text = element_text(size = 15),
    legend.title = element_text(size = 15),
    legend.key.height = unit(0.7, "cm"),
    panel.grid.major.x = element_blank()
  )

DATA

set.seed(123)

x <- c("Blue", "Magenta", "Yellow", "Brown", "Pink", "Red", "Grey")

df <- data.frame(
  Var2 = rep(x, 7),
  Var1 = rep(1:7, each = 7),
  cor = round(runif(49, -1, 1), 2),
  log.P = runif(49, 0, 10)
)

Upvotes: 2

Related Questions