gpt4 book ai didi

R plotly忽略文本标签对齐hjust

转载 作者:行者123 更新时间:2023-12-01 10:21:15 24 4
gpt4 key购买 nike

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

GGPLOT 输出

看到它在绘图窗口中呈现为一个 ggplot,标签左对齐。

ggplot left align chart example

情节输出

但是在转换中对齐丢失,并且文本居中。

plotly center chart example

所以问题是,是否可以通过 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" :

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

输出:

Output from Plotly

引用: https://github.com/ropensci/plotly/issues/769

关于R plotly忽略文本标签对齐hjust,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52077666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com