如何将数学字符(菱形)添加到 ggplot 标题
How to add a math character (lozenge) to a ggplot title
我正在尝试将 \lozenge
添加到 ggplot2 标题。一些示例代码是:
tmp = data.frame(x = rnorm(100), y = rnorm(100))
ggplot(tmp, aes(x, y)) +
geom_point() +
ggtitle(expression(lozenge))
但是我无法让菱形出现。它只是打印单词 lozenge。我还需要能够改变菱形的颜色。
你可以试试:
ggplot(tmp, aes(x, y)) +
geom_point() +
ggtitle(bquote(symbol("0")))
要更改其颜色,您可以添加 theme
参数。
例如,对于红色菱形:
ggplot(tmp, aes(x, y)) +
geom_point() +
ggtitle(bquote(symbol("0"))) +
theme(plot.title=element_text(color="red"))
举例:
编辑
如果你想有一个bi-color的标题,按照罗兰this question的解决方法,方法如下:
# assign your plot object to a variable
p<-ggplot(tmp, aes(x, y)) + geom_point() + ggtitle(bquote(paste("some text ",symbol("0")," some more text",sep="")))
# get the "Grob" object
grob_p<- ggplotGrob(p)
# modify the text and text colors. Here, you have to use the hexadecimal code for the lozenge
grob_p[[1]][[8]]$label<-c("some text ", bquote("\U0025CA"), " some more text")
grob_p[[1]][[8]]$gp$col<-c("black","red","black")
# adjust the coordinates of the different strings
grob_p[[1]][[8]]$x<-unit(c(0.41,0.5,0.63),"npc")
# plot the modified object
plot(grob_p)
我正在尝试将 \lozenge
添加到 ggplot2 标题。一些示例代码是:
tmp = data.frame(x = rnorm(100), y = rnorm(100))
ggplot(tmp, aes(x, y)) +
geom_point() +
ggtitle(expression(lozenge))
但是我无法让菱形出现。它只是打印单词 lozenge。我还需要能够改变菱形的颜色。
你可以试试:
ggplot(tmp, aes(x, y)) +
geom_point() +
ggtitle(bquote(symbol("0")))
要更改其颜色,您可以添加 theme
参数。
例如,对于红色菱形:
ggplot(tmp, aes(x, y)) +
geom_point() +
ggtitle(bquote(symbol("0"))) +
theme(plot.title=element_text(color="red"))
举例:
编辑
如果你想有一个bi-color的标题,按照罗兰this question的解决方法,方法如下:
# assign your plot object to a variable
p<-ggplot(tmp, aes(x, y)) + geom_point() + ggtitle(bquote(paste("some text ",symbol("0")," some more text",sep="")))
# get the "Grob" object
grob_p<- ggplotGrob(p)
# modify the text and text colors. Here, you have to use the hexadecimal code for the lozenge
grob_p[[1]][[8]]$label<-c("some text ", bquote("\U0025CA"), " some more text")
grob_p[[1]][[8]]$gp$col<-c("black","red","black")
# adjust the coordinates of the different strings
grob_p[[1]][[8]]$x<-unit(c(0.41,0.5,0.63),"npc")
# plot the modified object
plot(grob_p)