Underscore text in R

I am trying to emphasize the average in the following figure:

dummy <- c(4, 9, 6, 5, 3) barplot(dummy) text(4, 8,paste('Average value', mean(dummy))) 

I tried using underline() , but it says that it cannot find this function.

 text(4, 8,paste('Average value', underline(mean(dummy)))) 

Mistake:

 could not find function "underline" 

I use: R version 3.1.0

+5
r plot
source share
2 answers

Like this:

 text(4, 8, bquote("Average value"~underline(.(mean(dummy))))) 

or if you want to underline the entire text:

 text(4, 8, bquote(underline("Average value"~.(mean(dummy))))) 

Note the use of bquote and .(x) to insert the value of a variable into the expression.

+9
source share

I was unable to access the link provided by @EddieSanders, but I think this link probably refers to the same solution: http://tolstoy.newcastle.edu.au/R/help/02a/0471.html

 underlined <- function(x, y, label, ...){ text(x, y, label, ...) sw <- strwidth(label) sh <- strheight(label) lines(x + c(-sw/2, sw/2), rep(y - 1.5*sh/2, 2)) } dummy <- c(4, 9, 6, 5, 3) barplot(dummy) text(4, 8, underlined(4,8,paste('Average value', mean(dummy))), font=2) 

enter image description here

EDIT:

This will only mean the average:

 underlined <- function(x, y, label, ...){ text(x, y, label, ...) sw <- strwidth(label) sh <- strheight(label) lines(x + c(-sw/2, sw/2), rep(y - 1.5*sh/2, 2)) } dummy <- c(4, 9, 6, 5, 3) barplot(dummy) text(4, 8, paste('Average value', underlined(4.9,8,mean(dummy)))) 
+3
source share

All Articles