Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

RHTML custom visual, data loss results in misleading plot

Hello,

 

has anyone encountered the problem where the visual looks significantly different than when compiled in R?

My output clearly missed out on alot of data, as many bars are missing from the column plot. Data consists of less than 2000 obs.

 

This is my R-code, if anyone are interested:

 

source('./r_files/flatten_HTML.r')

############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly")
libraryRequireInstall("tidyverse")
libraryRequireInstall("scales")
libraryRequireInstall("lubridate")


################### Actual code ####################
# Prep maskin data --------------------------------------------------------
maskin = Values %>%
  mutate(Ferdig_Tid_Short = as.Date(Ferdig_Tid_Short),
         MaskinNr = as.character(MaskinNr)) %>% 
  mutate(
    year = year(Ferdig_Tid_Short),
    first = floor_date(x = today(),'year',),
    last = as.Date(ifelse(year(today())==year,
                          as.character(today()),
                          as.character((ceiling_date(x = today(),'year',)-days(1)))))
  ) %>%
  group_by(year,MaskinNr,MaskinGruppeNavn) %>% 
  summarise(
    maskintimer = sum(Reell_Maskintid),
    qyeartimer = as.numeric(max(last)-min(first))*24
  ) %>% 
  ungroup %>% 
  transmute(
    year = factor(year),
    MaskinNr = factor(MaskinNr, levels = unique(sort(as.numeric(Values$MaskinNr)))),
    maskingruppe = factor(MaskinGruppeNavn,
                          levels = c('65-90 tonn','120-400 tonn',
                                     '420-600 tonn',
                                     '1000-1500 tonn',
                                     'Pakningslegging')),
    diff = round(maskintimer/qyeartimer,3)
  );


# Lag plot ----------------------------------------------------------------
color_vec = c('#60D9BA','#287878','#01A001','#CFF06D','#00FF51','#FFFF00','#FF8000');


colors = maskin %>% 
  transmute(maskingruppe,
            MaskinNr = MaskinNr) %>%
  distinct() %>%
  arrange(MaskinNr) %>% 
  group_by(maskingruppe) %>% 
  mutate(color = map_chr(1:length(maskingruppe),function(x)color_vec[x])) %>% 
  pull(color);

g = maskin %>%
  filter(maskingruppe != 'Pakningslegging') %>% 
  ggplot(aes(year,diff,fill = MaskinNr)) +
  geom_col(position = position_dodge()) +
  facet_wrap(~maskingruppe) +
  theme_bw() +
  theme(plot.background = element_rect(fill = '#e7f0f0', color = '#e7f0f0'),
        legend.position="none") +
  xlab('') +
  ylab('') +
  scale_y_continuous (labels = scales::percent) +
  scale_fill_manual(values=colors);
####################################################

############# Create and save widget ###############
p = ggplotly(g);
internalSaveWidget(p, 'out.html');
####################################################

################ Reduce paddings ###################
ReadFullFileReplaceString('out.html', 'out.html', ',"padding":[0-9]*,', ',"padding":0,')
####################################################

 

 

 

  • Anonymous's avatar
    Anonymous
    3 years ago

    This would definitively be a part of the problem, thanks for letting me know! Unfortuneately, it didn't fix the issue. For example, if i restrict the data (PBI filter) to a year with few observations, it is still wrong (even after updating top "count":30000). I'm assuming there is some R function that isn't supported in R?

4 Replies

  • dm-p's avatar
    dm-p
    Super User

    Hi Anonymous,

    I don't do much with R, but with custom visuals in-general, they will cap at 1,000 data points by default unless the dataReductionAlgorithm is updated to declare your visual's data point limit (up to a maximum of 30,000).

    If you haven't tried this, here's the relevant section of documentation that explains the default limit and how to configure your capabilities.json accordingly.

    Hope this helps,

    Daniel

    • Anonymous's avatar
      Anonymous
      Not applicable

      This would definitively be a part of the problem, thanks for letting me know! Unfortuneately, it didn't fix the issue. For example, if i restrict the data (PBI filter) to a year with few observations, it is still wrong (even after updating top "count":30000). I'm assuming there is some R function that isn't supported in R?

      • dm-p's avatar
        dm-p
        Super User

        Unfortunately R isn't really my wheelhouse, so I can't offer anything to help on that side of things; just that the capabilities are usually a common sticking point for data being limited in the visual's data view. Hopefully someone else may be able to clarify that part for you 🤞

        All the best,

        Daniel