Graphically plotted chart does not display negative values

Background: I am working on updating a small Brilliant app to use plotly instead of ggplot2 to make the chart interactive. (Note: I'm relatively new to R).

Problem: I use a grouped histogram, which sometimes has a combination of positive and negative values. When using ggplot2 and renderPlot, my graph works as expected. However, when using plotly and renderPlotly, the bars in the chart show as positive. When I hang over the stripes, I see negative values.

Code example:

dat1 <- data.frame(
  Item = factor(c("Female","Female","Male","Male")),
  Attributes = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
  Score = c(13.53, 16.81, -16.24, 17.42)
)

build_chart <- function(df){

  p <- ggplot(df, aes(factor(Attributes), Score, fill=Item)) +
    geom_bar(stat="identity", position="dodge") +
    coord_flip()

  p
}

build_chart(dat1)
ggplotly(build_chart(dat1))

Illustration:

Desired Result (ggplot2):

ggplot2

result:

plotly

+4
1

, ggplotly() . , , . . API- plotly , ggplot , , ggplot

plot_ly(dat1, type = "bar", x=Score, y=Attributes, group=Item, orientation="h") 

enter image description here

+5

Source: https://habr.com/ru/post/1652393/


All Articles