我尝试使用因子轴站起来绘制散点图,为可读性添加抖动值。
我能够在ggplot2中获得想要的效果,但是当我尝试用图形包装时,抖动并没有成功。有什么方法可以让它发挥作用吗?
正在进行的一个例子。下面的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
答案 0 :(得分:3)
可能有一种解决方法。将您的因子替换为整数,然后重新标记。
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)
答案 1 :(得分:-1)
这有效:
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))
p <- ggplotly(g)
x <- list(title = "Category One",tickmode = "array",tickvals = c(1,2,3) , ticktext = c("A", "B", "C"))
y <- list(title = "Category Two",tickmode = "array",tickvals = c(1,2,3,4) , ticktext = c("A", "B", "C", "D"))
p <- p %>% layout(xaxis = x, yaxis = y)
p