'Error in range[1] : object of type 'environment' is not subsettable' persisting in PCA code R (factoextra package)

35 Views Asked by At

I am trying to use factoextra to plot a PCA biplot of some data.

My data frame looks like this:

head(pcadata)
  log_response_ratio water_depth_m maxbottomtemp BOsalinity
1         -0.4514584            10      17.09915     35.066
2         -0.1408754            10      17.09915     35.066
3         -0.6519815            10      17.09915     35.066
4         -0.5682017            15      17.09915     35.066
5         -0.2082759            15      17.09915     35.066
6         -0.3052670            15      17.09915     35.066

And this is the code I am using:


pcadata <- read.csv("pca.csv")
pcadata$water_depth_m <- as.numeric(pcadata$water_depth_m)
log_response_ratio <- complete.cases(pcadata$log_response_ratio)
pcadata <- pcadata[log_response_ratio, ]

#remove lnRR for the PCA

pcanolrr <- pcadata %>% dplyr::select(-log_response_ratio)

# create scaled df
pcadatafinal <- scale(pcanolrr, center = TRUE, scale = TRUE)


col_pca <- ncol(pcadatafinal)

pca_out <- PCA(pcadatafinal, scale.unit = FALSE, ncp = col_pca, graph = FALSE) # since already scaled, not need to do it again

# scree plot
fviz_screeplot(pca_out, addlabels = TRUE, ylim = c(0, 50))

#biplot

range(pcadata$log_response_ratio, na.rm=TRUE)
length(pcadata$log_response_ratio)

round(seq(-2, 2.25, length=1397), 4)

(var_breaks <- round(seq(-2, 2.25, length=1397), 4))

var_limits <- c(-2, 2.25)


fviz_pca_biplot(pca_out,
                geom.ind = "point",
                pointsize = 1.5,
                pointshape=21,
                col.ind = "#666666",
                stroke.ind = 1,
                fill.ind = pcadata$log_response_ratio,
                alpha.ind = 1,
                # palette=color_sequence[c(2,6)],
                label ="var",
                labelsize = 2,
                col.var = "#666666",
                # alpha.var=0.5,
                repel = TRUE,
                # alpha.var=0.5,
                # alpha.var="contrib",
                invisible="quali"
) +
  coord_fixed(ratio = 1) +
  labs(title="", x="Dim1", y="Dim2") +
  scale_x_continuous(breaks = scales::pretty_breaks(6), limits = xlim) +
  scale_y_continuous(breaks = scales::pretty_breaks(6), limits = ylim) +
  # scale_fill_fermenter(palette = "RdBu") +"
  guides(fill = guide_colorbar(title.position = "top", title.hjust = 0.5)) +
  # scale_fill_gradient2(name=axis_par_log_name, low="#2166AC", mid="white", high="#B2182B") +
  scale_fill_gradient2(low="#2166AC", mid="white", high="#B2182B", midpoint = median(pcadata$log_response_ratio)) +
  theme_bw() +
  theme(panel.grid.major = element_line(linewidth = 0.3, linetype = "dashed"),
        panel.grid.minor = element_blank(),
        axis.title = element_text(size = 10),
        axis.text = element_text(size = 10),
        axis.title.x = element_text(margin = margin(t=10)),
        axis.title.y = element_text(margin = margin(r=10)),
        legend.title = element_text(size=10),
        legend.text = element_text(size=10),
        # legend.background = element_rect(color = "#666666", linewidth = 0.2),
        legend.position = c(0.18, 0.1),
        legend.key.height = unit(2, units = "mm"),
        legend.key.width = unit(8, units = "mm"),
        # legend.position = "bottom",
        legend.direction = "horizontal",
        aspect.ratio = 1
  )

However, whatever I change to try and fix this error: 'Error in range[1] : object of type 'environment' is not subsettable' it persists every time

This is the error traceback

17.
oob(...)
16.
self$oob(x, limits)
15.
map(..., self = self)
14.
scales[[i]][[method]](data[[var]][scale_index[[i]]])
13.
FUN(X[[i]], ...)
12.
lapply(seq_along(scales), function(i) {
scales[[i]][[method]](data[[var]][scale_index[[i]]])
})
11.
FUN(X[[i]], ...)
10.
lapply(vars, function(var) {
pieces <- lapply(seq_along(scales), function(i) {
scales[[i]][[method]](data[[var]][scale_index[[i]]])
}) ...
9.
scale_apply(layer_data, x_vars, "map", SCALE_X, self$panel_scales_x)
8.
FUN(X[[i]], ...)
7.
lapply(data, function(layer_data) {
match_id <- match(layer_data$PANEL, layout$PANEL)
x_vars <- intersect(self$panel_scales_x[[1]]$aesthetics,
names(layer_data)) ...
6.
map_position(..., self = self)
5.
layout$map_position(data)
4.
ggplot_build.ggplot(x)
3.
ggplot_build(x)
2.
print.ggplot(x)
1.
(function (x, ...)
UseMethod("print"))(x)

Any help would be much appreciated!

0

There are 0 best solutions below