How to make a profile plot (principal component analysis) in R? - r

I'm currently running principal component analysis. For the interpretation I want to create a profile (pattern) plot to visualize the correlation between each principal component and the original variables. Is anyone familiar with a package or code to create this in R? I'm using the prcomp() function in R.
See examples:
https://canadianaudiologist.ca/predicting-speech-perception-from-the-audiogram-and-vice-versa/
https://blogs.sas.com/content/iml/2019/11/04/interpret-graphs-principal-components.html
This is similar data to my db:
db <- structure(list(T025 = c(20, 60, 20, 10, 85, 5, 15, 10, 10, 25,
15, 5, 15, 30, 15, 15, 10, 25, 45, 25, 55, 20, 65, 20, 10, 10,
15, 15, 30, 35, 10, 50, 20, 15, 30, 15, 20, 35, 30, 20, 10, 20,
30, 15, 40, 15, 10, 10, 20, 25, -5, 10, 40, 0, 15, 5, 15, 30,
15, 80, 15, 35, 10, 50, 25, 10, 15, 20, 20, 20, 25, 20, 30, 10,
20, 50, 25, 25, 55, 30, 20, 30, 15, 10, 15, 15, 35, 20, 30, 15,
40, 20, 25, 15, 20, 35, 15, 25, 20, 40, 0, 20, 10, 10, 15, 10,
20, 10, 35, 35, 25, 30, 20, 25, 15, 30, 35, 25, 30, 5, 20, 30,
15, 25, 10), T05 = c(0, 25, 0, 5, 25, 5, 0, 0, 5, 5, 5, -5, 5,
15, 15, 5, 0, 15, 25, 15, 50, 20, 45, 5, 5, 5, 0, 10, 10, 10,
5, 20, 15, 10, 20, 10, -5, 10, 30, -5, 0, 10, 35, 5, 40, 0, 0,
-5, 15, 25, 0, 5, 35, -5, 5, 0, 5, 5, 10, 70, 0, 20, 5, 30, 10,
10, 5, 5, 25, 10, 20, 5, 25, 5, 10, 35, 15, 10, 45, 15, 15, 25,
10, 5, 10, 5, 20, 15, 15, 5, 10, 10, 20, 5, 15, 25, 5, 20, 10,
35, -10, 5, 0, -5, 0, 5, 15, 5, 15, 35, 20, 25, 10, 15, 15, 25,
45, 0, 25, 0, 5, 25, 0, 20, 5), T1 = c(25, 20, 25, 20, 50, 10,
15, 20, 25, 25, 25, 25, 15, 45, 25, 25, 20, 35, 40, 35, 65, 45,
45, 30, 25, 20, 5, 20, 30, 25, 20, 35, 25, 25, 35, 15, 15, 25,
45, 20, 25, 35, 40, 25, 60, 15, 15, 15, 25, 45, 20, 20, 60, 15,
20, 25, 45, 45, 25, 75, 10, 45, 15, 50, 20, 25, 20, 15, 40, 30,
50, 20, 40, 20, 35, 50, 35, 15, 50, 30, 20, 45, 25, 25, 20, 45,
30, 35, 30, 30, 15, 15, 30, 25, 25, 25, 15, 40, 25, 55, 20, 30,
10, 15, 50, 15, 40, 20, 20, 55, 35, 45, 20, 50, 35, 20, 65, 10,
35, 15, 30, 55, 25, 15, 25), T2 = c(20, 20, 15, 25, 70, 10, 15,
45, 50, 30, 20, 25, 10, 40, 20, 40, 30, 40, 25, 30, 45, 25, 50,
20, 20, 20, 10, 10, 45, 10, 5, 40, 20, 15, 50, 25, 15, 20, 25,
30, 20, 30, 35, 15, 65, 20, 25, 10, 10, 60, 25, 20, 70, 5, 15,
15, 15, 25, 15, 60, 25, 55, 5, 50, 30, 35, 5, 10, 30, 10, 55,
25, 40, 35, 40, 45, 25, 20, 35, 40, 5, 40, 10, 25, 10, 40, 30,
20, 25, 25, 10, 25, 30, 45, 20, 25, 10, 55, 40, 60, 5, 10, 10,
5, 20, 0, 40, 20, 35, 80, 25, 40, 15, 55, 25, 15, 65, 5, 25,
5, 35, 45, 10, 5, 10), T4 = c(10, 25, 35, 35, 70, 20, 15, 70,
55, 30, 50, 35, 40, 40, 35, 45, 60, 50, 15, 25, 70, 10, 60, 40,
30, 15, 15, 15, 50, 5, 20, 70, 5, 35, 65, 40, 20, 65, 50, 30,
45, 55, 65, 35, 45, 35, 40, 20, 5, 65, 20, 25, 75, 10, 25, 25,
10, 25, 20, 55, 20, 65, 5, 60, 70, 45, 15, 25, 35, 5, 70, 55,
65, 40, 35, 55, 35, 45, 45, 45, 20, 40, 25, 50, 15, 55, 55, 40,
30, 60, 10, 60, 40, 35, 30, 65, 5, 75, 55, 80, 15, 30, 55, 15,
50, 25, 45, 30, 45, 90, 20, 45, 20, 40, 35, 20, 70, 20, 30, 45,
50, 55, 45, 5, 45), T8 = c(5, 55, 55, 40, 75, 40, 5, 70, 25,
10, 50, 55, 5, 35, 10, 30, 40, 55, 20, 20, 65, -5, 55, 50, -10,
45, 5, 50, 65, 20, 0, 75, 15, 30, 50, 50, 30, 70, 45, 25, 35,
40, 85, 30, 60, 50, 55, 15, 10, 75, 60, 20, 90, 0, 20, 55, -10,
20, 10, 45, 20, 65, 0, 70, 85, 0, -5, 30, 35, 5, 80, 45, 60,
25, 35, 55, 30, 45, 65, 45, -5, 35, 35, 40, 50, 55, 50, 70, 45,
40, 0, 55, 45, 30, 0, 56, 0, 45, 50, 70, 15, 20, 45, -10, 45,
55, 45, 20, 50, 85, 5, 50, 10, 20, 25, 0, 70, 0, 25, 5, 45, 35,
40, -5, 25)), row.names = c("1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "20", "21", "22", "23", "24", "25", "26",
"177", "191", "200", "205", "208", "212", "231", "236", "240",
"246", "250", "259", "263", "264", "275", "276", "282", "293",
"303", "304", "307", "309", "315", "316", "320", "322", "324",
"327", "333", "338", "343", "356", "365", "377", "379", "393",
"395", "399", "405", "411", "426", "428", "439", "448", "451",
"459", "490", "495", "498", "513", "515", "521", "524", "528",
"532", "550", "552", "559", "566", "570", "577", "583", "587",
"595", "624", "638", "641", "645", "647", "650", "660", "668",
"677", "683", "688", "691", "702", "704", "710", "719", "730",
"732", "748", "752", "758", "766", "772", "780", "782", "790",
"810", "828", "830", "836", "853", "862", "880", "889", "896"
), class = "data.frame")
db.pca <- prcomp(db, center= TRUE, scale.=TRUE)
summary(db.pca)
str(db.pca)
ggbiplot(db.pca)
screeplot(db.pca, type="line")

Here is a way with package FactoMineR to get the correlations. The plot is a base R plot.
library(FactoMineR)
res.pca <- PCA(iris[-5], graph = FALSE)
cos2 <- res.pca$var$cos2
old_par <- par(xpd = TRUE)
matplot(
cos2,
type = "l",
xlab = "variable",
ylab = "correlation",
main = "Component Pattern Profiles",
xaxt = "n"
)
axis(1, at = 1:nrow(cos2), labels = rownames(cos2))
legend(
x = "bottom",
inset = c(0, -0.2),
legend = colnames(cos2),
col = 1:ncol(cos2),
lty = 1:ncol(cos2),
bty = "n",
horiz = TRUE
)
par(old_par)

using your data I did this:
comp = prcomp(db, center=T, scale.=T)
b =matrix(ncol = 3)[-1,]
for(i in 1:ncol(comp$x)){
for(j in colnames(db)){
b = rbind(b, c(i,j,cor.test(comp$x[,i], db[,j])$estimate))
}
}
b= as.data.frame(b)
b$cor= as.numeric(b$cor)
ggplot(b,aes(x=V2,y=cor, group = V1, col= V1))+
geom_line()+
theme_classic()
And I obtained this :
did it help?

Related

Problems with overlaying the spatial map with a movement network

In order to overlay a movement network on a spatial map, I'm trying to define my own custom layout for plotting geocoordinates as node positions, using the packages igraph, ggraph and ggplot2, among others. The below code used to work but not anymore. Perhaps because i updated packages but i cannot remember. I cannot figure out how to solve the error message that i'm receiving below. Any help with solving is greatly appreciated.
The relevant code to create a custom layout are below.
igraph graph 'g':
g<-structure(list(32, FALSE, c(1, 4, 5, 6, 7, 8, 9, 10, 11, 13,
14, 15, 18, 19, 20, 22, 25, 27, 28, 31, 4, 6, 7, 9, 18, 22, 6,
7, 9, 13, 14, 18, 27, 9, 18, 7, 8, 9, 18, 22, 27, 9, 18, 10,
17, 18, 22, 27, 18, 18, 22, 18, 22, 27, 27), c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
4, 4, 4, 4, 4, 4, 4, 5, 5, 6, 7, 7, 7, 7, 7, 8, 8, 9, 9, 9, 9,
9, 10, 13, 13, 14, 18, 18, 22), c(0, 1, 20, 2, 3, 21, 26, 4,
22, 27, 35, 5, 36, 6, 23, 28, 33, 37, 41, 7, 43, 8, 9, 29, 10,
30, 11, 44, 12, 24, 31, 34, 38, 42, 45, 48, 49, 51, 13, 14, 15,
25, 39, 46, 50, 52, 16, 17, 32, 40, 47, 53, 54, 18, 19), c(0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54), c(0, 0, 1, 1, 1, 3, 4, 7, 11, 13, 19, 21, 22,
22, 24, 26, 27, 27, 28, 38, 39, 40, 40, 46, 46, 46, 47, 47, 53,
54, 54, 54, 55), c(0, 20, 26, 26, 26, 33, 35, 36, 41, 43, 48,
49, 49, 49, 51, 52, 52, 52, 52, 54, 54, 54, 54, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55), list(c(1, 0, 1), structure(list(), .Names = character(0)),
list(name = c("1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
"30", "31", "32"), long = c(-79.260866474359, -80.0214733333333,
-76.7089527272727, -74.780105, -80.2763261538462, -80.46082,
-80.030754125, -80.121487, -82.1419016578947, -81.41021461,
-85.6918532152941, -81.2031545454546, -81.187526, -80.11719,
-80.5063443067816, -82.5245573333333, -78.8618875, -80.883413,
-79.7296606, -76.9488, -77.8201766666667, -76.5490625, -81.5434341458974,
-93.59755, -88.035675, -83.2922209874074, -84.5110066666667,
-79.1122080625, -80.173172, -77.3383016666667, -78.2188216666667,
-77.7654788888889), lat = c(25.7205797179487, 26.85296, 25.53793,
38.33654, 27.1842307692308, 24.948536, 26.99901625, 27.171273,
28.2081715526316, 24.6188354115741, 29.4212288229412, 31.0068890909091,
29.643964, 26.6099285714286, 28.4826687301149, 27.100245,
26.63643, 31.3834996666667, 32.6213106, 34.2073, 24.8982166666667,
24.3104225, 24.6719581623077, 27.90902, 30.0528425, 27.764016172963,
29.706515, 26.97782228125, 25.5103912857143, 34.55239, 26.6567083333333,
26.9487844444444)), list(weight = c(426, 58, 3, 253, 493,
9, 392, 7, 4, 42, 5, 2, 40, 1, 2, 74, 2, 106, 17, 1, 2, 114,
62, 9, 1, 1, 1, 5, 3, 12, 1, 9, 1, 2, 1, 1, 1, 15, 6, 2,
17, 4, 1, 1, 1, 21, 4, 4, 1, 1, 1, 1, 2, 1, 2))), <environment>), class = "igraph")
Node positions:
nodes_pos<-structure(list(x = c(-79.260866474359, -80.0214733333333, -76.7089527272727,
-74.780105, -80.2763261538462, -80.46082, -80.030754125, -80.121487,
-82.1419016578947, -81.41021461, -85.6918532152941, -81.2031545454546,
-81.187526, -80.11719, -80.5063443067816, -82.5245573333333,
-78.8618875, -80.883413, -79.7296606, -76.9488, -77.8201766666667,
-76.5490625, -81.5434341458974, -93.59755, -88.035675, -83.2922209874074,
-84.5110066666667, -79.1122080625, -80.173172, -77.3383016666667,
-78.2188216666667, -77.7654788888889), y = c(25.7205797179487,
26.85296, 25.53793, 38.33654, 27.1842307692308, 24.948536, 26.99901625,
27.171273, 28.2081715526316, 24.6188354115741, 29.4212288229412,
31.0068890909091, 29.643964, 26.6099285714286, 28.4826687301149,
27.100245, 26.63643, 31.3834996666667, 32.6213106, 34.2073, 24.8982166666667,
24.3104225, 24.6719581623077, 27.90902, 30.0528425, 27.764016172963,
29.706515, 26.97782228125, 25.5103912857143, 34.55239, 26.6567083333333,
26.9487844444444)), row.names = 1:32, class = "data.frame")
manual layout:
lay <- create_layout(graph = g, layout = "manual",
node.positions = nodes_pos)
Error message
Error in layout_fun(graph, circular = circular, ...) :
unused argument (node.positions = nodes_pos)
Package versions:
ggraph 2.0.2
igraph 1.2.4.2
R 3.6.2
try:
g_new <- upgrade_graph(g)
lay <- create_layout(g_new, 'manual',
x = long, y = lat)
ggraph(lay) +
geom_node_point(color = "red") +
geom_polygon(data = map_data("state"), aes(x=long, y=lat, group=group),
color="black", fill="lightblue" )

Filling a list with for loop or Lapply

This is my list:
mylist=dput(mylist)
list(list(
c(30, 50, 35, 25, 45),
c(40, 35, 35, 50, 45),
c(40, 20, 40, 50, 25),
),
list(
c(50, 50, 25, 40, 45, 40, 35, 40, 45, 20),
c(40, 35, 40, 40, 45, 30, 20, 50, 35, 25),
c(20, 30, 50, 35, 45, 40, 25, 50, 35, 50),
),
list(
c(45, 50, 25, 25, 30, 25, 35, 35, 35, 30, 50, 50, 30, 30, 20),
c(40, 20, 35, 35, 50, 20, 25, 30, 35, 20, 40, 20, 45, 30, 20),
c(50, 20, 25, 35, 35, 30, 50, 25, 40, 35, 45, 45, 35, 45, 25),
),
list(
c(50, 50, 50, 40, 20, 25, 50, 40, 50, 50, 45, 40, 30, 50, 35, 45, 50, 30,
35, 45),
c(45, 20, 25, 20, 25, 30, 20, 30, 45, 25, 50, 30, 30, 25, 50, 45, 20, 45, 45, 50),
c(20, 40, 50, 25, 40, 45, 25, 30, 20, 20, 35, 45, 20, 40, 50, 45, 40, 40, 45, 35),
),
list(
c(35, 25, 45, 20, 25, 30, 30, 35, 30, 40, 30, 20, 20, 30, 45, 40, 35, 35, 35, 35, 25, 45, 35, 20, 50),
c(50, 35, 30, 30, 35, 45, 45, 50, 25, 25, 40, 25, 50, 45, 25, 30, 30, 25, 45, 45, 30, 20, 50, 30, 30),
c(35, 40, 50, 25, 40, 45, 30, 25, 50, 25, 35, 50, 50, 50, 25, 50, 20, 50, 40, 25, 25, 35, 20, 20, 50),
)
)
)
mylist=dput(mylist)
list
(
list( c(30, 50, 35, 25, 45),
c(40, 35, 35, 50, 45),
c(40, 20, 40, 50, 25),
),
list(
c(50, 50, 25, 40, 45, 40, 35, 40, 45, 20),
c(40, 35, 40, 40, 45, 30, 20, 50, 35, 25),
c(20, 30, 50, 35, 45, 40, 25, 50, 35, 50),
), list(
c(45, 50, 25, 25, 30, 25, 35, 35, 35, 30, 50, 50, 30, 30, 20),
c(40, 20, 35, 35, 50, 20, 25, 30, 35, 20, 40, 20, 45, 30, 20),
c(50, 20, 25, 35, 35, 30, 50, 25, 40, 35, 45, 45, 35, 45, 25),
),
list(
c(50, 50,50, 40, 20, 25, 50, 40, 50, 50, 45, 40, 30, 50, 35, 45, 50, 30, 35, 45),
c(45, 20, 25, 20, 25, 30, 20, 30, 45, 25, 50, 30, 30, 25, 50, 45, 20, 45, 45, 50),
c(20, 40, 50, 25, 40, 45, 25, 30, 20, 20, 35, 45, 20, 40, 50, 45, 40, 40, 45, 35),
),
list(
c(35, 25, 45, 20, 25, 30, 30, 35, 30, 40, 30, 20, 20, 30, 45, 40, 35, 35, 35, 35, 25, 45, 35, 20, 50),
c(50, 35, 30, 30, 35, 45, 45, 50, 25, 25, 40, 25, 50, 45, 25, 30, 30, 25, 45, 45, 30, 20, 50, 30, 30),
c(35, 40, 50, 25, 40, 45, 30, 25, 50, 25, 35, 50, 50, 50, 25, 50, 20, 50, 40, 25, 25, 35, 20, 20, 50),
)))
I am facing two problems:
First: I canĀ“t run this For Loop below:
resultlist<-vector(mode = "list", 5)
for (i in 1:6) {
for(k in 1:5) {
resultlist[[k]][[i]]<-mean(mylist[[k]][[i]])
}
}
It sends the message: Error in mylist[[k]][[i]] : subscript out of bounds
The second problem is actually a sugestion: My original for is much bigger (for example my i goes until 4828), to run a for with this 2 indexes (k and i) there are others functions that would let my code be more simple/efficient or in this situation the best to do is to keep with the for loop?
You are getting that error because the fourth element of mylist has only 5 sublists.
You could also do:
resultlist <- lapply(mylist, function(x) lapply(x, function(y) rep(mean(y), length(y))))
to get what you want.
In your case , your mylist[[4]] only have 5 element, so the error occur because you call the sixth element of it which does not exist.
If all the list elements are equal length, you can use expand.grid to find the all combination, and use sapply to execute the loop which is little bit faster than for
Try this :
list_len=length(mylist)
sub_list_len=6
combination<-expand.grid(1:sub_list_len,1:list_len)
temp_output<-apply(combination,1,function(x) mean( mylist[[x[2]]][[x[1]]] ))
resultlist<-split(temp_output,rep(1:list_len,each=sub_list_len)) %>% lapply(.,function(x) split(x,1:length(x)))
resultlist

r subset `x` and `labels` must be same type

I am trying to subset my dataset as follows
df[df$Age > 19,]
I am seeing an error , Error: x and labels must be same type
I am not sure I understand this, any suggestions are much appreciated.
=================
dput(df$Age)
c(20, 11, 10, 15, 6, 23, 45, 30, 18, 11, 15, 20, 7, 18, 19, 30,
40, 16, 14, 33, 12, 22, 12, 5, NA, 18, 30, 26, 25, 27, 12, 27,
13, 15, 32, 19, NA, 18, 13, 30, 10, 16, 47, 24, 64, 21, 9, 30,
12, 33, 16, 20, 14, 10, 19, 18, 20, 18, 10, 15, 55, 18, 50, 14,
35, 18, 21, 17, 14, 9, 25, 17, 10, 16, 12, 30, 38, 10, 27, 20,
27, 16, 30, 11, 5, 20, 30, 12, 24, 11, 7, 26, 48, 25, 20, 18,
27, 18, 28, 15, 17, 46, 30, 20, 20, 14, 35, 31, 10, 26, 13, NA,
15, 3, 30, 33, 15, 43, 19, 40, 8, 16, 8, 3, 37, 40, 58, 18, 12,
19, 14, 24, 34, 30, 23, 28, 47, 29, 21, 35, 23, 47, 11, 30, 16,
25, 30, 30, 8, 18, 20, 12, 8, 18, 30, 6, 54, 60, 18, 27, 42,
6, 42, 13, 21, 15, 17, 10, 33, 15, 16, 36, 16, 52, 4, 30, 28,
30, 14, 13, 14, NA, 15, 20, 20, 24, 27, 23, 10, 13, 22, 30, 45,
10, 23, 14, 27, 19, 12, 25, 10, 10, 14, 16, 16, 19, 18, 12, 65,
18, 35, 20, 31, NA, 21, 40, 8, 13, 25, 8, 13, 15, 19, 25, 10,
9, 24, 8, 25, 30, 38, 35, 20, 12, 15, 25, 27, 39, 8, 10, NA,
12, 50, 16, 14, 22, 12, 20, 44, 13, 8, 43, 48, 13, 21, 20, 42,
11, 20, 35, 53, 22, 17, 5, NA, 14, 10, 21, 33, 21, 69, 24, 15,
12, 8, 28, 11, 32, 25, 26, 21, 36, 12, 24, 20, 23, 14, 30, 50,
26, NA, 30, 22, 44, 22, 14, 30, 28, 10, 16, 32, 35, 40, 16, 40,
33, 23, 25, 10, 17, 10, 14, 22, 14, 25, 20, 39, 24, 52, 16, 34,
26, 23, 11, 12, 70, 59, 12, 38, 22, 13, 40, 57, 30, 7, 21, 20,
30, 12, 13, 5, 19, 35, 56, 17, 40, 48, 19, 8, 30, 21, 5, 40,
16, 22, 20, 17, 16, 30, 18, 13, 17, NA, 40, 9, 24, 26, 20, 22,
17, 44, 45, 18, 26, 50, 10, 21, 15, NA, 20, 12, 16, 54, 15, 16,
33, 22, 26, 60, 35, 11, 30, 16, 48, 16, 16, 16, 10, 14, 15, 23,
17, 18, NA, 49, 12, 7, 18, 24, 17, 14, 30, 13, 6, 51, 36, 16,
10, 43, 34, 15, 12, 15, 15, 17, 40, 58, 15, 33, 16, 48, 25, 15,
16, 5, NA, 40, 34, 10, 30, 30, 30, 15, 15, 12, 5, 10, 20, 18,
20, 16, 20, 26, 12, 14, 14, 20, 12, 30, 30, 29, 22, 19, 26, 11,
23, 40, 30, 16, 50, 20, 25, 29, 40, 44, 20, 40, 8, 16, 15, 38,
11, 27, 63, 16, NA, 47, 65, 21, 29, 30, 16, 21, 25, 16, 23, 5,
17, 22, 12, 14, 27, NA, 16, 9, 33, 11, 15, 34, 41, 30, 33, 15,
25, 40, 25, 12, 12, 17, 14)

Add vertical lines to time-series plot

I have the code below which plots two time-series. I'd like to add a vertical line every say 10 units on the x-axis to basically divide the plot up into like 5 squares. Any tips are very much appreciated.
Code:
## Plot Forecast & Actual
ts.plot(ts(CompareDf$stuff1),ts(CompareDf$stuff2),col=1:2,xlab="Hour",ylab="Minu tes",main='testVar')
legend("topleft", legend = c("Actual","Forecast"), col = 1:2, lty = 1)
Data:
dput(CompareDf)
structure(list(stuff1 = c(6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55), stuff2 = c(8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57)), .Names = c("stuff1",
"stuff2"), row.names = c(NA, -50L), class = "data.frame")
After plotting timeseries data, use abline to draw vertical lines.
abline(v = seq(10, 50, 10))

Independent alpha for each plot within facet_grid based on density

I am constructing a facet_grid using stat_hexbin however I would like the alpha value to be independent for each of the facet plots.
I am currently using the following code:
ggplot (data, aes (x, y , fill = z)) +
stat_binhex(bins=20, aes(alpha = ..count..)) +
facet_grid(. ~ z) +
guides(alpha = F) +
coord_equal() +
theme_bw()
which produces the following plot:
However, the alpha value, which is defined by ..count.. doesn't work when applied outside of the aes within stat_binhex.
I would like to show that there is some clustering in the 90 grouping on the right, around the (100,0) region, but the hexes are very pale, since there is such heavy clustering around (0,0) in the 10 grouping (leftmost plot) which skews the alpha.
Main question: How can I make the alpha independent for each facet, but still connected to count/density to better show the clustering in '70' and '90' groups?
Many thanks!
Data:
# rounded x and y, from 2 days of 365
structure(list(x = c(-24, 41, 43, 14, 9, 30, 8, -14, -45, 42,
65, 39, 43, 49, 39, 61, -53, -16, 29, 27, 9, 6, -61, 20, 5, -30,
-10, 75, 94, 28, 70, 44, -11, 26, 29, 33, 26, -35, 20, 40, 7,
4, 14, 4, -41, -7, -21, 95, 20, 50, 63, 31, 47, 19, 20, 19, 23,
-25, 29, -8, -73, 13, -82, 4, -29, 3, 9, 3, 35, 45, 64, -14,
-4, 34, 13, 12, 20, 13, 15, -17, 12, 19, -55, -49, 95, -19, 45,
94, 23, 29, 22, -91, -39, -35, -3, 63, 2, 5, 30, 62, 1, 4, -61,
-6, -2, 5, -26, -23, 5, 6, 8, 45, 104, -7, 8, 44, -43, -8, 9,
12, 29, 30, 69, 90, 12, -28, -10, -9, 49, 60, 32, 43, -11, 12,
28, 91, 11, 13, 43, 61, 11, 12, 28, 31, 47, 12, 13, 30, 46, 66,
98, 11, 12, 29, 31, 44, 64, -11, 14, 48, 62, 96, 10, 11, 12,
29, 67, 30, 93, -10, -9, 44, 101, -28, 34, 46, 10, 27, 30, 61,
8, 24, -7, -2, 52, 65, 5, -43, 41, 45, 91, -24, -23, 37, 73,
97, -61, 63, 57, 52, -37, -35, 19, 24, 110, -91, -5, -17, 95,
13, 85, -52, -50, 78, 30, 37, -8, -27, 19, -78, -75, 52, 42,
-11, -37, 27, 62, 78, -16, -56, 41), y = c(-100, -95, -95, -92,
-88, -86, -84, -82, -81, -78, -73, -72, -71, -70, -69, -68, -67,
-67, -64, -63, -62, -59, -58, -57, -56, -54, -54, -54, -54, -52,
-52, -49, -48, -48, -48, -47, -46, -45, -45, -45, -44, -42, -41,
-40, -39, -39, -38, -38, -37, -36, -36, -35, -35, -34, -34, -33,
-33, -32, -32, -31, -30, -30, -29, -29, -28, -27, -27, -26, -26,
-26, -26, -25, -25, -25, -24, -23, -23, -22, -22, -21, -21, -21,
-20, -20, -19, -18, -18, -18, -17, -17, -16, -14, -14, -14, -13,
-13, -12, -12, -12, -12, -11, -11, -10, -10, -10, -10, -9, -9,
-9, -9, -9, -9, -9, -8, -8, -8, -7, -7, -7, -7, -6, -6, -6, -6,
-5, -4, -4, -4, -4, -4, -3, -3, -2, -2, -2, -2, -1, -1, -1, -1,
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
3, 4, 4, 4, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 8, 9, 9, 9, 9, 10,
10, 11, 11, 11, 11, 12, 13, 14, 14, 14, 15, 15, 15, 16, 16, 18,
19, 20, 21, 23, 23, 24, 24, 24, 26, 27, 28, 28, 29, 30, 32, 32,
32, 36, 36, 41, 42, 44, 48, 48, 50, 51, 57, 60, 62, 76, 76, 85,
89, 93), z = c(90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90,
90, 90, 90, 90, 90, 70, 70, 70, 70, 70, 90, 70, 70, 70, 70, 90,
90, 70, 90, 70, 50, 70, 70, 70, 70, 70, 50, 70, 50, 50, 50, 50,
70, 50, 50, 90, 50, 70, 70, 50, 70, 50, 50, 50, 50, 50, 50, 30,
90, 30, 90, 30, 50, 30, 30, 30, 50, 50, 70, 30, 30, 50, 30, 30,
30, 30, 30, 30, 30, 30, 70, 70, 90, 30, 50, 90, 30, 30, 30, 90,
50, 50, 10, 70, 10, 10, 30, 70, 10, 10, 70, 10, 10, 10, 30, 30,
10, 10, 10, 50, 90, 10, 10, 50, 50, 10, 10, 10, 30, 30, 70, 90,
10, 30, 10, 10, 50, 70, 30, 50, 10, 10, 30, 90, 10, 10, 50, 70,
10, 10, 30, 30, 50, 10, 10, 30, 50, 70, 90, 10, 10, 30, 30, 50,
70, 10, 10, 50, 70, 90, 10, 10, 10, 30, 70, 30, 90, 10, 10, 50,
90, 30, 30, 50, 10, 30, 30, 70, 10, 30, 10, 10, 50, 70, 10, 50,
50, 50, 90, 30, 30, 50, 70, 90, 70, 70, 70, 70, 50, 50, 30, 30,
90, 90, 30, 30, 90, 30, 90, 70, 70, 90, 50, 50, 50, 50, 50, 90,
90, 70, 70, 70, 70, 70, 90, 90, 90, 90, 90)), .Names = c("x",
"y", "z"), row.names = c(NA, -231L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x0000000000330788>)

Resources