我在ggplotly中使用geom_bar图,它使负数条变为正数。有什么想法可能是这种情况,尤其是如何解决这个问题?
library(ggplot2)
library(plotly)
dat1 <- data.frame(
sex = factor(c("Female","Female","Male","Male")),
time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(-13.53, 16.81, 16.24, 17.42)
)
# Bar graph, time on x-axis, color fill grouped by sex -- use position_dodge()
g <- ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +
geom_bar(stat="identity", position=position_dodge())
ggplotly(g)
为什么第一个竖线会在正方向上带有负值?
我使用的是最新版本:
plotly_3.4.13
ggplot2_2.1.0
最佳答案
如果将绘图对象写入另一个变量,则可以修改绘图属性,包括用于渲染绘图的“数据”。
对于您的特定示例,将此附加到您的代码中:
#create plotly object to manipulate
gly<-ggplotly(g)
#confirm existing data structure/values
gly$x$data[[1]]
# see $y has values of 13.53, 16.81 which corresponds to first groups absolute values
#assign to original data
gly$x$data[[1]]$y <- dat1$total_bill[grep("Female",dat1$sex)]
#could do for second group too if needed
gly$x$data[[2]]$y <- dat1$total_bill[grep("Male",dat1$sex)]
#to see ggplotly object with changes
gly
关于r - ggplotly在正方向绘制负条,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37274106/