Using the bi_class(), I am trying to create mapping classes for a bivariate map. These data will be stored in a new variable named bi_class, which will be added to the given data object.
The code below returns an error of
Error in cut.default(.data[[var]], breaks = classInt::classIntervals(.data[[var]],:'breaks' are not unique
IDD_nhmap <- IDD_nhmap %>%
group_by(ProjectID) %>%
bi_class(x = race_black, y = svi, style = "quantile", dim = 3) %>%
bi_class(x = race_hisp, y = svi, style = "quantile", dim = 3)
Related
The code below returns what I need. However, when I tried to include groupby() function, I got an error:
Error in bi_class(., IDD_nhmap, x = Zip_Black, y = svi, style = "quantile", :
A logical scalar must be supplied for 'keep_factors'. Please provide either 'TRUE' or 'FALSE'.
# The code before including groupby function
IDD_nhmap <- bi_class(IDD_nhmap, x = Zip_Black, y = svi, style = "quantile", dim = 3)
# The code after including groupby function
IDD_nhmap <- IDD_nhmap %>%
group_by(ProjectID) %>%
bi_class(IDD_nhmap, x = Zip_Black, y = svi, style = "quantile", dim = 3)
TL;DR: remove the IDD_nhmap from your call to bi_class.
In your second use, you are passing the frame to bi_class twice which is incorrect.
In a %>%-pipe, the data as it appears in the pipe is passed as the first argument to the next function; this can be specified (or repeated) by using the . placeholder. Your code therefore is really something like this:
IDD_nhmap <- IDD_nhmap %>%
group_by(., ProjectID) %>%
bi_class(., IDD_nhmap, x = Zip_Black, y = svi, style = "quantile", dim = 3)
For group_by, this makes sense: it expects the first argument to be a frame (equivalent to .data = .) and all remaining unnamed arguments are taken as the symbols for grouping variables.
For bi_class, the . is placed in the first argument (.data = . again), which means your first unnamed argument is interpreted as the next not-yet-used argument. The arguments listed in ?bi_class are:
bi_class(.data, x, y, style, dim = 3, keep_factors = FALSE, dig_lab = 3)
Since you explicitly name x, y, style, and dim, the first unused argument is keep_factors, so your call is effectively:
IDD_nhmap <- IDD_nhmap %>%
group_by(., ProjectID) %>%
bi_class(., keep_factors = IDD_nhmap, x = Zip_Black, y = svi, style = "quantile", dim = 3)
which is obviously not correct. Your first step should be
IDD_nhmap <- IDD_nhmap %>%
group_by(ProjectID) %>%
bi_class(x = Zip_Black, y = svi, style = "quantile", dim = 3)
However, you are still not likely to get what you are hoping for. While I don't know the bi_class function personally, it does not look for the grouping attributes that dplyr::group_by adds to the data, so the results from this call will be the same as your first (ungrouped) call. A hasty attempt at this might be:
IDD_nhmap <- IDD_nhmap %>%
group_by(., ProjectID) %>%
do(bi_class(., IDD_nhmap, x = Zip_Black, y = svi, style = "quantile", dim = 3))
though do is superseded. Untested, perhaps you can try
IDD_nhmap <- IDD_nhmap %>%
group_by(., ProjectID) %>%
summarize(
bi = bi_class(cur_data(), IDD_nhmap, x = Zip_Black, y = svi, style = "quantile", dim = 3)
)
to get a nested result (bi will be a list-column), over to you how you intend to utilize this.
I am trying to produce a ribbon on my highcharter chart (roughly following is there an equivalent to geom_ribbon in highcharter?).
However, the following example to produce a highcharter graph in R produces an error:
library(quantmod)
library(dplyr)
library(highcharter)
getSymbols("VOD")
bb_data = BBands(Cl(VOD), n=20)
highchart(type = "stock") %>%
hc_add_series(bb_data, type = "arearange", hcaes(low = dn, high=up))
The error is:
Error: 'hcaes(low = dn, high = up)' argument is not named in hc_add_series
I have think this is because it is a time series object (xts).
It works if I cast it to a data.frame, but then I lose the date.
highchart(type = "stock") %>%
hc_add_series(as.data.frame(bb_data), type = "arearange", hcaes(low = dn, high=up))
I cannot combine it to with the moving average or price data as I would wish, as the ribbon is then missing from the subsequent plot:
highchart(type = "stock") %>%
hc_add_series(Cl(VOD), name = "VOD") %>%
hc_add_series(bb_data$mavg, name = "20d MA") %>%
hc_add_series(as.data.frame(bb_data), type = "arearange", hcaes(low = dn, high=up))
ok, so I had to first extract the date from the time series object and bind it with the time series object to form a data frame or data table and then plot using that.
bb_data2 = cbind(date = as.Date(index(bb_data)), data.table(bb_data))
highchart(type = "stock") %>%
hc_add_series(bb_data2, type = "arearange", hcaes(x=date, low = dn, high=up)) %>%
hc_add_series(Cl(VOD), name = "VOD") %>%
hc_add_series(bb_data$mavg, name = "20d MA")
I need to get the dataframe from a function in rShiny server. But that function returns a Plot and the return value cannot be changed as the plots are used in the future use.
have not pasted the whole code as its like 200 lines each for the function and also for the rshiny server.
Hist_Read_data4 <- full_join(Hist_Read_data1,Hist_Read_data_opst, by = c("timestamp"))%>%
arrange(timestamp)%>%
subset(timestamp >= as.POSIXct(start_timestamp, origin = "1970-01-01") & timestamp <= as.POSIXct(end_timestamp, origin = "1970-01-01"))%>%
mutate(value.y = na.locf(value.y, na.rm = FALSE))%>%
mutate(value.y = fct_explicit_na(value.y, na_level = "None"))%>%
mutate(value.x = na.locf(value.x, na.rm=FALSE))%>%
mutate(new_value = abs(value.x - lag(value.x)))%>%
mutate(new_value = replace_na(new_value, 0))%>%
mutate(new_value = cumsum(new_value))
plot <- ggplot() +
geom_path(data = Hist_Read_data4, mapping = aes(x = timestamp, y=value.x, color = value.y), na.rm = TRUE, linejoin = 'round' , size=1.5, group = 1)
//Hist_Read_data4 is the dataframe which i need to return//
//plot is the return value of the function//
output$HoverText <- renderText({
coordinfo <- input$PlotHover
nearpts <- nearPoints(Hist_Read_data4, coordinfo, xvar= "timestamp", yvar = "value.y", threshold = 20)
})
need Hist_Read_data4 in inside nearpoints. But it cannot be accessed as its inside a function named chooseDevice() in a separate script file named data_funcs.R
I do not want to change the return value of the chooseDevice function from plot to returning this dataframe as it will complicate the whole code and 2 months work will be wasted.
Considering a data.frame like this:
df <- data.frame(t = rep(seq(from=as.POSIXct('00:15:00',format='%H:%M:%S'),
to=as.POSIXct('24:00:00',format='%H:%M:%S'),by='15 min'),times=2),
y = c(rnorm(96,10,10),rnorm(96,40,5)),
group = factor(rep(1:2,each=96)),
type = factor(rep(1:3,each=64)))
Using ggvis, I want to generate a point-line plot in which the line is grouped by group. The size of points with type==3 should be 100 while the size of points withtype==1 and type==2 are all 50. The colour of the points should be green, blue and red corresponding to type1,type2 and type3. Here is my ggvis code:
df <- data.frame(df,id=1:nrow(df))
all_values <- function(x) {
if(is.null(x)) return(NULL)
row <- df[df$id == x$id, ]
paste0(names(row), ": ", format(row), collapse = "<br />")
}
ggvis(data=df,x=~t,y=~y,stroke=~group) %>%
layer_points(fill=~type,size=~type, key:=~id, fillOpacity := 0.5,
fillOpacity.hover := 0.8,size.hover := 500) %>%
scale_nominal("size",domain = c(1,2,3), range = c(50,50,100)) %>%
scale_nominal("fill",domain = c(1,2,3), range = c('green','blue','red')) %>%
layer_lines() %>%
add_tooltip(all_values,'click') %>%
add_legend(scales=c("fill","size"), properties = legend_props(legend = list(y = 150))) %>%
set_options(duration = 0) %>%
add_axis(type="x",format="%H:%M")
I get the error of Error: length(x) not less than or equal to 2.
Why this happened and how can I fix it?
It turns out that scale_nominal("size",domain = c(1,2,3), range = c(50,50,100)) should be replaced by scale_nominal("size",domain = c(1,2,3), range = c('50','50','100')).
The culprit for the error is more than 2 values defined for range. The definition for range suggests : For numeric values, the range can take the form of a two-element array with minimum and maximum values.
For ordinal data, the range may by an array of desired output values, which are mapped to elements in the specified domain. In this case, value should be defined in character.
This should resolve your error.
I would like to insert a blank column in between "Delta = delta" and "Card = vars" in the dataframe below. I would also like to sort the output by the column "Model_Avg_Error" in the dataframe as well.
df = data.frame(Card = vars, Model_Avg_Error = model_error, Forecast = forecasts, Delta = delta, ,Card = vars, Model_Avg_Error = model_error,
Forecast = forecasts, Delta = delta)
# save
write.csv(df, file = file.path(proj_path, "output.csv"), row.names = F)
This was the error received from above:
Error in data.frame(Card = vars, Model_Avg_Error = model_error, Forecast = forecasts, :
argument is missing, with no default
You can add your blank column, re-order, and sort using the code below:
df$blankVar <- NA #blank column
df[c("Card", "blankVar", "Model_Avg_Error", "Forecast", "Delta")] #re-ordering columns by name
df[order(df$Model_Avg_Error),] #sorting by Model_Avg_Error
Here's a general way to add a new, blank column
library(tibble)
# Adds after the second column
iris %>% add_column(new_col = NA, .after = 2)
# Adds after a specific column (in this case, after Sepal.Width)
iris %>% add_column(new_col = NA, .after = "Sepal.Width")