hafen / trelliscopejs

TrelliscopeJS R Package
https://hafen.github.io/trelliscopejs
Other
263 stars 36 forks source link

Error using facet_trelliscope about naming cognostic columns #63

Closed Roctober92 closed 5 years ago

Roctober92 commented 6 years ago

We are trying to run facet_trelliscope() and we get an error shown below. We can't quite figure out what to do based on the error. We can get the examples in the help file to run correctly.

#> Error in names(cog_desc) <- paste0(non_unique_col, "_", names(cog_desc)): 'names' attribute [1] must be the same length as the vector [0]

See the reproducible example below.

#######################################
# In depth data analysis using 
#######################################
library(trelliscopejs)
library(tidyverse)
# get_data
#snow_data <- read_csv("data/full_snow.csv")

# set theme
theme_set(theme_bw())

example <- structure(list(date = structure(c(12798, 12815, 13149, 13180, 7227), class = "Date"), 
                          start_snow_depth = c(55L, 61L, 57L, 68L, 1L), 
                          data_type = c("imputed", "imputed", "imputed", "imputed", "imputed"), 
                          station_name = c("Arapaho Ridge", "Arapaho Ridge", "Arapaho Ridge", "Arapaho Ridge", "Bear Lake")), 
                     row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"), 
                     spec = structure(list(cols = structure(list(date = structure(list(format = ""), 
                    .Names = "format", class = c("collector_date", "collector")), 
                    station_name = structure(list(), class = c("collector_character", "collector")), 
                    station_id = structure(list(), class = c("collector_integer",  "collector")), 
                    start_snow_water = structure(list(), class = c("collector_double",  "collector")), 
                    start_snow_depth = structure(list(), class = c("collector_integer", "collector")), 
                    max_temp_mean = structure(list(), class = c("collector_integer", "collector")), 
                    min_temp_mean = structure(list(), class = c("collector_integer", "collector")), 
                    start = structure(list(format = ""), .Names = "format", class = c("collector_date", "collector")), 
                    end = structure(list(format = ""), .Names = "format", class = c("collector_date", "collector")), latitude = structure(list(), 
                    class = c("collector_double", "collector")), longitude = structure(list(), class = c("collector_double", "collector")), 
                    elev = structure(list(), class = c("collector_integer", "collector")), county = structure(list(), 
                    class = c("collector_character", "collector")), mon = structure(list(), class = c("collector_character", "collector")), 
                    year = structure(list(), class = c("collector_double", "collector")), avg_temp = structure(list(), 
                    class = c("collector_double", "collector")), data_type = structure(list(), class = c("collector_character", "collector"))), 
                    .Names = c("date", "station_name", "station_id", "start_snow_water", "start_snow_depth", "max_temp_mean", "min_temp_mean", 
                               "start", "end", "latitude", "longitude", "elev", "county", "mon", "year", "avg_temp", "data_type")), 
                    default = structure(list(), class = c("collector_guess",  "collector"))), .Names = c("cols", "default"), class = "col_spec"), 
                    .Names = c("date", "start_snow_depth", "data_type", "station_name"))

# Make app
example %>% 
  ggplot(aes(x = date, y = start_snow_depth, color = data_type)) +
  geom_point() +
  facet_wrap(~station_name, nrow = 29, ncol = 4)


example %>% 
  ggplot(aes(x = date, y = start_snow_depth, color = data_type)) +
  geom_point() +
  facet_trelliscope(~station_name)
#> using data from the first layer
#> Error in names(cog_desc) <- paste0(non_unique_col, "_", names(cog_desc)): 'names' attribute [1] must be the same length as the vector [0]

Created on 2018-06-27 by the reprex package (v0.2.0).

jpn5089 commented 5 years ago

Has anyone found of solution to this? I think it may have to do with an updated version of ggplot2 or trelliscopejs

Roctober92 commented 5 years ago

@jpn5089 I have not found a solution to it yet, nor has the author I believe. Do you think I need to update ggplot2? Or that that trelliscopejs was not written for the updated version I have?

jpn5089 commented 5 years ago

@Roctober92 I just installed ggplot2 (3.0.0) and trelliscopejs (0.1.13) and ran this code successfully

`library(trelliscopejs) library(ggplot2)

x <- factor(LETTERS[1:4]); names(x) <- letters[1:4]

data <- data.frame(var = rep(x,10), x = seq(1,40,1), y = seq(1,200,5))

ggplot(data, aes(x,y)) + geom_point() + facet_trelliscope(~var, path = "C://R Projects/") `

jpn5089 commented 5 years ago

@Roctober92 I think it has something to do with the x-axis variable being of some sort of class date. See 2 examples below:

x <- factor(LETTERS[1:4]); names(x) <- letters[1:4]

data_year_date <- data.frame(year = structure(seq(1950,1989,1), class = "Date"), var = rep(x,10), y = seq(1,200,5))

ggplot(data_year_date, aes(year,y)) + geom_point() + facet_trelliscope(~var)

data_year_character <- data.frame(year = structure(seq(1950,1989,1), class = "Date"), var = rep(x,10), y = seq(1,200,5)) %>% mutate(year = as.character(year))

ggplot(data_year_character, aes(year,y)) + geom_point() + facet_trelliscope(~var)

dinhani commented 5 years ago

I am having the same issue just by having a Date column in the dataframe sent to ggplot2.

jpn5089 commented 5 years ago

@dinhani i've found that ggplot 2.2.1 and trelliscopejs 0.1.11 allow you to get the plots to render correctly, if you happen to have those package versions located in older projects, etc

JiaxiangBU commented 5 years ago

Hi @dinhani, @Roctober92, I get the same bug because of time variable as the x-axis. The temporal solution is to check the data type of time variable, like t = as.numeric(t). But in this way, I geta ugly x-axis.

hafen commented 5 years ago

Looking into this.

hafen commented 5 years ago

This has been fixed in master. Thanks for the report!

jpn5089 commented 5 years ago

Thanks Ryan, appreciate you taking the time to fix this! Downloaded the updated package and can confirm it now works fine

Roctober92 commented 5 years ago

@hafen I can also confirm it now works fine, thank you!