How to underline Text in an R Plot? [duplicate] - r

I am trying to underline the mean value in the following plot:
dummy <- c(4, 9, 6, 5, 3)
barplot(dummy)
text(4, 8,paste('Average value', mean(dummy)))
I tried using underline() but it says it could not find the function.
text(4, 8,paste('Average value', underline(mean(dummy))))
Error:
could not find function "underline"
I am using : R version 3.1.0

Like this:
text(4, 8, bquote("Average value"~underline(.(mean(dummy)))))
or if you want the whole text underlined:
text(4, 8, bquote(underline("Average value"~.(mean(dummy)))))
Note use of bquote and .(x) to insert the value of a variable in the expression.

I could not access the link provided by #EddieSanders but I think this link is probably 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)
EDIT:
This will underline just the mean value:
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))))

Related

When I run my code in R it works, but if I try to knit it into a pdf, it just fails

I have been trying to get the code below to knit. This is for a lab so it isn't much code.
plot(cars$speed, cars$dist)
library(ggplot2)
ggplot(cars, aes(x=speed, y=dist)) + geom_point()
abline(lm(cars$speed ~ cars$dist), col ="red")
cor(cars$speed, cars$dist, use="complete.obs")
myVector = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
MyFunction <- function(x) {
y <- x*5
return(y)
}
MyFunction(myVector)
Error:
Error in value [[3L]] (cond) : invalid graphics states Calls: ... TryCatch -> tryCatchList -> tryCatchOne ->
By putting the abline() directly after plot() I get it to knit a pdf
library(ggplot2)
plot(cars$speed, cars$dist)
abline(lm(cars$speed ~ cars$dist), col ="red")
ggplot(cars, aes(x=speed, y=dist)) + geom_point()
cor(cars$speed, cars$dist, use="complete.obs")
myVector = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
MyFunction <- function(x) {
y <- x*5
return(y)
}
MyFunction(myVector)

Automatically insert whitespace in RStudio script

In the same ways that lines can be correctly indented using Ctrl + I or Cmd + I, is there a shortcut to automatically insert correct whitespace in RStudio scripts?
For example, for this:
df<-data.frame(x=c(1,2,3,4,5),y=c(3,4,5,6,7))
RStudio gives information saying "expected whitespace around '<-' operator" and "expected whitespace around '=' operator". Is there a shortcut to get this:
df <- data.frame(x = c(1, 2, 3, 4, 5), y = c(3, 4, 5, 6, 7))
Under RStudio you can select the code and type ctrl+shift+A for code reformatting, see RStudio shortcuts.
Result:
df <- data.frame(x = c(1, 2, 3, 4, 5), y = c(3, 4, 5, 6, 7))

Passing vector elements to call as consecutive arguments

Given function and test vector:
multiply_stuff <- function(...) {
arguments <- list(...)
Reduce(f = `*`, x = arguments)
}
test_vec <- c(1, 20, 3, 40, 5, 60)
I would like to create an unevaluated call automatically listing all arguments of the passed vector. In this example that would be equivalent of expression:
call("multiply_stuff",
test_vec[1],
test_vec[2],
test_vec[3],
test_vec[4],
test_vec[5],
test_vec[6])
Attempts
For instance for the vector:
test_vec_B <- c(1, 5, 6, 8, 9, 11, 12, 14, 20, 11)
I would like to automatically list all test_vec_B arguments within call("multiply_stuff",...). Naturally this won't work:
call("multiply_stuff", test_vec_B)
call("multiply_stuff", list(test_vec_B))
Desired results
Unevaluated expression equivalent to:
call(
"multiply_stuff",
test_vec_B[1],
test_vec_B[2],
test_vec_B[3],
test_vec_B[4],
test_vec_B[5],
test_vec_B[6],
test_vec_B[7],
test_vec_B[8],
test_vec_B[9],
test_vec_B[10]
)
You can create a call object and then add arguments to it:
multiply_stuff <- function(...) {
arguments <- list(...)
Reduce(f = `*`, x = arguments)
}
test_vec_B <- c(1, 5, 6, 8, 9, 11, 12, 14, 20, 11)
get_call <- function(f, arg_vector){
my_call <- call(f)
my_call[2:(length(arg_vector) + 1)] <- arg_vector
return(my_call)
}
multiply_stuff(1, 5, 6, 8, 9, 11, 12, 14, 20, 11)
[1] 878169600
test_call <- get_call("multiply_stuff", test_vec_B)
eval(test_call)
[1] 878169600
Explanation: when you create a call object, you can access/modify the function and its arguments by index just like usual. Index 1 is the function call, indices from 2 onwards are arguments.Run to verify:
test_call2 <- call("sum", 1, 2)
test_call2[1]
test_call2[2]
test_call2[3]
eval(test_call2)
test_call2[3] <- 1234
eval(test_call2)

Underline Text in a barplot in R

I am trying to underline the mean value in the following plot:
dummy <- c(4, 9, 6, 5, 3)
barplot(dummy)
text(4, 8,paste('Average value', mean(dummy)))
I tried using underline() but it says it could not find the function.
text(4, 8,paste('Average value', underline(mean(dummy))))
Error:
could not find function "underline"
I am using : R version 3.1.0
Like this:
text(4, 8, bquote("Average value"~underline(.(mean(dummy)))))
or if you want the whole text underlined:
text(4, 8, bquote(underline("Average value"~.(mean(dummy)))))
Note use of bquote and .(x) to insert the value of a variable in the expression.
I could not access the link provided by #EddieSanders but I think this link is probably 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)
EDIT:
This will underline just the mean value:
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))))

Exporting multiple panels of plots and data to *.png (in the style layout() works within R)

I'm trying to export multiple panels of data and 1 plot as a single image using either the .png device or .pdf device and I'm not meeting with any success. I can produce the image that I want within R using the R native plotting device, but when I try to produce the same image directly, I get results that I do not anticipate.
Here is my example code
testMat <- matrix(1:20, ncol = 5)## create data
testMatDF <- as.data.frame(testMat)
names(testMatDF) <- c("Hey there", "Column 2",
"Some * Symbols", "And ^ More",
"Final Column")
rownames(testMatDF) <- paste("Group", 1:4)
library(gplots) ## gplots needed for textplot()
layout(matrix(c(1, 1, 2, 3, 3, 3), 2, 3, byrow = TRUE))
curve(dnorm, -3, 4)
textplot(testMat)
textplot(testMatDF)
## produces what I want within R
layout(matrix(c(1, 1, 2, 3, 3, 3), 2, 3, byrow = TRUE))
png(file='plot1.png')
curve(dnorm, -3, 4)
textplot(testMat)
textplot(testMatDF)
dev.off()
## only the last function texplot(testMatDF) gets output, not what I anticipated
I've also tried the mfrow() graphical parameter without success.
par(mfrow= c(3, 1))
png(file='plot2.png')
curve(dnorm, -3, 4)
textplot(testMat)
textplot(testMatDF)
dev.off()
## only the last function texplot(testMatDF) gets output
If you move your calls to par or layout after you open your graphics device, it should work correctly.
png(file='plot2.png')
par(mfrow= c(3, 1))
curve(dnorm, -3, 4)
textplot(testMat)
textplot(testMatDF)
dev.off()

Resources