Check linearity in logistic regression - r

In order to check linearity in logistic regression ->
Is independent1 and independent2variable linear related to the log-odds of depdendent?
I would like optimize this (working) calculations:
This is the code:
# Check Linearity ---------------------------------------------------------
# quartiles of independent1
quantile(df$independent1, probs=c(0, 0.25, 0.5, 0.75, 1))
table(df$dependent[df$independent1<52])
table(df$dependent[df$independent1>=52 & df$independent1 < 60])
table(df$dependent[df$independent1>=60 & df$independent1 < 73])
table(df$dependent[df$independent1>=73 & df$independent1 < 91])
p1 <- mean(df$dependent[df$independent1<52])
p2 <- mean(df$dependent[df$independent1>=52 & df$independent1 < 60])
p3 <- mean(df$dependent[df$independent1>=60 & df$independent1 < 73])
p4 <- mean(df$dependent[df$independent1>=73 & df$independent1 < 91])
probs <- c(p1, p2, p3, p4)
# calculate the log-odds
logits <- log(probs/(1-probs))
# quartiles of independent1
q <- quantile(df$independent1, probs=seq(0,1,0.25))
# calculate median independent1 for each of the 4 groups
meds <- c( median(df$independent1[ df$independent1<q[2]]),
median(df$independent1[ df$independent1>=q[2] & df$independent1<q[3]]),
median(df$independent1[ df$independent1>=q[3] & df$independent1<q[4]]),
median(df$independent1[ df$independent1>=q[4]])
)
plot(meds, logits, main="xxx",
xlab = "independent1",
ylab = "log-odds(dependent|independent1)", las=1)
For one variable this might be ok. But I have more independent variables. So how could I optimize this code (checking and plotting) for each independent variable (in this example independent1 and independent2)
My dataframe:
df <- structure(list(dependent = c(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), independent1 = c(84,
49, 54, 75, 49, 70, 75, 42, 60, 72, 80, 73, 51, 61, 59, 78, 45,
38, 78, 65, 91, 60, 39, 31, 42, 72, 41, 77, 73, 74, 39, 86, 71,
55, 43, 75, 80, 75, 67, 74, 46, 70, 57, 66, 57, 72, 46, 52, 53,
76, 57, 86, 67, 71, 57, 50, 76, 61, 41, 57, 62, 41, 64, 82, 53,
75, 59, 38, 54, 56, 68, 63, 73, 26, 75, 76, 81, 46, 77, 53, 59,
66, 51, 72, 80, 70, 39, 57, 62, 85, 84, 57, 73, 55, 70, 78, 66,
69, 60, 51, 72, 68, 60, 62, 64, 44, 50, 59, 45, 81, 54, 68, 75,
66, 54, 45, 52, 87, 44, 77, 49, 84, 68, 76, 82, 44, 58, 55, 69,
33, 48, 62, 60, 76, 56, 73, 55, 58, 53, 53, 60, 52, 60, 41, 39,
36, 38, 59, 54, 64), independent2 = c(23, 25, 34, 25, 31, 25,
32, 19, 25, 28, 22, 18, 30, 26, 25, 25, 25, 19, 24, 27, 23, 28,
39, 27, 30, 28, 22, 28, 25, 23, 18, 27, 27, 19, 25, 27, 26, 26,
21, 26, 23, 28, 37, 32, 24, 32, 26, 23, 24, 27, 28, 25, 24, 22,
34, 23, 35, 20, 29, 29, 21, 29, 25, 26, 23, 33, 25, 26, 29, 27,
26, 28, 19, 22, 29, 22, 26, 35, 32, 29, 26, 23, 31, 30, 27, 28,
23, 27, 34, 22, 24, 28, 21, 25, 18, 32, 21, 24, 31, 31, 24, 30,
27, 23, 16, 26, 26, 19, 38, 21, 32, 34, 28, 19, 30, 24, 26, 24,
40, 26, 15, 26, 28, 22, 25, 26, 31, 24, 26, 42, 26, 30, 28, 21,
21, 19, 22, 20, 26, 31, 22, 25, 21, 20, 27, 27, 26, 29, 22, 24
)), row.names = c(NA, -150L), class = c("tbl_df", "tbl", "data.frame"
))

I'll demonstrate a somewhat different and decidedly more efficient method of splitting a variable that is to be used in a logistic regression model:
df$q41 <- with(df, cut(independent1, quantile(independent1), include = TRUE))
# creates 4 level factor into roughly equally sized groups
table(df$q41)
#--------------------
#[26,52] (52,60] (60,73] (73,91]
# 39 37 39 35
#Examine for "eyeball" trends in the log-odds of dependent
fit1.q41 <- glm(dependent~q41+0, data=df, fam="binomial")
fit1.q41
#---------------------------
Call: glm(formula = dependent ~ q41 + 0, family = "binomial", data = df)
Coefficients:
q41[26,52] q41(52,60] q41(60,73] q41(73,91]
-3.638 -2.862 -2.918 -2.048
Degrees of Freedom: 150 Total (i.e. Null); 146 Residual
Null Deviance: 207.9
Residual Deviance: 65.52 AIC: 73.52
I chose to remove the intercept term because its presence prevented viewing the coefficient of the lowest group on the same scale as the upper 3. The coefficients are just the logits for the grouping I created. Compare:
> logits
[1] -3.555348 -2.740840 -2.970414 -2.169054
> coef(fit1.q41)
q41[26,52] q41(52,60] q41(60,73] q41(73,91]
-3.637586 -2.862201 -2.917771 -2.047693
I then tried to automate the process but ran into a bit of a problem because of the small numbers of events in one of the quartile groups, The ridiculously low coefficient for the lowest quartile in independent2 is from that lack of any events or "1"'s in that catergory. (An estimate log-odds of -19.566069 does rather point to a proportion of 0.)
lapply( df[-1], function(x){cat(str(x)); IVq <- cut(x, quantile(x), include = TRUE); logits<-coef( summary(glm(df$dependent~IVq+0, fam="binomial"))); logits})
num [1:150] 84 49 54 75 49 70 75 42 60 72 ...
num [1:150] 23 25 34 25 31 25 32 19 25 28 ...
$independent1
Estimate Std. Error z value Pr(>|z|)
IVq[26,52] -3.637586 1.0130639 -3.590678 3.298191e-04
IVq(52,60] -2.862201 0.7270292 -3.936845 8.256004e-05
IVq(60,73] -2.917771 0.7259663 -4.019155 5.840732e-05
IVq(73,91] -2.047693 0.5312796 -3.854266 1.160776e-04
$independent2
Estimate Std. Error z value Pr(>|z|)
IVq[15,23] -19.566069 1639.9716035 -0.01193074 9.904809e-01
IVq(23,26] -3.091042 0.7229988 -4.27530783 1.908734e-05
IVq(26,28] -2.397895 0.7385489 -3.24676555 1.167245e-03
IVq(28,42] -1.856298 0.4808846 -3.86017349 1.133066e-04
> lapply( df[-1], function(x){ IVq <- cut(x, quantile(x), include = TRUE); table(IVq, df$dependent) })
$independent1
IVq 0 1
[26,52] 38 1
(52,60] 35 2
(60,73] 37 2
(73,91] 31 4
$independent2
IVq 0 1
[15,23] 43 0
(23,26] 44 2
(26,28] 22 2
(28,42] 32 5
At any rate I think I've demonstrated a more R-ish approach to calculating logits within quartiles. It also sets you up for a model comparison approach to examining departures from linearity as well as demonstrating possible pitfalls. If you had more events you might have considered looking at the change in deviance from the null model with the addition of a quartile factor on top of a simple linear model ... or even more powerfully using poly to create your comparison model.
In the past when working with datasets with adequate numbers of events I have chosen to split on the basis of quantiles calculated from an event==1 subset rather than letting the splits be based on the whole dataset.

Related

fill delaunay triangles with colors of vertex points in R

here is a reprex
data<- structure(list(lanmark_id = 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, 55, 56, 57, 58,
59, 60, 61, 62, 63, 64, 65, 66, 67), V1 = c(0.00291280916742007,
0.00738863171211713, 0.0226678081211574, 0.0475105228945172,
0.0932285720818941, 0.167467706279089, 0.257162845610094, 0.365202733889021,
0.49347857580521, 0.623654594804239, 0.738846221030799, 0.838001377618909,
0.911583795022151, 0.954620025430512, 0.976736039833402, 0.99275439380643,
1.00100526672829, 0.0751484964183746, 0.136267471453466, 0.223219796351563,
0.312829176190895, 0.396253287447153, 0.589077347394549, 0.682150866526948,
0.771279538477539, 0.856242644022999, 0.915433541338973, 0.493665602840245,
0.491283285973581, 0.488913167946858, 0.486968906096063, 0.384707082576335,
0.43516446651127, 0.48730704698643, 0.541730425616146, 0.590794609520034,
0.176234316360877, 0.230353437655898, 0.295908510434122, 0.350673723300921,
0.2927721757992, 0.228392965512228, 0.634474821310078, 0.692554938010577,
0.757884656518485, 0.809961553290539, 0.760324208523177, 0.696892501347341,
0.299062528225204, 0.371899560139738, 0.440183530232855, 0.488448817156316,
0.542120710507391, 0.613931454931259, 0.683122622479693, 0.614367295821043,
0.544516611213321, 0.487065702940653, 0.43466839036949, 0.367662837035504,
0.329392110306872, 0.439192556373207, 0.488617118648197, 0.543288506065858,
0.652131615571443, 0.541622182786469, 0.486664920417254, 0.437126878794749
), V2 = c(0.201088019764115, 0.335422141956174, 0.468591127485112,
0.597955245417373, 0.719502795031081, 0.826191980419368, 0.912263437847338,
0.978932088608654, 0.996572250349122, 0.975164350943783, 0.906204543800476,
0.817791059656974, 0.711167374856116, 0.587462637963028, 0.457981280500493,
0.327526817895531, 0.19652402489511, 0.0832018969548692, 0.0247526745448235,
0.00543973063471442, 0.0169853862992864, 0.0463565705952832,
0.0442986445765913, 0.0151651597693172, 0.00747493463745755,
0.0263496825405166, 0.0805712600069456, 0.160307477500307, 0.24640401358039,
0.332244740019727, 0.420995916418539, 0.486383354389177, 0.505514985155285,
0.521022030162301, 0.5059272511442, 0.48818970795347, 0.184054088286897,
0.153658218058329, 0.153359749238857, 0.186997311695192, 0.20294291755153,
0.204166125257439, 0.186997311695192, 0.153386090373069, 0.155932705636629,
0.184603717976376, 0.203900583330345, 0.202836636618411, 0.670663080116174,
0.635972857244521, 0.619932598923225, 0.632625553953685, 0.620132318139554,
0.637530241507316, 0.668109937001625, 0.718821664744205, 0.73956412947459,
0.744898219300658, 0.74046882628352, 0.720755964662638, 0.672731384920681,
0.666152981987244, 0.670464844757437, 0.664772611108765, 0.671145517468628,
0.673968618595099, 0.67986363963374, 0.675352028351748), coef2 = c(0,
0, 0, 0, 0, 0, 0, 0, 0.565178003460693, 0, 0, 0, 0, 0, 0, 0,
0, 0.0433232019717308, 0.0433232019717308, 0.442833876807268,
0.574211955093656, 0.574211955093656, 0.574211955093656, 0.574211955093656,
0.442833876807268, 0.0433232019717308, 0.0433232019717308, 0.0612451242746323,
0.0612451242746323, 0, 0, 0, 0, 0, 0, 0, 0.343056259557492, 0.701076795777046,
0.674029769391816, 0, 0.538117834886036, 0.990039002564078, 0.451921167678043,
0.701076795777046, 0.701076795777046, 0.316009233172263, 0.990039002564078,
0.990039002564078, 0.878350036859346, 0.343364662128988, 0.282119537854356,
0.282119537854356, 0.282119537854356, 0.343364662128988, 0.384793696241895,
0.608382647917744, 0.608382647917744, 1, 0.608382647917744, 0.608382647917744,
0.384793696241895, 0.501936678206125, 0.501936678206125, 0, 0.878350036859346,
0, 0.501936678206125, 0.501936678206125)), row.names = c(NA,
-68L), class = c("tbl_df", "tbl", "data.frame"))
I used this data to create a deulanay plot in R
library(tidyverse)
library(ggforce)
data%>%
mutate(coef2 = coef2/max(coef2))%>%
ggplot(aes(V1, V2))+
geom_delaunay_tile(aes(colour = coef2, fill = coef2), alpha = .5)+
geom_delaunay_segment2(aes(colour = coef2, fill = coef2))+
geom_point(aes(colour = coef2))+
ylim(1,0)+
scale_color_viridis_c(option = "magma")+
scale_fill_viridis_c(option = "magma")+
theme_minimal()
which gives this
I want to fill all triangles with a blend of colors that match the color of each point, just as the lines are colored.
as you can see I have tried using fill = coef2 within de geom_delaunay but this doesn't really achieve what I want.
is there a way to do this in R.
Many thanks!

How to consecutively subset an array and count the number of iterations?

I need to repeatedly apply a function on the resultant arrays until all data in the array is reduced to a single set, and count the number of iterations.
Data
Array ar
structure(c(0, 11, 17, 15, 22, 67, 73, 68, 31, 31, 28, 33, 34,
32, 11, 0, 9, 12, 21, 67, 73, 67, 35, 30, 34, 67, 60, 36, 17,
9, 0, 6, 19, 70, 74, 68, 36, 36, 36, 64, 66, 39, 15, 12, 6, 0,
13, 64, 69, 66, 34, 37, 39, 77, 65, 45, 22, 21, 19, 13, 0, 59,
60, 66, 38, 39, 39, 40, 43, 43, 67, 67, 70, 64, 59, 0, 10, 18,
77, 75, 78, 93, 93, 85, 73, 73, 74, 69, 60, 10, 0, 15, 76, 74,
80, 103, 101, 95, 68, 67, 68, 66, 66, 18, 15, 0, 59, 65, 73,
90, 87, 82, 31, 35, 36, 34, 38, 77, 76, 59, 0, 8, 19, 24, 28,
32, 31, 30, 36, 37, 39, 75, 74, 65, 8, 0, 12, 20, 22, 23, 28,
34, 36, 39, 39, 78, 80, 73, 19, 12, 0, 6, 14, 18, 33, 67, 64,
77, 40, 93, 103, 90, 24, 20, 6, 0, 2, 8, 34, 60, 66, 65, 43,
93, 101, 87, 28, 22, 14, 2, 0, 6, 32, 36, 39, 45, 43, 85, 95,
82, 32, 23, 18, 8, 6, 0), .Dim = c(14L, 14L))
From
a<-colSums(ar<25)
b<-which.max(a)
c<-ar[ar[,b] > 25,, drop = FALSE]
we get
structure(c(0, 11, 17, 15, 22, 67, 73, 68, 11, 0, 9, 12, 21,
67, 73, 67, 17, 9, 0, 6, 19, 70, 74, 68, 15, 12, 6, 0, 13, 64,
69, 66, 22, 21, 19, 13, 0, 59, 60, 66, 67, 67, 70, 64, 59, 0,
10, 18, 73, 73, 74, 69, 60, 10, 0, 15, 68, 67, 68, 66, 66, 18,
15, 0, 31, 35, 36, 34, 38, 77, 76, 59, 31, 30, 36, 37, 39, 75,
74, 65, 28, 34, 36, 39, 39, 78, 80, 73, 33, 67, 64, 77, 40, 93,
103, 90, 34, 60, 66, 65, 43, 93, 101, 87, 32, 36, 39, 45, 43,
85, 95, 82), .Dim = c(8L, 14L))
then from
a<-colSums(c<25)
b<-which.max(a)
d<-c[c[,b]>25,,drop=FALSE]
we get
structure(c(67, 73, 68, 67, 73, 67, 70, 74, 68, 64, 69, 66, 59,
60, 66, 0, 10, 18, 10, 0, 15, 18, 15, 0, 77, 76, 59, 75, 74,
65, 78, 80, 73, 93, 103, 90, 93, 101, 87, 85, 95, 82), .Dim = c(3L,
14L))
applying once more
a<-colSums(d<25)
b<-which.max(a)
e<-d[d[,b]>25,,drop=FALSE]
results in a array with no values
structure(numeric(0), .Dim = c(0L, 14L))
Then, the operation can be performed a number of times, and I need to know how many times; in this case it was 3 times.
Also, I need to reduce the number of the lines of the code probably with a loop function, as the action is repetitive.
You can use recursion as shown below:
fun<- function(x, i=1){
a<-which.max(colSums(x<25))
b<-x[x[,a] > 25,, drop = FALSE]
if(length(b)) Recall(b, i+1) else i
}
fun(ar)
[1] 3

Discrepancy between gggmisc and broom packages in LM estimates

I'm trying to extract slope values from a number of linear regression models. I plotting acetone emission against water content on different days.
I have these graphs and models
I have tried to extract the slope values using this code:
Library(broom)
Library(tidyverse)
lm_table <- df %>%
nest_by(days) %>%
summarise(mdl = list(lm(water_content ~ acetone, data)), .groups = "drop") %>%
mutate(adjrsquared = map_dbl(mdl, ~summary(.)$adj.r.squared ),
mdl = map(mdl, broom::tidy)) %>%
unnest(mdl)%>%
filter(term=="acetone")
and also this code:
lm_table2 <- df %>%
nest_by(days) %>%
mutate(model = list(lm(water_content ~ acetone, data)),
coefficients2 = list(tidy(model)))
coefficients2 = lm_table2 %>%
unnest(coefficients2)
Both codes however give different slope values than what I get from the graphs. Any ideas as to why that is?
Here's the data
df <- structure(list(i.x45.03 = c(22, 17, 11, 1782, 1767, 250, 3568,
79, 219, 855, 12009, 395, 1552, 705, 2282, 84, 3396, 252, 2058,
1480, 5, 745, 2573, 1005, 946, 3320, 5406, 2192, 20, 1207, 9519,
66, 463, 250, 1095, 16556, 88, 2695, 275, 16, 1577, 29, 3221,
25, 6295, 2, 63, 123, 8, 1, 37, 5308, 4546, 994, 4567, 421, 0,
1938, 19480, 1027, 3474, 1982, 2819, 69, 27733, 2152, 15429,
996, 8, 3435, 8748, 17062, 269, 26188, 35823, 2572, 67, 761,
13493, 1, 1, 1, 16, 9, 29, 89, 20, 11, 21644, 3, 37, 13, 0, 0,
0, 0, 3, 30, 19, 0, 0, 242, 7246, 1, 20081, 77, 0, 0, 0, 5878,
0, 0, 22, 2, 4, 1, 93, 12, 2, 73, 0, 19, 0, 0, 2, 48, 3, 0, 0,
0, 0, 22, 4, 0, 0, 0, 0, 0, 0, 1, 87, 0, 0, 3, 0, 0, 4, 1, 0,
82, 7, 0, 0, 0, 7, 22, 34, 17, 0, 0, 0, 0, 0, 2, 19, 3, 0, 990,
0, 0, 0, 0, 84, 9, 0, 5, 1246, 1944, 633, 23640, 262, 5399, 83,
19, 4417, 125, 7801, 69, 6755, 6, 39, 262), i.water_content_percent_es = c(98,
39, 85, 14, 21, 28, 50, 83, 21, 59, 20, 66, 61, 70, 46, 23, 8,
17, 10, 75, 52, 13, 9, 8, 47, 8, 8, 46, 86, 24, 17, 31, 35, 19,
32, 40, 79, 22, 49, 91, 15, 90, 63, 90, 60, 53, 29, 91, 98, 39,
85, 14, 21, 28, 50, 83, 21, 59, 20, 66, 61, 70, 46, 23, 8, 17,
10, 75, 52, 13, 9, 8, 47, 8, 8, 46, 86, 24, 17, 39, 85, 14, 21,
28, 50, 83, 21, 59, 20, 66, 61, 70, 46, 98, 23, 8, 17, 10, 75,
52, 13, 9, 8, 47, 8, 8, 46, 86, 24, 17, 31, 35, 19, 32, 40, 79,
22, 49, 91, 15, 90, 63, 90, 60, 53, 29, 91, 98, 39, 85, 14, 21,
28, 50, 83, 21, 59, 20, 66, 61, 70, 46, 23, 8, 17, 10, 75, 52,
13, 9, 8, 47, 8, 8, 46, 86, 24, 17, 31, 35, 19, 32, 40, 79, 22,
49, 91, 15, 90, 63, 90, 60, 53, 29, 91, 31, 35, 19, 32, 40, 79,
22, 49, 91, 15, 90, 63, 90, 60, 53, 29, 91), daysincubated4 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 116, 116, 116,
116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116,
116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116,
116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116,
116, 116, 116, 116, 116, 116, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4), days = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 116, 116, 116, 116, 116, 116, 116, 116, 116,
116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116,
116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116,
116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4), water_content = c(98,
39, 85, 14, 21, 28, 50, 83, 21, 59, 20, 66, 61, 70, 46, 23, 8,
17, 10, 75, 52, 13, 9, 8, 47, 8, 8, 46, 86, 24, 17, 31, 35, 19,
32, 40, 79, 22, 49, 91, 15, 90, 63, 90, 60, 53, 29, 91, 98, 39,
85, 14, 21, 28, 50, 83, 21, 59, 20, 66, 61, 70, 46, 23, 8, 17,
10, 75, 52, 13, 9, 8, 47, 8, 8, 46, 86, 24, 17, 39, 85, 14, 21,
28, 50, 83, 21, 59, 20, 66, 61, 70, 46, 98, 23, 8, 17, 10, 75,
52, 13, 9, 8, 47, 8, 8, 46, 86, 24, 17, 31, 35, 19, 32, 40, 79,
22, 49, 91, 15, 90, 63, 90, 60, 53, 29, 91, 98, 39, 85, 14, 21,
28, 50, 83, 21, 59, 20, 66, 61, 70, 46, 23, 8, 17, 10, 75, 52,
13, 9, 8, 47, 8, 8, 46, 86, 24, 17, 31, 35, 19, 32, 40, 79, 22,
49, 91, 15, 90, 63, 90, 60, 53, 29, 91, 31, 35, 19, 32, 40, 79,
22, 49, 91, 15, 90, 63, 90, 60, 53, 29, 91), acetone = c(22,
17, 11, 1782, 1767, 250, 3568, 79, 219, 855, 12009, 395, 1552,
705, 2282, 84, 3396, 252, 2058, 1480, 5, 745, 2573, 1005, 946,
3320, 5406, 2192, 20, 1207, 9519, 66, 463, 250, 1095, 16556,
88, 2695, 275, 16, 1577, 29, 3221, 25, 6295, 2, 63, 123, 8, 1,
37, 5308, 4546, 994, 4567, 421, 0, 1938, 19480, 1027, 3474, 1982,
2819, 69, 27733, 2152, 15429, 996, 8, 3435, 8748, 17062, 269,
26188, 35823, 2572, 67, 761, 13493, 1, 1, 1, 16, 9, 29, 89, 20,
11, 21644, 3, 37, 13, 0, 0, 0, 0, 3, 30, 19, 0, 0, 242, 7246,
1, 20081, 77, 0, 0, 0, 5878, 0, 0, 22, 2, 4, 1, 93, 12, 2, 73,
0, 19, 0, 0, 2, 48, 3, 0, 0, 0, 0, 22, 4, 0, 0, 0, 0, 0, 0, 1,
87, 0, 0, 3, 0, 0, 4, 1, 0, 82, 7, 0, 0, 0, 7, 22, 34, 17, 0,
0, 0, 0, 0, 2, 19, 3, 0, 990, 0, 0, 0, 0, 84, 9, 0, 5, 1246,
1944, 633, 23640, 262, 5399, 83, 19, 4417, 125, 7801, 69, 6755,
6, 39, 262)), row.names = c(NA, -192L), class = "data.frame")
and the code for the graph I've made is:
library(ggpmisc)
library(tidyverse)
formula <- y~x
ggplot(df, aes(water_content, acetone)) +
geom_point() +
geom_smooth(method = "lm",formula = y~x) +
theme_bw()+
facet_wrap(~days, scales = "free")+
stat_poly_eq(
aes(label = paste(stat(adj.rr.label), stat(eq.label), stat(p.value.label), sep = "*\", \"*")),
formula = formula, parse = TRUE, size=3)
Any ideas why I don't get the same slope values?
All help is much appreciated!
You swapped x and y. Possibly because of using complex 'tidyverse' coding this was not obvious.
library(nlme)
lmList(acetone ~ water_content | days, data = df)
gives
Call:
Model: acetone ~ water_content | days
Data: df
Coefficients:
(Intercept) water_content
0 3314.26811 -31.663431
4 12046.87296 -154.277916
24 3103.13075 -44.368527
116 63.82385 -0.792739
Degrees of freedom: 192 total; 184 residual
Residual standard error: 4538.636

How do I use column index as x axis in R

I have a data frame with 7 columns and 100 observations
I divided observations into two groups
the question I'm working on is: b) Construct two time plots of the mean blood lead levels superimposed on the blood lead levels at each occasion for succimer and placebo groups.
This is my code so far:
library(tidyverse)
library(haven)
library(dplyr)
library(plyr)
library(foreign)
library(ggplot2)
tlc = read_dta(file = 'tlc.dta')
head(tlc)
## a)
placebo = subset(tlc, tlc$trt==0)
succimer = subset(tlc, tlc$trt==1)
summary(placebo[, 3:6])
summary(succimer[, 3:6])
placebo_mean=colMeans(placebo[ ,3:6])
placebo_std=apply(placebo[ ,3:6],2,sd)
placebo_var=placebo_std^2
succimer_mean=colMeans(succimer[ ,3:6])
succimer_std=apply(succimer[ ,3:6],2,sd)
succimer_var=succimer_std^2
## b)
## c)
placebo_cor=cor(placebo[ , 3:6]) %>% round(digits = 3)
succimer_cor=cor(succimer[ , 3:6]) %>% round(digits = 3)
placebo_cov=cov(placebo[ , 3:6]) %>% round(digits = 3)
succimer_cov=cov(succimer[ , 3:6]) %>% round(digits = 3)
So the purpose is to plot all observation by using values as y axis, and columns y0, y1, y4, y6 (represent to week 0, week 1, week 4, week 6) as x axis, then plot the mean of each group superimposed on the plot. I'm planning to use different colors to distinguish two groups, so the final plot will have a lot of points on each x coordinate, and two short lines to indicate means for each group at each x coordinate.
My question is how to use column index as x axis in R? with or with out using ggplot. I know this question may be too elementary, but it caused a lot of trouble for me as a beginner.
below is my data:
dput(tlc)
structure(list(id = structure(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, 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, 58,
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
91, 92, 93, 94, 95, 96, 97, 98, 99, 100), format.stata = "%9.0g"),
trt = structure(c(0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1,
0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0,
0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1,
1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1), format.stata = "%9.0g", class = "haven_labelled", labels = c(Placebo = 0,
Succimer = 1)), y0 = structure(c(30.7999992370605, 26.5,
25.7999992370605, 24.7000007629395, 20.3999996185303, 20.3999996185303,
28.6000003814697, 33.7000007629395, 19.7000007629395, 31.1000003814697,
19.7999992370605, 24.7999992370605, 21.3999996185303, 27.8999996185303,
21.1000003814697, 20.6000003814697, 24, 37.5999984741211,
35.2999992370605, 28.6000003814697, 31.8999996185303, 29.6000003814697,
21.5, 26.2000007629395, 21.7999992370605, 23, 22.2000007629395,
20.5, 25, 33.2999992370605, 26, 19.7000007629395, 27.8999996185303,
24.7000007629395, 28.7999992370605, 29.6000003814697, 32,
21.7999992370605, 24.3999996185303, 33.7000007629395, 24.8999996185303,
19.7999992370605, 26.7000007629395, 26.7999992370605, 20.2000007629395,
35.4000015258789, 25.2999992370605, 20.2000007629395, 24.5,
20.2999992370605, 20.3999996185303, 24.1000003814697, 27.1000003814697,
34.7000007629395, 28.5, 26.6000003814697, 24.5, 20.5, 25.2000007629395,
34.7000007629395, 30.2999992370605, 26.6000003814697, 20.7000007629395,
27.7000007629395, 24.2999992370605, 36.5999984741211, 28.8999996185303,
34, 32.5999984741211, 29.2000007629395, 26.3999996185303,
21.7999992370605, 27.2000007629395, 22.3999996185303, 32.5,
24.8999996185303, 24.6000003814697, 23.1000003814697, 21.1000003814697,
25.7999992370605, 30, 22.1000003814697, 20, 38.0999984741211,
28.8999996185303, 25.1000003814697, 19.7999992370605, 22.1000003814697,
23.5, 29.1000003814697, 30.2999992370605, 25.3999996185303,
30.6000003814697, 22.3999996185303, 31.2000007629395, 31.3999996185303,
41.0999984741211, 29.3999996185303, 21.8999996185303, 20.7000007629395
), format.stata = "%9.0g"), y1 = structure(c(26.8999996185303,
14.8000001907349, 23, 24.5, 2.79999995231628, 5.40000009536743,
20.7999992370605, 31.6000003814697, 14.8999996185303, 31.2000007629395,
17.5, 23.1000003814697, 26.2999992370605, 6.30000019073486,
20.2999992370605, 23.8999996185303, 16.7000007629395, 33.7000007629395,
25.5, 15.8000001907349, 27.8999996185303, 15.8000001907349,
6.5, 26.7999992370605, 12, 4.19999980926514, 11.5, 21.1000003814697,
3.90000009536743, 26.2000007629395, 21.3999996185303, 13.1999998092651,
21.6000003814697, 21.2000007629395, 26.3999996185303, 17.5,
30.2000007629395, 19.2999992370605, 16.3999996185303, 14.8999996185303,
20.8999996185303, 18.8999996185303, 6.40000009536743, 20.3999996185303,
10.6000003814697, 30.3999996185303, 23.8999996185303, 17.5,
10, 21, 17.2000007629395, 20.1000003814697, 14.8999996185303,
39, 32.5999984741211, 22.3999996185303, 5.09999990463257,
17.5, 25.1000003814697, 39.5, 29.3999996185303, 25.2999992370605,
19.2999992370605, 4, 24.2999992370605, 23.2999992370605,
28.8999996185303, 10.6999998092651, 19, 9.19999980926514,
15.3000001907349, 10.6000003814697, 28.5, 22, 25.1000003814697,
23.6000003814697, 25, 20.8999996185303, 5.59999990463257,
21.8999996185303, 27.6000003814697, 21, 22.7000007629395,
40.7999992370605, 12.5, 28.1000003814697, 11.6000003814697,
21.1000003814697, 7.90000009536743, 16.7999992370605, 3.5,
24.2999992370605, 28.2000007629395, 7.09999990463257, 10.8000001907349,
3.90000009536743, 15.1000003814697, 22.1000003814697, 7.59999990463257,
8.10000038146973), format.stata = "%9.0g"), y4 = structure(c(25.7999992370605,
19.5, 19.1000003814697, 22, 3.20000004768372, 4.5, 19.2000007629395,
28.5, 15.3000001907349, 29.2000007629395, 20.5, 24.6000003814697,
19.5, 18.5, 18.3999996185303, 19, 21.7000007629395, 34.4000015258789,
26.2999992370605, 22.8999996185303, 27.2999992370605, 23.7000007629395,
7.09999990463257, 25.2999992370605, 16.7999992370605, 4,
9.5, 17.3999996185303, 12.8000001907349, 34, 21, 14.6000003814697,
23.6000003814697, 22.8999996185303, 23.7999992370605, 21,
30.2000007629395, 16.3999996185303, 11.6000003814697, 14.5,
22.2000007629395, 18.8999996185303, 5.09999990463257, 19.2999992370605,
9, 26.5, 22.2000007629395, 17.3999996185303, 15.6000003814697,
16.7000007629395, 15.8999996185303, 17.8999996185303, 18.1000003814697,
28.7999992370605, 27.5, 21.7999992370605, 8.19999980926514,
19.6000003814697, 23.3999996185303, 38.5999984741211, 33.0999984741211,
25.1000003814697, 21.8999996185303, 4.19999980926514, 18.3999996185303,
40.4000015258789, 32.7999992370605, 12.6000003814697, 16.2999992370605,
8.30000019073486, 24.6000003814697, 14.3999996185303, 35,
19.1000003814697, 27.7999992370605, 21.2000007629395, 21.7000007629395,
21.7000007629395, 7.30000019073486, 23.6000003814697, 24,
8.60000038146973, 21.2000007629395, 38, 16.7000007629395,
27.5, 13, 21.5, 12.3999996185303, 15.1000003814697, 3, 22.7000007629395,
27, 17.2000007629395, 19.7999992370605, 7, 10.8999996185303,
25.2999992370605, 10.8000001907349, 25.7000007629395), format.stata = "%9.0g"),
y6 = structure(c(23.7999992370605, 21, 23.2000007629395,
22.5, 9.39999961853027, 11.8999996185303, 18.3999996185303,
25.1000003814697, 14.6999998092651, 30.1000003814697, 27.5,
30.8999996185303, 19, 16.2999992370605, 20.7999992370605,
17, 20.2999992370605, 31.3999996185303, 30.2999992370605,
25.8999996185303, 34.2000007629395, 23.3999996185303, 16,
24.7999992370605, 19.2000007629395, 16.2000007629395, 14.5,
21.1000003814697, 12.6999998092651, 28.2000007629395, 22.3999996185303,
11.6000003814697, 27.7000007629395, 21.8999996185303, 22,
24.2000007629395, 27.5, 17.6000003814697, 16.6000003814697,
63.9000015258789, 19.7999992370605, 15.5, 15.1000003814697,
23.7999992370605, 16, 28.1000003814697, 27.2000007629395,
18.6000003814697, 15.1999998092651, 13.5, 17.7000007629395,
18.7000007629395, 21.2999992370605, 34.7000007629395, 22.7999992370605,
21, 23.6000003814697, 18.3999996185303, 22.2000007629395,
43.2999992370605, 28.3999996185303, 27.8999996185303, 21.7999992370605,
11.6999998092651, 27.7999992370605, 39.2999992370605, 31.7999992370605,
21.2000007629395, 18.6000003814697, 18.3999996185303, 32.4000015258789,
18.7000007629395, 30.5, 18.7000007629395, 27.2999992370605,
21.1000003814697, 23.8999996185303, 19.8999996185303, 12.3000001907349,
24.7999992370605, 23.7000007629395, 24.6000003814697, 20.5,
32.7000007629395, 22.2000007629395, 24.7999992370605, 23.1000003814697,
20.6000003814697, 18.8999996185303, 18.7999992370605, 11.5,
20.1000003814697, 25.5, 18.7000007629395, 22.2000007629395,
17.7999992370605, 27.1000003814697, 4.09999990463257, 13,
12.3000001907349), format.stata = "%9.0g")), row.names = c(NA,
-100L), class = c("tbl_df", "tbl", "data.frame"))
also I have tried this:
p=ggplot(tlc, aes(x=colnames(tlc[,3:6],do.NULL=TRUE)),
y=value)
p=p+geom_point()
No errors found when running the code, but R did report an error (Aesthetics must be either length 1 or the same as the data (100): x) when I call 'p' to plot it.
I don't have your data, but it sounds like you want something that looks like this:
Here is how I made it:
library(tidyverse)
# Setting up some fake data: 100 observations and 7 variables
set.seed(123)
some_data <- data.frame(y0 = rnorm(100),
y1 = runif(100),
y2 = rexp(100, 2),
y3 = rnorm(100, 2, 1),
y4 = rexp(100),
y5 = rnorm(100, 2,2),
y6 = runif(100, -5, 5))
# pivoting the data to longer format:
long_data <- some_data %>%
pivot_longer(cols = everything(),
names_to = "variable")
# building the base plot
p <- ggplot(long_data, aes(x = variable, y = value))
# adding the points - use position_jitter to give it some width if you want
p <- p + geom_point(position = position_jitter(width = 0.2))
# adding the bars at mean - play around with width, color, and size
p <- p + stat_summary(geom = "errorbar",
fun = mean,
width = 0.4,
aes(ymax = ..y.., ymin = ..y..),
color = "orange",
size = 1.5)
p # show plot

Assign the same colors to the nodes in different graphics

I have a network and I found the degree of the nodes.
I created two charts:
the first represents the network in such a way that the nodes of degree greater are larger and I used the RColorBrewer library for coloring the nodes
the second graph is a barplot which always represents the degree of each node.
I wish the same nodes have the same color.
For example, the Valjean node in the first graph is green/blue, while in the second is yellow.
How can I do so that it has the same color in both graphs?
This is the code:
library(igraph)
library(RColorBrewer)
library(sna)
library(scales)
library(plotrix)
net <- read.graph("./s.gml", format = c("gml"))
dput(net)
# find degree
deg <- igraph::degree(net, mode = "all")
pal <- rev(colorRampPalette(RColorBrewer::brewer.pal(9, "Set2"))(max(deg)))
##### FIRST CHART ######
labelSpecific <- ifelse(igraph::degree(net) > 35, V(net)$label, NA)
lab1 <- labelSpecific
V(net)$label.color <- "black"
x11()
V(net)$frame.color <- NA
plot(net,
vertex.color = pal[deg - min(deg) + 1],
vertex.size = plotrix::rescale(deg, c(4, 15)),
vertex.label = lab1,
main = "Degree")
###### SECOND CHART #########
mar.default <- c(5, 4, 4, 2) + 0.1
par(mar = mar.default + c(4, 0, 0, 0))
V(net)$label <- V(net)$label
x11()
barplot(height = deg,
space = 1,
names.arg = V(net)$label,
main = "Degree",
ylab = "Degree",
ylim = c(0, 40),
cex.axis = 1,
cex.names = 0.6,
col = pal,
border = NA,
las = 2)
I get this image.
My network is:
> dput(net)
structure(list(77, FALSE, c(1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 11,
11, 11, 11, 12, 13, 14, 15, 17, 18, 18, 19, 19, 19, 20, 20, 20,
20, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23,
23, 23, 23, 23, 23, 24, 24, 25, 25, 25, 26, 26, 26, 26, 27, 27,
27, 27, 27, 28, 28, 29, 29, 29, 30, 31, 31, 31, 31, 32, 33, 33,
34, 34, 35, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37, 37, 38, 38,
38, 38, 38, 38, 39, 40, 41, 41, 42, 42, 42, 43, 43, 43, 44, 44,
45, 47, 48, 48, 48, 48, 49, 49, 50, 50, 51, 51, 51, 52, 52, 53,
54, 54, 54, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 56, 56, 57,
57, 57, 58, 58, 58, 58, 58, 59, 59, 59, 59, 60, 60, 60, 61, 61,
61, 61, 61, 61, 62, 62, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63,
63, 63, 63, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 65, 65,
65, 65, 65, 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 66, 66, 66,
66, 67, 68, 68, 68, 68, 68, 68, 69, 69, 69, 69, 69, 69, 69, 70,
70, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 71, 71, 71, 72,
72, 72, 73, 74, 74, 75, 75, 75, 75, 75, 75, 75, 76, 76, 76, 76,
76, 76, 76), c(0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 10, 3, 2, 0, 11,
11, 11, 11, 16, 16, 17, 16, 17, 18, 16, 17, 18, 19, 16, 17, 18,
19, 20, 16, 17, 18, 19, 20, 21, 16, 17, 18, 19, 20, 21, 22, 12,
11, 23, 11, 24, 23, 11, 24, 11, 16, 25, 11, 23, 25, 24, 26, 11,
27, 23, 27, 11, 23, 30, 11, 23, 27, 11, 11, 27, 11, 29, 11, 34,
29, 34, 35, 11, 29, 34, 35, 36, 11, 29, 34, 35, 36, 37, 11, 29,
25, 25, 24, 25, 41, 25, 24, 11, 26, 27, 28, 11, 28, 46, 47, 25,
27, 11, 26, 11, 49, 24, 49, 26, 11, 51, 39, 51, 51, 49, 26, 51,
49, 39, 54, 26, 11, 16, 25, 41, 48, 49, 55, 55, 41, 48, 55, 48,
27, 57, 11, 58, 55, 48, 57, 48, 58, 59, 48, 58, 60, 59, 57, 55,
55, 58, 59, 48, 57, 41, 61, 60, 59, 48, 62, 57, 58, 61, 60, 55,
55, 62, 48, 63, 58, 61, 60, 59, 57, 11, 63, 64, 48, 62, 58, 61,
60, 59, 57, 55, 64, 58, 59, 62, 65, 48, 63, 61, 60, 57, 25, 11,
24, 27, 48, 41, 25, 68, 11, 24, 27, 48, 41, 25, 69, 68, 11, 24,
27, 41, 58, 27, 69, 68, 70, 11, 48, 41, 25, 26, 27, 11, 48, 48,
73, 69, 68, 25, 48, 41, 70, 71, 64, 65, 66, 63, 62, 48, 58),
c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 13, 12, 11, 10, 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, 47, 46, 39, 40, 41, 42, 43, 44,
45, 49, 48, 52, 51, 50, 54, 55, 53, 56, 57, 58, 60, 59, 61,
62, 63, 66, 64, 65, 67, 69, 70, 71, 68, 72, 73, 74, 75, 76,
77, 79, 78, 82, 83, 80, 81, 87, 88, 84, 85, 86, 93, 94, 89,
90, 91, 92, 95, 96, 97, 98, 101, 100, 99, 102, 103, 104,
106, 105, 107, 108, 112, 110, 111, 109, 114, 113, 116, 115,
119, 118, 117, 121, 120, 122, 125, 124, 123, 131, 132, 133,
130, 128, 134, 135, 127, 126, 129, 136, 137, 139, 140, 138,
145, 143, 142, 141, 144, 148, 147, 149, 146, 150, 151, 152,
153, 158, 157, 154, 156, 155, 164, 162, 159, 163, 160, 161,
166, 165, 168, 174, 170, 171, 167, 173, 172, 169, 184, 177,
175, 183, 179, 182, 181, 180, 176, 178, 187, 194, 193, 189,
192, 191, 190, 188, 185, 186, 200, 196, 197, 203, 202, 198,
201, 195, 199, 204, 206, 207, 205, 208, 210, 209, 213, 214,
211, 215, 217, 216, 212, 221, 222, 218, 223, 224, 225, 220,
219, 230, 233, 226, 232, 231, 228, 227, 229, 236, 234, 235,
237, 238, 239, 242, 244, 243, 241, 240, 245, 246, 252, 253,
251, 250, 247, 248, 249), c(0, 1, 2, 4, 5, 6, 7, 8, 9, 13,
3, 12, 11, 10, 14, 15, 16, 17, 47, 49, 52, 54, 57, 62, 66,
69, 72, 73, 75, 77, 82, 87, 93, 102, 106, 112, 114, 119,
131, 145, 184, 206, 213, 221, 230, 236, 46, 18, 19, 21, 24,
28, 33, 39, 55, 132, 20, 22, 25, 29, 34, 40, 23, 26, 30,
35, 41, 27, 31, 36, 42, 32, 37, 43, 38, 44, 45, 48, 51, 58,
64, 67, 70, 50, 53, 60, 97, 101, 116, 207, 214, 222, 56,
59, 95, 96, 98, 100, 110, 133, 205, 211, 218, 233, 242, 61,
103, 113, 118, 125, 130, 234, 63, 65, 71, 74, 104, 111, 143,
208, 215, 223, 226, 235, 105, 107, 76, 79, 83, 88, 94, 68,
78, 80, 84, 89, 81, 85, 90, 86, 91, 92, 121, 128, 99, 134,
139, 164, 210, 217, 224, 232, 244, 108, 109, 135, 140, 142,
148, 150, 153, 162, 168, 177, 187, 200, 209, 216, 231, 237,
238, 243, 252, 115, 117, 124, 127, 136, 120, 122, 123, 126,
129, 137, 138, 141, 147, 158, 159, 174, 175, 194, 144, 149,
157, 163, 170, 183, 193, 204, 146, 151, 154, 160, 171, 179,
189, 196, 225, 253, 152, 156, 161, 167, 182, 192, 197, 155,
166, 173, 181, 191, 203, 165, 172, 180, 190, 202, 169, 176,
188, 198, 251, 178, 185, 201, 250, 186, 195, 247, 199, 248,
249, 212, 220, 228, 241, 219, 227, 240, 229, 245, 246, 239
), c(0, 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 10, 14, 15, 16, 17,
18, 18, 19, 21, 24, 28, 33, 39, 48, 50, 53, 57, 62, 64, 67,
68, 72, 73, 75, 77, 80, 84, 89, 95, 96, 97, 99, 102, 105,
107, 108, 108, 109, 113, 115, 117, 120, 122, 123, 126, 136,
138, 141, 146, 150, 153, 159, 167, 175, 185, 195, 204, 205,
211, 218, 226, 234, 237, 238, 240, 247, 254), c(0, 10, 10,
12, 13, 13, 13, 13, 13, 13, 13, 14, 46, 47, 47, 47, 47, 56,
62, 67, 71, 74, 76, 77, 83, 92, 105, 112, 124, 126, 131,
132, 132, 132, 132, 136, 139, 141, 142, 142, 144, 144, 153,
153, 153, 153, 153, 154, 155, 173, 178, 178, 182, 182, 182,
183, 192, 192, 200, 210, 217, 223, 228, 233, 237, 240, 242,
243, 243, 247, 250, 252, 253, 253, 254, 254, 254, 254), list(
c(1, 0, 1), structure(list(), .Names = character(0)),
structure(list(id = 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, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
67, 68, 69, 70, 71, 72, 73, 74, 75, 76), label = c("Myriel",
"Napoleon", "MlleBaptistine", "MmeMagloire", "CountessDeLo",
"Geborand", "Champtercier", "Cravatte", "Count", "OldMan",
"Labarre", "Valjean", "Marguerite", "MmeDeR", "Isabeau",
"Gervais", "Tholomyes", "Listolier", "Fameuil", "Blacheville",
"Favourite", "Dahlia", "Zephine", "Fantine", "MmeThenardier",
"Thenardier", "Cosette", "Javert", "Fauchelevent", "Bamatabois",
"Perpetue", "Simplice", "Scaufflaire", "Woman1", "Judge",
"Champmathieu", "Brevet", "Chenildieu", "Cochepaille",
"Pontmercy", "Boulatruelle", "Eponine", "Anzelma", "Woman2",
"MotherInnocent", "Gribier", "Jondrette", "MmeBurgon",
"Gavroche", "Gillenormand", "Magnon", "MlleGillenormand",
"MmePontmercy", "MlleVaubois", "LtGillenormand", "Marius",
"BaronessT", "Mabeuf", "Enjolras", "Combeferre", "Prouvaire",
"Feuilly", "Courfeyrac", "Bahorel", "Bossuet", "Joly",
"Grantaire", "MotherPlutarch", "Gueulemer", "Babet",
"Claquesous", "Montparnasse", "Toussaint", "Child1",
"Child2", "Brujon", "MmeHucheloup"), maincharacter = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0)), .Names = c("id", "label", "maincharacter"
)), structure(list(value = c(1, 8, 10, 6, 1, 1, 1, 1,
2, 1, 1, 3, 3, 5, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 3, 3,
3, 4, 3, 3, 3, 3, 5, 3, 3, 3, 3, 4, 4, 3, 3, 3, 3, 4,
4, 4, 2, 9, 2, 7, 13, 1, 12, 4, 31, 1, 1, 17, 5, 5, 1,
1, 8, 1, 1, 1, 2, 1, 2, 3, 2, 1, 1, 2, 1, 3, 2, 3, 3,
2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 1, 1,
2, 3, 2, 2, 1, 3, 1, 1, 3, 1, 2, 1, 2, 1, 1, 1, 3, 2,
1, 1, 9, 2, 2, 1, 1, 1, 2, 1, 1, 6, 12, 1, 1, 21, 19,
1, 2, 5, 4, 1, 1, 1, 1, 1, 7, 7, 6, 1, 4, 15, 5, 6, 2,
1, 4, 2, 2, 6, 2, 5, 1, 1, 9, 17, 13, 7, 2, 1, 6, 3,
5, 5, 6, 2, 4, 3, 2, 1, 5, 12, 5, 4, 10, 6, 2, 9, 1,
1, 5, 7, 3, 5, 5, 5, 2, 5, 1, 2, 3, 3, 1, 2, 2, 1, 1,
1, 1, 3, 5, 1, 1, 1, 1, 1, 6, 6, 1, 1, 2, 1, 1, 4, 4,
4, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1, 2,
2, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = "value")),
<environment>), class = "igraph")
You are passing two different colors vectors to plot and to barplot.
Just use the same code for both, e.g. in the barplot use:
barplot(
...
col = pal[deg - min(deg) + 1]
...
)

Resources