ggplot中换行文本的行距 [英] Line spacing for wrapped text in ggplot

查看:83
本文介绍了ggplot中换行文本的行距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改包装的geom_text层中的行距.

 库(ggplot2)库(字符串)txt = c('一二三','四五六','七八九')p = ggplot(数据= NULL,aes(x = 1:3,y = 1:3,标签= str_wrap(txt,宽度= 3)))+geom_text()+ expand_limits(x = c(0.5,3.5),y = c(0.5,3.5)) 

但是 theme(text = element_text(lineheight = ...))无效,因为 theme 仅适用于

I need to change the line spacing in wrapped geom_text layer.

library(ggplot2)
library(stringr)
txt = c('one two three', 'four five six', 'seven eight nine')
p = ggplot(data=NULL, aes(x=1:3, y=1:3, label = str_wrap(txt, width = 3))) + 
  geom_text() + expand_limits(x = c(0.5, 3.5), y = c(0.5, 3.5))

But theme(text=element_text(lineheight = ...)) has no effect because theme only works for non-data components of the plot, so I'm not clear how to achieve this. Suggestions?

解决方案

Just use lineheight, e.g.:

ggplot(data = NULL, aes(x = 1:3, y = 1:3, 
                        label = str_wrap(txt, width = 3))) + 
  geom_text(lineheight = .5) + 
  expand_limits(x = c(0.5, 3.5), y = c(0.5, 3.5))

(s. ?geom_text)

这篇关于ggplot中换行文本的行距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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