# first plot I would like to have a subscript 1 and for the scond one a subscript 2
# One solution is to use bquote(). Use .() within bquote to get the value of objects or expressions.
plotf = function(title=expression("Test"~a )){
plot(cars)
title(title)
}
foo = expression(a[1], a^-2)
layout(matrix(1:2, nrow=1))
for(i in 1:2){
plotf(title=bquote("Test"~.(foo[[i]]) )) #bquote() to utilize the iteration
}
# using astrisk instead of tilde
for(i in 1:2){
plotf(title=bquote("Test"*a[.(i)]) )
}
Using two datasets
# graph using two datasets
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.1 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
data1=mtcars %>% filter(cyl>7)
data2=mtcars %>% filter(cyl<7)
ggplot() +
geom_point(data = data1, aes(x=hp, y=disp),color="gray",size=1)+
geom_line(data = data2, aes(x=hp, y=disp ) ) # must include argument label "data"
Convert a Vector to String and back
#create vector
x <- c("Andy", "Bernard", "Caleb", "Dan", "Eric", "Frank", "Greg")
#convert vector to string
new_string <- toString(x)
#view string
new_string
## [1] "Andy, Bernard, Caleb, Dan, Eric, Frank, Greg"
#create vector
x <- c("Andy", "Bernard", "Caleb", "Dan", "Eric", "Frank", "Greg")
#convert vector to string
new_string <- paste(x, collapse = " ")
#view string
new_string
## [1] "Andy Bernard Caleb Dan Eric Frank Greg"
Convert string to vector
# R convert string to vector
string1 <- "This is my string"
strsplit(string1, "\\s+")[[1]]
## [1] "This" "is" "my" "string"
string1 <- "This is my string"
scan(text = string1, what = "")
## [1] "This" "is" "my" "string"