Skip to content

Size and Color inside marker not working properly in R #3049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
renanxcortes opened this issue Sep 25, 2018 · 3 comments
Closed

Size and Color inside marker not working properly in R #3049

renanxcortes opened this issue Sep 25, 2018 · 3 comments

Comments

@renanxcortes
Copy link

renanxcortes commented Sep 25, 2018

Hello, When I was running a code that was supposed to draw a scatterplot and color and resize a single dot of it, I realized that it wasn't working properly. The only way to fix this problem was to add an "arrange" function for the State Initial in the input dataset of plotly. Otherwise, this code was coloring and resizing the shape of Arkansas instead of the US.

It seems like the code was coloring and resizing it according to the alphabetical order of the states initial and not according to the 'ifelse' function inside the marker list.

Here's the code and the file "national_county.txt" is attached:

# Code to extractdata from FRED
library(alfred)
library(tidyverse)
library(lubridate)
library(plotly)
library(zoo)
library(data.table)
library(BBmisc)

calculate_acum_growth_S_months    <- function(x, s) { aux <- rollapply(x, width = s, FUN = sum); return(c(rep(NA, s - 1),(aux / lag(aux, s) - 1) * 100)) }
calculate_acelleration_S_months   <- function(x, s, k) { aux <- diff(calculate_acum_growth_S_months(x, s), k); return(c(NA, aux)) }


# List of US States initials
county_corresp <- tbl_df(fread("national_county.txt"))
names(county_corresp) <- c("STATE", "STATEFP", "COUNTYFP", "COUNTYNAME", "CLASSFP")
states_initials <- unique(county_corresp$STATE)

# United States first (all join will be after the first grab)
string_grab = c("UNRATENSA", paste0(states_initials, "URN"))

# Inspired in https://stackoverflow.com/questions/11254524/omit-rows-containing-specific-column-of-na
# Just modified a little bit in order to use the column name "dplyr's wise"
completeFun <- function(data, desiredCols) {
  completeVec <- complete.cases(data[, desiredCols])
  return(data[completeVec, ])
}


get_fred_series_paese <- function(series_id, series_name = NULL,
                                  observation_start = NULL, observation_end = NULL) {
  
  
  length_series_id <- nchar(series_id)
  
  if (is.character(series_id) == FALSE) {
    stop("series_id is always in characters")
  }
  
  if (is.null(series_name) == TRUE ) {
    series_name <- series_id
  }
  
  if (is.null(observation_start) == TRUE) {
    observation_start <- "1776-07-04"
  }
  
  if (is.null(observation_end) == TRUE) {
    observation_end <- "9999-12-31"
  }
  
  df_series <-
    try({
      fromJSON(
        paste0("https://api.stlouisfed.org/fred/series/observations?series_id=",
               series_id,
               "&observation_start=",
               observation_start,
               "&observation_end=",
               observation_end,
               "&output_type=2",
               "&api_key=98f9f5cad7212e246dc5955e9b744b24&file_type=json")
      )$observations %>%
        mutate_(date = ~as_date(date))
    }, silent = TRUE)
  
  print(class(df_series))
  
  if (!class(df_series) == "try-error") {
    colnames(df_series)[!(colnames(df_series) %in% "date")] <- series_name
    df_series[, 2] <- as.numeric(unlist(df_series[, 2]))
    df_series
    
  } else {
    df_series <- tibble(date = as.Date(NA))
  }
  
}

create_df <- function(i) {
x <- get_fred_series_paese(string_grab[i])
if (!is.error(x)) # Even if FRED doesn't the series, it won't stop
{x} else 
{tibble(date = as.Date(NA))}}

data = 1:length(string_grab) %>% 
       map(create_df) %>% 
       reduce(full_join, by = "date") %>%
       completeFun("date")


data_clean <- data %>%
  rename(USURN = UNRATENSA) %>%
  gather(Raw_Type, UR, -date) %>%
  separate(Raw_Type, into = c("State", "Seasonal_Type"), sep = 2) %>%
  mutate(year = year(date),
         month = month(date)) %>%
  group_by(State, Seasonal_Type) %>%
  mutate(Var = calculate_acum_growth_S_months(UR, 12),
         Ace = calculate_acelleration_S_months(Var, s = 12, k = 1))


base_aux = data_clean %>%
              filter(Seasonal_Type == "URN",
                     year == 2016,
                     month == 10) #%>%
              #arrange(State)


# Spatial Unemployment
base_aux %>%
  plot_ly(x = ~Ace, 
          y = ~Var, 
          type = 'scatter', 
          mode = 'markers', 
          marker = list(size = ifelse(base_aux$State == "US", 20, 10),
                        #sizeref = .10, 
                        color = ifelse(base_aux$State == "US", "red","#004B82")#,
                        #symbol = 1:length(base_aux$State)
          ),  
          hoverinfo = "text",
          text = paste("", base_aux$State, "<br>",
                       "Growth: ", round(base_aux$Var, 3), "<br>",
                       "Acelleration: ", round(base_aux$Ace, 3)),
          showlegend = TRUE)

national_county.txt

@renanxcortes renanxcortes changed the title Size and Color inside marker not working properly Size and Color inside marker not working properly in R Sep 25, 2018
@etpinard
Copy link
Contributor

Can someone try to replicate the problem in JS for us?

@renanxcortes
Copy link
Author

renanxcortes commented Sep 26, 2018

Can someone try to replicate the problem in JS for us?

Perhaps, my question is more related with the plotly's R package, right? Therefore, I opened the same issue here: plotly/plotly.R#1351

@etpinard
Copy link
Contributor

Closing, until someone can replicate the problem in JS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants