13146

subset of prcomp object in R

Question:

I'm basically computing the PCA for a set of variables and everything works fine. Lets say I'm using the iris data as an example, but my data is different. The iris data should be sufficient to explain my question:

data(iris) log.ir <- log(iris[, 1:4]) log.ir[mapply(is.infinite, log.ir)] <- 0 ir.groups<- iris[, 5] ir.pca <- prcomp(log.ir, center = TRUE, scale. = TRUE) library(ggbiplot) g <- ggbiplot(ir.pca, obs.scale = 1,var.scale = 1,groups = ir.groups, var.axes=F) g <- g + scale_color_discrete(name = '') g <- g + theme(legend.direction = 'horizontal', legend.position = 'top') + theme(legend.text=element_text(size=15), legend.key.size = unit(2.5, "lines")) + theme(text = element_text(size=20)) ggsave("pca2.pdf", g, width=15, height=15)

When I get the plot, some groups are plotted too close together so I want to make a new plot for this subset of groups (without computing a new PCA for the subset).

Is there a way to make a subset of the ir.pca object to select only specific groups to plot?

Answer1:

I think you can define a new graphical window with ggplot2::coord_equal, eg:

g + coord_equal(xlim=c(0, 3))

would exclude setosa from the graph, not from the PCA.

<hr />

Taking your comment into account, you can do it programmatically:

# first we filter the scores filtered_scores <- ir.pca$x[which(iris$Species != "setosa"), ] # then on PC1 and PC2 g + coord_equal(xlim=range(filtered_scores[, 1]), ylim=range(filtered_scores[, 2]))

Is this what you wanted?

Recommend

  • PCA and Hotelling's T^2 for confidence intervall in R
  • Renaming the file downloaded with Python Requests
  • extract unique combinations of subset of parameters from tidy data
  • What does the column and rows for images in TensorBoard mean?
  • location data format for adehabitat package
  • Reorder stacks in horizontal stacked barplot (R)
  • karplus equation in gnuplot
  • How to have rotated Ellipse shape in Java?
  • Multiply 2D NumPy arrays element-wise and sum
  • Scikit-learn (sklearn) PCA throws Type Error on sparse matrix
  • Add a label to y-axis to show the value of y for a horizontal line in matplotlib
  • Plotting a 3d matrix in slices - MATLAB
  • R rbind - unexpected symbol error when merging rows from two data frames
  • jQueryUI Draggable: Constrain draggability to a single axis?
  • bty = “n” in ggplot2
  • Multiple scatterplots using Core Plot and Swift
  • plotting spatial points over a raster layer in r
  • How to execute 2 Observables in parallel, ignoring their results and execute next Observable
  • Einsum optimize fails for basic operation
  • Plotting densities in R
  • WPF version of .ScaleControl?
  • blade.php method outputting it's result to the form
  • Update Google Maps traffic layer without page reloading
  • Plotting line graph with factors in R
  • D3 get axis values on zoom event
  • Installing iPhone App to iPhone
  • Combining SpatialPolygonsDataFrame of two neighbour countries
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Fill an image in a square container while keeping aspect ratio
  • Why winpcap requires both .lib and .dll to run?
  • How to delete a row from a dynamic generate table using jquery?
  • using HTMLImports.whenReady not working in chrome
  • JTable with a ScrollPane misbehaving
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • reshape alternating columns in less time and using less memory
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?