R plotly忽略文本标签对齐hjust [英] R plotly ignoring text label alignment hjust

查看:60
本文介绍了R plotly忽略文本标签对齐hjust的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 ggplot2 3.0.0 的 plotly 4.8,并尝试将文本标签添加和对齐到我的散点图.但是,geom_text(aes(....), hjust = "left") 中的 plotly 似乎忽略了 hjust 参数.(也试过 hjust = 0.)

I am using plotly 4.8 with ggplot2 3.0.0, and trying to add and align text labels to my scatter plots. However, it seems the hjust parameter is being ignored by plotly in geom_text(aes(....), hjust = "left"). (Also tried hjust = 0.)

GGPLOT 输出

看到它在绘图窗口中渲染得很好,作为一个标签左对齐的 ggplot.

See it renders fine in plot window as a ggplot with labels left aligned.

情节输出

但是在转换中对齐丢失,文本居中.

But the alignment is lost in conversion, and the text is centered.

那么问题是,是否可以使用 plotly 修复这种对齐方式?

So the question, is fixing this alignment possible with plotly?

测试示例代码

library(ggplot2)
library(data.table)
library(plotly)

data(mtcars)

plotdata <- as.data.table(mtcars)
plotdata$carname <- rownames(mtcars)

# take a small demo subset
plotdata <- plotdata[1:10,]

gg <- ggplot(plotdata, aes(x = wt, y = mpg, label = carname)) +  
               geom_point()  + theme_minimal()
gg <- gg + geom_text(aes(label = carname),
                       size = 2,
                       hjust = "left")
print(gg)

# convert ggplot
p <- ggplotly(gg)
p

推荐答案

你只需要添加文本位置textposition = "right":

You just need to add text position textposition = "right":

ggplotly(p) %>% style(textposition = "right")

输出:

参考:https://github.com/ropensci/plotly/issues/769

这篇关于R plotly忽略文本标签对齐hjust的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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