ggplot2 stacked barplot, reorder its y-variables...

my dataset is available here for testing: Online CSV editor and viewer

basically, I want to have my y-variable OEE to be the starting from 0 %, which I achieve, for all, except Line 1. I am puzzled why Line 1 is an exception.

Code to produce the plot is as below:

OEE_Outputs_ByLine_Daily$Metrics <- as.factor(OEE_Outputs_ByLine_Daily$Metrics)
OEE_Outputs_ByLine_Daily$Metrics2 <- relevel(OEE_Outputs_ByLine_Daily$Metrics,'OEE')

  g <- ggplot(OEE_Outputs_ByLine_Daily, 
              aes(fill=Metrics2, y=Percent, x=Date)) +
    geom_bar(position="stack", stat="identity") +
    geom_col(position = position_stack(reverse = TRUE)) +
    facet_wrap(~Line, strip.position = "bottom") +
       labs(title="Different Metrics (%) vs Date",x="",y="")
  ggplotly(g)

Thanks.

Sorry, I do not see any fundamental difference between the y axis of Line 1 and any other plot. All of the plots seem to have a few negative values that dip below the zero level. Can you explain in more detail what you don't like about the Line 1 plot?

perhaps the printscreen is not that clear or my explanation is not clear. You can see for Line 2 to Line 5, its OEE is always start from 0%, but for Line 1, it is not the case. I hope the below printscreens can show clearly that Line 1's OEE does not start at 0%.

image

image

image

image

image

image

The problem seems to come from the ggplotly function. If I just look at the ggplot output, it looks as expected. I am not familiar enough with ggplotly to suggest a solution.

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5
library(plotly)
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
OEE_Outputs_ByLine_Daily <- read.csv("~/R/Play/file.csv")
OEE_Outputs_ByLine_Daily$Metrics <- as.factor(OEE_Outputs_ByLine_Daily$Metrics)
OEE_Outputs_ByLine_Daily$Metrics2 <- relevel(OEE_Outputs_ByLine_Daily$Metrics,'OEE')

g <- ggplot(OEE_Outputs_ByLine_Daily, 
            aes(fill=Metrics2, y=Percent, x=Date)) +
  geom_bar(position="stack", stat="identity") +
  geom_col(position = position_stack(reverse = TRUE)) +
  facet_wrap(~Line, strip.position = "bottom") +
  labs(title="Different Metrics (%) vs Date",x="",y="")
print(g)
#> Warning: Removed 423 rows containing missing values (position_stack).
#> Warning: Removed 423 rows containing missing values (position_stack).

Created on 2021-07-21 by the reprex package (v0.3.0)

Thanks for testing out..so the issue is with plotly.

I will raise a new issue on Plotly github, hope they are able to solve it, thanks.

is ggplot2 the only package which can do

facet_wrap

The problem I have here is using ggplot's facet_wrap, and next ggplotly, then I will have issue.
I am trying to switch to highchart in R, but it seems to be it is not possible to separate plots with

facet_wrap

like what ggplot2 is.

A faceted plot can be made directly with plotly but I have almost no experience working directly with plotly. The following code begins to make such a plot but the result has many problems. Among them are that the x axis tick mark labels are terrible and there is no legend. If I turn the legend on for each subplot, I get 5 copies of the legend. I'm sure there is a way to prevent that. The code may be the silliest possible way to make such a plot with plotly. As I said, I do not use that package.

library(plotly)
DAT <- read.csv("~/R/Play/file.csv")
DAT <- DAT %>% 
  mutate(Metrics = factor(Metrics, 
                          levels = c("OEE", "BreakDown", "ChangeOver", "Industrialization", 
                                     "NonPlannedStop", "PlannedStop", "Quality", "Scrap")))

ThePlot <- . %>% plot_ly() %>% 
  add_trace(x = ~Date, y = ~Percent, color = ~Metrics, type = "bar") %>% 
  add_annotations(
    text = ~unique(Line),
    x = 0.5,
    y = 1.10,
    yref = "paper",
    xref = "paper",
    xanchor = "middle",
    yanchor = "top",
    showarrow = FALSE,
    font = list(size = 10)
  ) %>% 
  layout(barmode = "stack", showlegend = FALSE)

DAT %>% group_by(Line) %>% 
  do(p = ThePlot(.)) %>% 
  subplot(nrows = 2, shareX = TRUE, shareY=TRUE)

thanks FJCC for trying out.. I have concluded that plotly is indeed terrible, and I decided to switch to HighChart, to pay for some price.


you can see that above picture which shows values of Quality when it is moused over at OEE. Quality is in fact at the top of OEE.

using plotly directly solved the issue. lesson learn is not to go to ggplot and then ggplotly.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.