Тёмный

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

Statistics Globe
Подписаться 30 тыс.
Просмотров 453
50% 1

How to add a text with expressions to a plot in the R programming language. More details: statisticsglob...
R code of this video:
data <- data.frame(x = 1:5, # Create example data frame
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))
install.packages("ggplot2") # Install & load ggplot2
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:
Facebook - Statistics Globe Page: / statisticsglobecom
Facebook - R Programming Group for Discussions & Questions: / statisticsglobe
Facebook - Python Programming Group for Discussions & Questions: / statisticsglobepython
LinkedIn - Statistics Globe Page: / statisticsglobe
LinkedIn - R Programming Group for Discussions & Questions: / 12555223
LinkedIn - Python Programming Group for Discussions & Questions: / 12673534
Twitter: / joachimschork
Music by bensound.com

Опубликовано:

 

13 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 10   
@larissacury7714
@larissacury7714 Год назад
thank you!!!
@matthias.statisticsglobe
@matthias.statisticsglobe Год назад
You're welcome Larissa! Thanks for the feedback and your support!
@generalsifr2995
@generalsifr2995 Год назад
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
@cansustatisticsglobe
@cansustatisticsglobe Год назад
Hello, Do you still any help? Did you put a" + " after "data, " or is it due to copy-pasting here? Otherwise, your code is correct. One thing that comes to my mind is that your data is not defined well. It looks like it can't read the data. Regards, Cansu
@haraldurkarlsson1147
@haraldurkarlsson1147 4 месяца назад
Nice but I would like to see examples with the symbols inside text no just at the end.
@StatisticsGlobe
@StatisticsGlobe 4 месяца назад
Hey Haraldur, thanks for the kind comment. I'll keep this in mind for a future video.
@haraldurkarlsson1147
@haraldurkarlsson1147 4 месяца назад
@@StatisticsGlobe I figured it out. You surround the symbol in a pair of ~. E.g., title = expression("This is ~gamma~ radiation."). However, I think there must be a simpler way perhaps using the glue package.
@StatisticsGlobe
@StatisticsGlobe 4 месяца назад
Thanks for sharing, glad you found a solution!
@santosh6148
@santosh6148 Год назад
Nice
@matthias.statisticsglobe
@matthias.statisticsglobe Год назад
Thanks for the feedback:)
Далее
All Rust string types explained
22:13
Просмотров 176 тыс.
This Is Why Python Data Classes Are Awesome
22:19
Просмотров 809 тыс.
Best Data Science Books for Beginners 📚
16:13
Просмотров 355 тыс.
Using Glue with R
9:17
Просмотров 54