本文介绍了如何同时使用上标和变量在轴标签与ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在n轴标签内一起使用一个变量(这里是矢量元素type)和一个包含上标(这里是m ^ 2)的单位。 data< - list(houses = data.frame(surface = c(450,320,280),
price = c(12,14,6)),
flats = data.frame(surface = c(45,89,63),
price = c(4,6,9)))
我使用表达式显示m ^ 2,
for(type in c('houses','flats')){
p< - ggplot(aes(x = surface,y = price ),data = data [[type]])+
geom_point()+
xlab(expression(paste('surface of this type /',m ^ {2})))
}
p
但是当我尝试在标签中添加变量时,当然,不行:
for(输入c('house','flats')){
p < - ggplot(aes(x = surface,y = price),data = data [[type]])+
geom_point()+
xlab(expression ,'/',m ^ {2})))
}
p
您有建议吗?
解决方案
它适用于 bquote
:
xlab(bquote('表面'〜。(type)〜'/'〜m ^ {2}))
I would like to use together a variable (here the vector element "type") and a unit containing a superscript (here m^2) inside n axis label.
data <- list(houses = data.frame(surface = c(450, 320, 280),
price = c(12, 14, 6)),
flats = data.frame(surface = c(45, 89, 63),
price = c(4, 6, 9)))
I achieve to display "m^2" using an expression,
for (type in c('houses', 'flats')){
p <- ggplot(aes(x = surface, y = price), data = data[[type]]) +
geom_point() +
xlab(expression(paste('surface of this type /', m^{2})))
}
p
but when I try to add the variable in the label, the following, of course, does not work :
for (type in c('houses', 'flats')){
p <- ggplot(aes(x = surface, y = price), data = data[[type]]) +
geom_point() +
xlab(expression(paste('surface of ', type, '/', m^{2})))
}
p
Would you have a suggestion ?
解决方案
It works with bquote
:
xlab(bquote('surface of' ~ .(type) ~ '/' ~ m^{2}))
这篇关于如何同时使用上标和变量在轴标签与ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!