duttashi / visualizer

My experiments in data visualizations. Feel free to show your :heart: by giving a star :star:
MIT License
15 stars 1 forks source link

Annotation in ggplot2 #30

Open duttashi opened 6 years ago

duttashi commented 6 years ago
duttashi commented 6 years ago

Annotation is the art of adding text labels to a visualization. In this first example, I provide how to

How to annotate a basic plot

Example 1

library(ggplot2)

set.seed(10)
df.rnorm <- data.frame(rnorm = rnorm(10000, mean = 5))

# Here we have a histogram with a dashed line at the mean. The mean is 5.
ggplot(data = df.rnorm, aes(x = rnorm)) +
  geom_histogram(bins = 50) +
  geom_vline(xintercept = 5, color = "red", linetype = "dashed")

plot

Add an annotation with the annotate() function

ggplot(data = df.rnorm, aes(x = rnorm)) +
  geom_histogram(bins = 50) +
  geom_vline(xintercept = 5, color = "red", linetype = "dashed") +
  annotate("text", label = "Mean = 5", x = 4.5, y = 170, color = "white")

finalPlot

duttashi commented 6 years ago

How to place an annotation onto a ggplot with TRUE and FALSE on the y-axis.

This question was originally asked on SO

df1 <- data.frame(y=c(TRUE,TRUE,FALSE,TRUE,FALSE,TRUE,FALSE,FALSE,FALSE),
                  x=c(1,2,3,4,5,6,7,8,9))
df2 <- data.frame(y=c(1,1,0,1,0,1,0,0,0),
                  x=c(1,2,3,4,5,6,7,8,9))

# yplace <- mean(as.numeric(levels(as.factor(df$y))))
yplace1 <- 1.5
yplace2 <- 0.5

# Solution
getYplace <- function(Y) {
  if (is.numeric(Y)) {
    res <- mean(unique(Y))
  } else {
    res <- 1.5
  }
  return(res)
}

library(ggplot2)
ggplot(df1, aes(x, y)) + 
  geom_point() +
  annotate("text", label = "read me", x = mean(df1$x), y = getYplace(df1$y))
ggplot(df2, aes(x, y)) + 
  geom_point() +
  annotate("text", label = "read me", x = mean(df2$x), y = getYplace(df2$y))
duttashi commented 6 years ago

How to add overall counts for each tile as well as the proportion of each tile's counts

that correspond to male and female ratings. There are 45 tiles, and my very cumbersome solution is to add 90 annotate layers. This question was originally asked on SO

Solution

library(ggplot2)

d <- expand.grid(x=1:5,y=1:5)
d$z <- rnorm(nrow(d))
d$f <- sample(LETTERS, size = nrow(d), replace = TRUE)
d$m <- sample(LETTERS, size = nrow(d), replace = TRUE)

dd <- tidyr::gather(d, gender, label, -x,-y,-z)
ggplot(dd, aes(x,y,fill=z)) + geom_tile() +
  geom_text(aes(label=label,colour=gender, hjust = ifelse(gender=="f",0,1))) +
  scale_colour_manual("gender", values=c("pink","green"))

Plot

duttashi commented 6 years ago

How to add annotation to the centre of plot

SO

Solution

library(grid) # for textGrob
library(ggplot2)
qplot(1,1) + annotation_custom(textGrob("Annotation in the center"), 
                               xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf) 

Plot

duttashi commented 6 years ago

How to add an annotation (a label) to a ggplot2 plot (using R) with a position relative to another element, namely, above a bar in a bar chart.

SO

Solution

d <- data.frame(x = c("var1", "var2"), y = c(.2, 100))
library(ggplot2)
ggplot(d, aes(x = x, y = y)) +
  geom_col() + geom_text(aes( label = "hi!"), vjust = -0.5)

Plot

duttashi commented 6 years ago

How to add text to ggplot in a loop

SO

Solution

Use aes_string() in geom_text()

library(ggplot2)
gp <- list()

for(k in 1:3) {
  gp[[k]] <- ggplot() + 
    geom_text(aes_string(x = 2, y = 1, label = k), colour = "#1874CD")
}
gp[1]
gp[2]
gp[3]