How to Combine Character String & Expressions in Plot Text in R (2 Examples) | Base R vs. ggplot2

preview_player
Показать описание
R code of this video:

y = c(1, 3, 5, 2, 7))

plot(data, # Draw plot without expressions
main = "My Main Title")

plot(data, # Draw plot with expressions in main title
main = expression("My Main Title"["No. 2"] ~ alpha ^ beta))

library("ggplot2")

ggp <- ggplot(data, aes(x, y)) + # Create ggplot2 scatterplot
geom_point()
ggp # Draw ggplot2 scatterplot

ggp + # Add main title without expressions
ggtitle("My Main Title")

ggp + # Add main title with expressions
ggtitle(expression("My Main Title"["No. 2"] ~ alpha ^ beta))

ggp + # Annotate text with expressions inside of plot
annotate("text",
x = 2,
y = 4,
label = expression("My Text"["No. 2"] ~ alpha ^ beta))

ggp +
annotate("text",
x = 2.2,
y = 4,
label = expression("My Text"["No. 2"] ~ alpha ^ beta),
col = 2,
size = 5)

Follow me on Social Media:

Рекомендации по теме
Комментарии
Автор

I getting error below from 8 to 9

plot(data, # Draw plot with expressions in main title
+ main = expression("My Main Title"["No. 2"] ~ alpha ^ beta))
Error in curve(expr = x, from = from, to = to, xlim = xlim, ylab = ylab, :
'expr' did not evaluate to an object of length 'n'
In addition: Warning message:
In x(x) : data set ‘x’ not found

generalsifr
Автор

Nice but I would like to see examples with the symbols inside text no just at the end.

haraldurkarlsson