Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Ploty: wrong y value with minus values

Tags:

r

ggplot2

plotly

I have a problem with R Plotly package:

When I would like to plot a barplot with a minus Y values, it creates wrong plot.

Here is the example:

dane<-data.frame(x=1:10,y=seq(-5,4),g=rep(c('A','B'),each=5))
dane$x<-as.factor(dane$x)

ggplot(data=dane,aes(x=x,y=y,fill=g)) +
  geom_bar(stat='identity', position = "identity")

ggplotly()

plot1

When I just simple plot with ggplot (without plotly), everything is fine:

ggplot(data=dane,aes(x=x,y=y,fill=g)) +
  geom_bar(stat='identity', position = "identity")

plot2

Is it a bug? How can I fix it?

like image 359
Slownz Avatar asked Aug 17 '26 08:08

Slownz


2 Answers

It´s a known bug: http://community.plot.ly/t/inversion-negative-values-in-ggplotly/875.

Even the newest development version did no fix for this, I just tried this. So you can follow the issue on: https://github.com/ropensci/plotly/issues/560

like image 111
J_F Avatar answered Aug 18 '26 23:08

J_F


But if you are open to use native plotly, this would give you the result you want:

library(dplyr)
dane_p <- dane %>% filter(g == "A")
dane_p2 <- dane %>% filter(g == "B")

p <- plot_ly(data=dane_p,
  x = x,
  y = y,
  name = "A",
  type = "bar")

p2 <- add_trace(p,
  data=dane_p2,
  x = x,
  y = y,
  name = "B",
  type = "bar")
p2
like image 21
MLavoie Avatar answered Aug 18 '26 23:08

MLavoie