首页 > 解决方案 > R: ggplot2 let the characters of geom_text exactly cover one X unit

问题描述

I want to highlight text based on the position in a string, for example if we have this text:

this is a really nice informative piece of text

Then I want to say let's draw a rectangle around positions 2 till 4:

t[his] is a really nice informative piece of text

I tried to do so in ggplot2 using the following code:

library(ggplot2)
library(dplyr)

box.data <- data.frame(
  start   = c(4,6,5,7,10,7),
  type    = c('BOX1.start', 'BOX1.start', 'BOX1.start','BOX1.end', 'BOX1.end', 'BOX1.end'),
  text.id = c(1,2,3,1,2,3)
)

text.data <- data.frame(
  x = rep(1,3),
  text.id = c(1,2,3),
  text = c('Thisissomerandomrandomrandomrandomtext1',
           'Thisissomerandomrandomrandomrandomtext2',
           'Thisissomerandomrandomrandomrandomtext3')
)


ggplot(data = text.data, aes(x = x, y = text.id)) + 
  scale_x_continuous(limits = c(1, nchar(as.character(text.data$text[1])))) +
  geom_text(label = text.data$text, hjust = 0, size = 3) +
  geom_line(data = box.data, aes(x = start, y = text.id, group = text.id, size = 3, alpha = 0.5, colour = 'red'))

This produces the following graph:
enter image description here

My method fails as a letter does not cover exactly one unit of the x-axis, is there any way to achieve this?

标签: rggplot2text

解决方案


I just figured out that I can split the string in characters and plot these, perhaps it is useful for someone else.

library(ggplot2)
library(dplyr)
library(splitstackshape)

# First remember the plotting window, which equals the text length
text.size = nchar(as.character(text.data$text[1]))

# Split the string into single characters, and adjust the X-position to the string position
text.data <- cSplit(text.data, 'text', sep = '', direction = 'long', stripWhite = FALSE) %>%
  group_by(text.id) %>%
  mutate(x1 = seq(1,n()))

# Plot each character and add highlights 
ggplot(data = text.data, aes(x = x1, y = text.id)) + 
  scale_x_continuous(limits = c(1, text.size)) +
  geom_text(aes(x = text.data$x1, y = text.data$text.id,  group = text.id, label = text)) +
  geom_line(data = box.data, aes(x = start, y = text.id, group = text.id, size = 3, alpha = 0.5, colour = 'red'))

Which produces this plot:
enter image description here

Perhaps the marking should extend a little but upwards and downwards, but that's an easy fix.


推荐阅读