在ggplot2标题表达式中使用italic()和一个变量 [英] Using italic() with a variable in ggplot2 title expression

查看:390
本文介绍了在ggplot2标题表达式中使用italic()和一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ggplot2 制作地图并尝试使用 expression() italic()使用一个字符串,我的地图按需要出来: c $ c $> plottitle < - 表达式(paste('示例地图','italic'''))

犯罪< - data.frame(state = tolower(rownames(USArrests) ),USArrests)
states_map< - map_data(state)
map< - ggplot(crime,aes(map_id = state))+
geom_map(aes(fill = Murder) ,
map = states_map)+
expand_limits(x = states_map $ long,
y = states_map $ lat)+
labs(title = plottitle)

map

但是,当我尝试做同样的事情,但使用一个对象而不是字符串时,该对象不计算为所需的字符串:

  word<  - 'italics'
plottitle2< - 表达式(粘贴('示例地图',它(aic(fill =谋杀),
map = states_map) )+
expand_limits(x = states_map $ long,
y = states_map $ lat)+
labs(title = plottitle2)

map

在应用<$ c $之前,如何获取向量 word 来评估c> italic()?

解决方案

bquote或substitute应该有效,

  a ='text'
plot(1,1,main = bquote(italic(。(a))))
plot(1,1,main = substitute(italic(x),list(x = a)))


When I make a map using ggplot2 and attempt to italicize part of the figure title using a combination of expression() and italic() using a string, my map comes out as desired:

plottitle <- expression(paste('Example map with ', italic('italics')))

crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
states_map <- map_data("state")
map <- ggplot(crimes, aes(map_id = state)) + 
  geom_map(aes(fill = Murder), 
           map = states_map) + 
  expand_limits(x = states_map$long, 
                y = states_map$lat) +
  labs(title = plottitle)

map

However, when I try to do the same thing, but use an object instead of a string, the object does not evaluate to the desired string:

word <- 'italics'
plottitle2 <- expression(paste('Example map with ', italic(word)))

map <- ggplot(crimes, aes(map_id = state)) + 
  geom_map(aes(fill = Murder), 
           map = states_map) + 
  expand_limits(x = states_map$long, 
                y = states_map$lat) +
  labs(title = plottitle2)

map

How can I get the vector word to evaluate before applying italic()?

解决方案

bquote or substitute should work,

 a = 'text' 
 plot(1,1, main=bquote(italic(.(a))))
 plot(1,1, main=substitute(italic(x), list(x=a)))

这篇关于在ggplot2标题表达式中使用italic()和一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆