I've run a PCA with a moderately-sized data set, but I only want to visualize a certain amount of points from that analysis because they are from repeat observations and I want to see how close the paired observations are to each other on the plot. I've set it up so that the first 18 individuals are the ones I want to plot, but I can't seem to only plot just the first 18 points without only doing an analysis of only the first 18 instead of the whole data set (43 individuals).
# My data file
TrialsMR<-read.csv("NER_Trials_Matrix_Retrials.csv", row.names = 1)
# I ran the PCA of all of my values (without the categorical variable in col 8)
R.pca <- PCA(TrialsMR[,-8], graph = FALSE)
# When I try to plot only the first 18 individuals with this method, I get an error
fviz_pca_ind(R.pca[1:18,],
labelsize = 4,
pointsize = 1,
col.ind = TrialsMR$Bands,
palette = c("red", "blue", "black", "cyan", "magenta", "yellow", "gray", "green3", "pink" ))
# This is the error
Error in R.pca[1:18, ] : incorrect number of dimensions
The 18 individuals are each paired up, so only using 9 colours shouldn't cause an error (I hope).
Could anyone help me plot just the first 18 points from a PCA of my whole data set?
My data frame looks similar to this in structure
TrialsMR
Trees Bushes Shrubs Bands
JOHN1 1 4 18 BLUE
JOHN2 2 6 25 BLUE
CARL1 1 3 12 GREEN
CARL2 2 4 15 GREEN
GREG1 1 1 15 RED
GREG2 3 11 26 RED
MIKE1 1 7 19 PINK
MIKE2 1 1 25 PINK
where each band corresponds to a specific individual that has been tested twice.
You are using the wrong argument to specify individuals. Use select.ind to choose the individuals required, for eg.:
data(iris) # test data
If you want to rename your rows according to a specific grouping criteria for readily identifiable in a plot. For eg. let setosa lies in series starting with 1, something like in 100-199, similarly versicolor in 200-299 and virginica in 300-399. Do it before the PCA.
new_series <- c(101:150, 201:250, 301:350) # there are 50 of each
rownames(iris) <- new_series
R.pca <- prcomp(iris[,1:4],scale. = T) # pca
library(factoextra)
fviz_pca_ind(X= R.pca, labelsize = 4, pointsize = 1,
select.ind= list(name = new_series[1:120]), # 120 out of 150 selected
col.ind = iris$Species ,
palette = c("blue", "red", "green" ))
Always refer to R documentation first before using a new function.
R documentation: fviz_pca {factoextra}
X
an object of class PCA [FactoMineR]; prcomp and princomp [stats]; dudi and pca [ade4]; expOutput/epPCA [ExPosition].
select.ind, select.var
a selection of individuals/variables to be drawn. Allowed values are NULL or a list containing the arguments name, cos2 or contrib
For your particular dummy data, this should do:
R.pca <- prcomp(TrailsMR[,1:3], scale. = TRUE)
fviz_pca_ind(X= R.pca,
select.ind= list(name = row.names(TrialsMR)[1:4]), # 4 out of 8
pointsize = 1, labelsize = 4,
col.ind = TrialsMR$Bands,
palette = c("blue", "green" )) + ylim(-1,1)
Dummy Data:
TrialsMR <- read.table( text = "Trees Bushes Shrubs Bands
JOHN1 1 4 18 BLUE
JOHN2 2 6 25 BLUE
CARL1 1 3 12 GREEN
CARL2 2 4 15 GREEN
GREG1 1 1 15 RED
GREG2 3 11 26 RED
MIKE1 1 7 19 PINK
MIKE2 1 1 25 PINK", header = TRUE)
Related
I have a dataframe which includes 2 columns below
|Systolic blood pressure |Urea Nitrogen|
|------------------------|-------------|
|155.86667|50.000000|
|140.00000| 20.33333|
|135.33333| 33.857143|
|126.40000|15.285714|
|...|...|
I want to create 2 more columns called Sys_points and BUN_points based on the bucket criteria like the image attached, which will store the values (not in equally spaced) of column Points in the image. I have tried findInterval and cut but can't find functions that allow me to assign values not in sequence order to buckets.
#findInterval
BUN_int <- seq(0,150,by=10)
data3$BUN <- findInterval(data3$`Urea Nitrogen`,BUN_int)
#cut
cut(data3$`Urea Nitrogen`,breaks = BUN_int, right=FALSE, dig.lab=c(0,2,4,6,8,9,11,13,15,17,19,21,23,25,27,28))
Is there any function that can help me with this?
Here’s how to do it using cut(). Note the use of -Inf and Inf to include <x and >=x bins.
bun_data$Sys_points <- cut(
bun_data$`Systolic blood pressure`,
breaks = c(5:20 * 10, Inf),
labels = c(28,26,24,23,21,19,17,15,13,11,9,8,6,4,2,0),
right = FALSE
)
bun_data$BUN_points <- cut(
bun_data$`Urea Nitrogen`,
breaks = c(-Inf, 1:15 * 10, Inf),
labels = c(0,2,4,6,8,9,11,13,15,17,19,21,23,25,27,28),
right = FALSE
)
Result:
Systolic blood pressure Urea Nitrogen Sys_points BUN_points
1 155.8667 50.00000 9 9
2 140.0000 20.33333 11 4
3 135.3333 33.85714 13 6
4 126.4000 15.28571 15 2
I am working with R. Suppose I have the following data frame:
my_data <- data.frame(
"col" = c("red","red","red","red","red","blue","blue","blue","blue","blue","green", "green", "green", "green","green"),
"x_cor" = c(1,2,5,6,7,4,9,1,0,1,4,4,7,8,2),
"y_cor" = c(2,3,4,5,9,5,8,1,3,9,11,5,7,9,1),
"frame_number" = c(1,2,3,4,5, 1,2,3,4,5, 1,2,3,4,5)
)
my_data$col = as.factor(my_data$col)
head(my_data)
col x_cor y_cor frame_number
1 red 1 2 1
2 red 2 3 2
3 red 5 4 3
4 red 6 5 4
5 red 7 9 5
6 blue 4 5 1
In R, is it possible to create a (two-dimensional) graph that will "animate" each colored point to a new position based on the "frame number"?
For example:
I started following the instructions from this website here: https://www.datanovia.com/en/blog/gganimate-how-to-create-plots-with-beautiful-animation-in-r/
First, I made a static graph:
library(ggplot2)
library(gganimate)
p <- ggplot(
my_data,
aes(x = x_cor, y=y_cor, colour = col)
Then, I tried to animate it:
p + transition_time(frame_number) +
labs(title = "frame_number: {frame_number}")
Unfortunately, this produced an empty plot and the following warnings:
There were 50 or more warnings (use warnings() to see the first 50)
1: Cannot get dimensions of plot table. Plot region might not be fixed
2: values must be length 1,
but FUN(X[[1]]) result is length 15
Can someone please show me how to fix this problem?
Thanks
I'm switching from Mathematica to R but I'm finding some difficulties with visualizations.
I'm trying to do a heatmap as follows:
short
penetration scc pi0
1 0 0 0.002545268
2 5 0 -0.408621176
3 10 0 -0.929432006
4 15 0 -1.121309680
5 20 0 -1.587298317
6 25 0 -2.957853131
7 30 0 -5.123329738
8 0 50 1.199748327
9 5 50 0.788581883
10 10 50 0.267771053
11 15 50 0.075893379
12 20 50 -0.390095258
13 25 50 -1.760650073
14 30 50 -3.926126679
15 0 100 2.396951386
16 5 100 1.985784941
17 10 100 1.464974112
18 15 100 1.273096438
19 20 100 0.807107801
20 25 100 -0.563447014
21 30 100 -2.728923621
mycol <- c("navy", "blue", "cyan", "lightcyan", "yellow", "red", "red4")
ggplot(data = short, aes(x = penetration, y = scc)) +
geom_tile(aes(fill = pi0)) +
scale_fill_gradientn(colours = mycol)
And I get this:
But I need something like this:
That is, I would like that the color is continuous (degraded) over the surface of the plot instead of discrete for each square. I've seen in other SO questions that some people interpolate de data but I think there should be an easier way to do it inside the ggplot call (in Mathematica is done by default).
Besides, I would like to lock the color scale such that the 0 is always white (separating therefore between warm colors for positive values and cold for negative ones) and the color distribution is always the same across plots independently of the range of the data (since I will use the same plot structure for several datasets)
You can use geom_raster with interpolate=TRUE:
ggplot(short , aes(x = penetration, y = scc)) +
geom_raster(aes(fill = pi0), interpolate=TRUE) +
scale_fill_gradient2(low="navy", mid="white", high="red",
midpoint=0, limits=range(short$pi0)) +
theme_classic()
To get the same color mapping to values of pi0 across all of your plots, set the limits argument of scale_fill_gradient2 to be the same in each plot. For example, if you have three data frames called short, short2, and short3, you can do this:
# Get range of `pi0` across all data frames
pi0.rng = range(lapply(list(short, short2, short3), function(s) s$pi0))
Then set limits=pi0.rng in scale_fill_gradient2 in all of your plots.
I would adjust your scale_fill_gradient2:
scale_fill_gradient2('pi0', low = "blue", mid = "white", high = "red", midpoint = 0)
to make plot colours directly comparable add consistent limits to each plot:
scale_fill_gradient2('pi0', low = "blue", mid = "white", high = "red", midpoint = 0, limits=c('your lower limit','your upper limit'))
I wanted to plot a survival curve using the following data. I called the data file as A.txt and the object A
A <- read.table(text = "
Time Status Group
8 1 A
8 1 A
8 1 A
9 1 A
9 1 A
9 1 A
15 0 A
15 0 A
7 1 B
7 1 B
8 1 B
9 1 B
10 1 B
10 1 B
15 0 B
15 0 B", header = TRUE)
I tried to plot a survival curve using this code:
title(main="Trial for Survival Curve")
fit <- survfit(Surv(Time, Status) ~ Group, data = A)
par(col.lab="red")
legend(10, .9, c("A", "B"), pch=c(2,3),col=2:3)
plot(fit, lty=2:3, col=2:3,lwd=5:5, xlab='Time(Days)',
ylab='% Survival',mark.time=TRUE,mark=2:3)
I would like to put marks (triangle for A and "+" for B) every time when survival % decreases for instance at Day 7 and Day 8. I want this labeling throughout the graph, but it adds the labels only at the end of the experiment.
First, I'd recommend rearranging the plotting calls:
par(col.lab="red")
plot(fit, lty=2:3, col=2:3,lwd=5:5, xlab='Time(Days)',
ylab='% Survival',mark.time=TRUE,mark=2:3)
title(main="Trial for Survival Curve")
legend(10, .9, c("A", "B"), pch=c(2,3),col=2:3)
You can add points to the survival plot with the points function. However, it looks like there's a small bug, which you can get around fairly easily:
firsty <- 1 ## Gets around bug
points(fit[1], col = 2, pch = 2) # Plots first group in fit
points(fit[2], col = 3, pch = 3) # Plots second group in fit
The points are plotted at the bottom of the "cliff" in the survival plot.
I have the following problem:
I need to create a mosaic plot but want to display the number of cases for each mosaic, as total numbers per country differ. The plot is based on the following data:
1 - not agree 2 3 4 5 - fully agree
DE 6 2 0 0 1
ES 5 3 1 1 0
FR 6 3 1 2 0
SE 4 3 0 0 0
I used the following code:
> mosaicplot(Q1, col=c("red", "orange", "yellow", "green", "green4"),
+ las = 1,
+ main = "There is no need to do anything about it.",
+ ylab = "",
+ xlab = "Country")
Giving me this graph:
Now I would like to divide the first red bar into six bars of the same colour, as there were 6 votes in Germany a.s.o. Any ideas on how to accomplish that?
I applied the procedure explained here:
https://learnr.wordpress.com/2009/03/29/ggplot2_marimekko_mosaic_chart/
Only I had to use two data frames, one for the percentages and one for the absolute values.
Both data frames went through the same calculations. Whilst dfm1 created the chart, dfm21 was used for the labels:
p2 <- p1 + geom_text(aes(x = xtext, y = ytext,
label = ifelse(dfm21$value == "0", paste(" "), paste(dfm21$value))), size = 3.5)