Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colorize parts of the title in a plot

Is it possible to colorize parts of the title in a plot?

x = 1:10 y = 1:10 plot(x, y, main="title (slope=1)") 

In this plot I'd like to change the color of slope=1 to red.

like image 552
R_User Avatar asked Jun 13 '13 09:06

R_User


People also ask

How do you specify colors in a plot?

To specify colors with either approach, call the desired plotting function with an output argument so you can access the individual plot objects. Then set properties on the plot object you want to change. For example, create a scatter plot with 100-point filled markers.

How do you change the color of a title in R?

To change the color of Title and Subtitle, We simply add a color parameter to element_text() function. All others are the same as the above implementation. Here we set the value of the color of title as green and the value of subtitle as red.

How do you color a plot by a variable in R?

One of the ways to add color to scatter plot by a variable is to use color argument inside global aes() function with the variable we want to color with. In this scatter plot we color the points by the origin airport using color=origin. The color argument has added colors to scatterplot with default colors by ggplot2.

How do you make a title in R?

title() function in R Language is used to add main title and axis title to a graph. This function can also be used to modify the existing titles. Syntax: title(main = NULL, sub = NULL, xlab = NULL, ylab = NULL, …)


2 Answers

This is a quite simple solution to your problem:

plot(x, y) title(expression("title (" * phantom("slope=1)") * ")"), col.main = "black") title(expression(phantom("title (") * "slope=1"), col.main = "red") 

enter image description here

like image 167
Alpha Avatar answered Sep 23 '22 11:09

Alpha


A solution for ggplot2 plots using the ggtext package

library(ggplot2) # devtools::install_github("clauswilke/ggtext") library(ggtext)  p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) +    geom_point(size = 3)  p +    labs(title = "New plot <b style='color:#009E73'>title</b>",         subtitle = "A <b style='color:#D55E00'>subtitle</b>") +   theme_classic(base_size = 24) +   theme(plot.title = element_markdown(lineheight = 1.1),         plot.subtitle = element_markdown(lineheight = 1.1)) 

Created on 2019-08-11 by the reprex package (v0.3.0)

like image 31
Tung Avatar answered Sep 22 '22 11:09

Tung