gpt4 book ai didi

r - 在 ggplotly 中显示抖动

转载 作者:行者123 更新时间:2023-12-03 20:28:01 26 4
gpt4 key购买 nike

我试图建立一个带有因子轴的散点图,为可读性的值添加抖动。

我能够在 ggplot2 中获得想要的效果,但是当我尝试用 plotly 包裹它时,抖动并没有实现。有什么办法让这个工作吗?

正在发生的事情的一个例子。下面的 g 看起来不错,但是 p 然后失去了抖动。

data<-data.frame(cbind(
sample(c('level 1', 'level 2', 'level 3'), 100, replace = TRUE),
sample(c('level 1', 'level 2', 'level 3', 'level 4'), 100, replace = TRUE)))

names(data)<-c('factor1', 'factor2')

g <- ggplot(data, aes(x=factor1, y=factor2))
g <- g + geom_point(position = position_jitter(w = 0.2,h = 0.2))
#g <- g + geom_jitter(width = .2, height = .2)
g <- g +xlab('Category One')
g <- g +ylab('Category Two')

g

p <- ggplotly(g)
p

最佳答案

也许有解决办法。用整数替换你的因子,然后重新标记。

data<-data.frame(cbind(
sample(1:3, 100, replace = TRUE),
sample(1:4, 100, replace = TRUE)))

names(data)<-c('factor1', 'factor2')

g <- ggplot(data, aes(x=factor1, y=factor2))
g <- g + geom_point(position = position_jitter(w = 0.2,h = 0.2)) + scale_x_continuous("Factor 1", breaks = c(1,2,3)) + scale_y_continuous("Factor 2", breaks = c(1,2,3,4))
ggplotly(g)

重新标记 x 和 y 刻度
x <- list(
tickprefix = "Level"
)

y <- list(
tickprefix = "Level"
)

g %>% layout(xaxis = x, yaxis = y)

enter image description here

关于r - 在 ggplotly 中显示抖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35341419/

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