I know that polr does not give p-values because they are not very reliable. Nevertheless, I would like to add them to my modelsummary (Vignette) output. I know to get the values as follows:
library(MASS)
polr_res <- polr(as.ordered(rep77) ~ foreign + length + mpg, Hess=TRUE, data=fullauto);summary(polr_res)
Call:
polr_res(formula = as.ordered(rep77) ~ foreign + length + mpg, data = fullauto,
Hess = TRUE)
## coefficient test
library("AER")
coeftest(polr_res)
modelsummary
Because polr has no p-values, I cannot call modelsummary(models, stars=TRUE) on my models (which includes other models which do have p-values and for which I want to show stars).
library(modelsummary)
models <- list(
"Ordinal Probit" = polr_res,
)
# model_names <- c("OLS", "")
modelsummary(models, stars=TRUE)
I tried first to simply add the p-values to the tidy object, but I cannot add that object to the list of models.
polr_pval <- coeftest(polr)[,4]
polr_pval <- as.data.frame(polr_pval)
tidy_polr <- tidy(polr)
tidy_polr[,5] <- polr_pval
The vignette describes that I can make a custom class which adapts the polr, but I do not understand how:
https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html#customizing-existing-models-part-i-
https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html#customizing-existing-models-part-ii-
Could anyone help me figure this out?
EDIT:
I am posting an edit showing the problem I was having when using Vincent's answer, with R version 3.6.1 (2019-07-05). If you are encountering this issue, (preferably) update to R version 4.0.0 or download an update for modelsummary from Github (see also Vincent's comments below).:
library(remotes)
remotes::install_github('vincentarelbundock/modelsummary')
Output:
DATA for R
fullauto <- structure(list(make = structure(c(1, 1, 1, 2, 2, 3, 4, 4, 4,
4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
9, 10, 10, 11, 11, 12, 12, 12, 13, 14, 14, 14, 14, 14, 14, 15,
15, 15, 15, 15, 15, 15, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18,
18, 18, 19, 20, 21, 21, 21, 22, 22, 22, 22, 23), label = "Make", format.stata = "%8.0g", class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(AMC = 1, Audi = 2, BMW = 3,
Buick = 4, Cad. = 5, Chev. = 6, Datsun = 7, Dodge = 8, Fiat = 9,
Ford = 10, Honda = 11, Linc. = 12, Mazda = 13, Merc. = 14, Olds = 15,
Peugeot = 16, Plym. = 17, Pont. = 18, Renault = 19, Subaru = 20,
Toyota = 21, VW = 22, Volvo = 23)), model = structure(c(1, 2,
3, 4, 5000, 320, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 200, 210, 510, 810, 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, 98, 604, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
58, 59, 60, 61, 62, 63, 64, 65, 260), label = "Model", format.stata = "%8.0g", class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(Concord = 1, Pacer = 2, Spirit = 3,
Fox = 4, Century = 5, Electra = 6, LeSabre = 7, Opel = 8, Regal = 9,
Riviera = 10, Skylark = 11, Deville = 12, Eldrado = 13, Seville = 14,
Chevette = 15, Impala = 16, Malibu = 17, MCarlo = 18, Monza = 19,
Nova = 20, Colt = 21, Diplomat = 22, Magnum = 23, StRegis = 24,
Strada = 25, Fiesta = 26, Mustang = 27, Accord = 28, Civic = 29,
Cntntl = 30, `Mark V` = 31, Vrsills = 32, GLC = 33, Bobcat = 34,
Cougar = 35, `XR-7` = 36, Marquis = 37, Monarch = 38, Zephyr = 39,
Cutlass = 40, CutlSupr = 41, `Delta 88` = 42, Omega = 43, Starfire = 44,
Toronado = 45, Arrow = 46, Champ = 47, Horizon = 48, Sapporo = 49,
Volare = 50, Catalina = 51, Firebird = 52, GranPrix = 53, `Le Mans` = 54,
Phoenix = 55, Sunbird = 56, `Le Car` = 57, Subaru = 58, Celica = 59,
Corolla = 60, Corona = 61, Rabbit = 62, Diesel = 63, Scirocco = 64,
Dasher = 65)), price = structure(c(4099, 4749, 3799, 6295, 9690,
9735, 4816, 7827, 5788, 4453, 5189, 10372, 4082, 11385, 14500,
15906, 3299, 5705, 4504, 5104, 3667, 3955, 6229, 4589, 5079,
8129, 3984, 4010, 5886, 6342, 4296, 4389, 4187, 5799, 4499, 11497,
13594, 13466, 3995, 3829, 5379, 6303, 6165, 4516, 3291, 4733,
5172, 4890, 4181, 4195, 10371, 8814, 12990, 4647, 4425, 4482,
6486, 4060, 5798, 4934, 5222, 4723, 4424, 4172, 3895, 3798, 5899,
3748, 5719, 4697, 5397, 6850, 7140, 11995), label = "Price", format.stata = "%8.0g"),
mpg = structure(c(22, 17, 22, 23, 17, 25, 20, 15, 18, 26,
20, 16, 19, 14, 14, 21, 29, 16, 22, 22, 24, 19, 23, 35, 24,
21, 30, 18, 16, 17, 21, 28, 21, 25, 28, 12, 12, 14, 30, 22,
14, 14, 15, 18, 20, 19, 19, 18, 19, 24, 16, 21, 14, 38, 34,
25, 26, 18, 18, 18, 19, 19, 19, 24, 26, 35, 18, 31, 18, 25,
41, 25, 23, 17), label = "Mileage (mpg)", format.stata = "%8.0g"),
rep78 = structure(c(3, 3, NA, 3, 5, 4, 3, 4, 3, NA, 3, 3,
3, 3, 2, 3, 3, 4, 3, 2, 2, 3, 4, 5, 4, 4, 5, 2, 2, 2, 3,
4, 3, 5, 4, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 4, 3, 1,
3, 4, NA, 3, 5, 3, NA, 2, 4, 1, 3, 3, NA, 2, 3, 5, 5, 5,
5, 4, 5, 4, 4, 5), label = "Repair Record 1978", format.stata = "%9.0g", class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(Poor = 1, Fair = 2, Average = 3,
Good = 4, Excellent = 5)), rep77 = structure(c(2, 1, NA,
3, 2, 4, 3, 4, 4, NA, 3, 4, 3, 3, 2, 3, 3, 4, 3, 3, 2, 3,
3, 5, 4, 4, 4, 2, 2, 2, 1, NA, 3, 5, 4, 4, 4, 3, 4, 3, 3,
4, 2, NA, 3, 3, 4, 4, 3, 1, 3, 4, NA, 3, 4, NA, NA, 2, 4,
2, 3, 3, NA, 2, 3, 4, 5, 5, 5, 3, 4, 3, 3, 3), label = "Repair Record 1977", format.stata = "%9.0g", class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(Poor = 1, Fair = 2, Average = 3,
Good = 4, Excellent = 5)), hdroom = structure(c(2.5, 3, 3,
2.5, 3, 2.5, 4.5, 4, 4, 3, 2, 3.5, 3.5, 4, 3.5, 3, 2.5, 4,
3.5, 2, 2, 3.5, 1.5, 2, 2.5, 2.5, 2, 4, 4, 4.5, 2.5, 1.5,
2, 3, 2.5, 3.5, 2.5, 3.5, 3.5, 3, 3.5, 3, 3.5, 3, 3.5, 4.5,
2, 4, 4.5, 2, 3.5, 4, 3.5, 2, 2.5, 4, 1.5, 5, 4, 1.5, 2,
3.5, 3.5, 2, 3, 2.5, 2.5, 3, 2, 3, 3, 2, 2.5, 2.5), label = "Headroom (in.)", format.stata = "%6.1f"),
rseat = structure(c(27.5, 25.5, 18.5, 28, 27, 26, 29, 31.5,
30.5, 24, 28.5, 30, 27, 31.5, 30, 30, 26, 29.5, 28.5, 28.5,
25, 27, 21, 23.5, 22, 27, 24, 29, 29, 28, 26.5, 26, 23, 25.5,
23.5, 30.5, 28.5, 27, 25.5, 25.5, 29.5, 25, 30.5, 27, 29,
28, 28, 29, 27, 25.5, 30, 31.5, 30.5, 21.5, 23, 25, 22, 31,
29, 23.5, 28.5, 28, 27, 25, 23, 25.5, 22, 24.5, 23, 25.5,
25.5, 23.5, 37.5, 29.5), label = "Rear Seat (in.)", format.stata = "%6.1f"),
trunk = structure(c(11, 11, 12, 11, 15, 12, 16, 20, 21, 10,
16, 17, 13, 20, 16, 13, 9, 20, 17, 16, 7, 13, 6, 8, 8, 8,
8, 17, 17, 21, 16, 9, 10, 10, 5, 22, 18, 15, 11, 9, 16, 16,
23, 15, 17, 16, 16, 20, 14, 10, 17, 20, 14, 11, 11, 17, 8,
16, 20, 7, 16, 17, 13, 7, 10, 11, 14, 9, 11, 15, 15, 16,
12, 14), label = "Trunk space (cu. ft.)", format.stata = "%8.0g"),
weight = structure(c(2930, 3350, 2640, 2070, 2830, 2650,
3250, 4080, 3670, 2230, 3280, 3880, 3400, 4330, 3900, 4290,
2110, 3690, 3180, 3220, 2750, 3430, 2370, 2020, 2280, 2750,
2120, 3600, 3600, 3740, 2130, 1800, 2650, 2240, 1760, 4840,
4720, 3830, 1980, 2580, 4060, 4130, 3720, 3370, 2830, 3300,
3310, 3690, 3370, 2730, 4030, 4060, 3420, 3260, 1800, 2200,
2520, 3330, 3700, 3470, 3210, 3200, 3420, 2690, 1830, 2050,
2410, 2200, 2670, 1930, 2040, 1990, 2160, 3170), label = "Weight (lbs.)", format.stata = "%8.0g"),
length = structure(c(186, 173, 168, 174, 189, 177, 196, 222,
218, 170, 200, 207, 200, 221, 204, 204, 163, 212, 193, 200,
179, 197, 170, 165, 170, 184, 163, 206, 206, 220, 161, 147,
179, 172, 149, 233, 230, 201, 154, 169, 221, 217, 212, 198,
195, 198, 198, 218, 200, 180, 206, 220, 192, 170, 157, 165,
182, 201, 214, 198, 201, 199, 203, 179, 142, 164, 174, 165,
175, 155, 155, 156, 172, 193), label = "Length (in.)", format.stata = "%8.0g"),
turn = structure(c(40, 40, 35, 36, 37, 34, 40, 43, 43, 34,
42, 43, 42, 44, 43, 45, 34, 43, 31, 41, 40, 43, 35, 32, 34,
38, 35, 46, 46, 46, 36, 33, 43, 36, 34, 51, 48, 41, 33, 39,
48, 45, 44, 41, 43, 42, 42, 42, 43, 40, 43, 43, 38, 37, 37,
36, 38, 44, 42, 42, 45, 40, 43, 41, 34, 36, 36, 35, 36, 35,
35, 36, 36, 37), label = "Turn Circle (ft.) ", format.stata = "%8.0g"),
displ = structure(c(121, 258, 121, 97, 131, 121, 196, 350,
231, 304, 196, 231, 231, 425, 350, 350, 231, 250, 200, 200,
151, 250, 119, 85, 119, 146, 98, 318, 318, 225, 105, 98,
140, 107, 91, 400, 400, 302, 86, 140, 302, 302, 302, 250,
140, 231, 231, 231, 231, 151, 350, 350, 163, 156, 86, 105,
119, 225, 231, 231, 231, 231, 231, 151, 79, 97, 134, 97,
134, 89, 90, 97, 97, 163), label = "Displacement (cu. in.)", format.stata = "%8.0g"),
gratio = structure(c(3.57999992370605, 2.52999997138977,
3.07999992370605, 3.70000004768372, 3.20000004768372, 3.64000010490417,
2.9300000667572, 2.41000008583069, 2.73000001907349, 2.86999988555908,
2.9300000667572, 2.9300000667572, 3.07999992370605, 2.27999997138977,
2.19000005722046, 2.24000000953674, 2.9300000667572, 2.55999994277954,
2.73000001907349, 2.73000001907349, 2.73000001907349, 2.55999994277954,
3.89000010490417, 3.70000004768372, 3.53999996185303, 3.54999995231628,
3.53999996185303, 2.47000002861023, 2.47000002861023, 2.94000005722046,
3.36999988555908, 3.15000009536743, 3.07999992370605, 3.04999995231628,
3.29999995231628, 2.47000002861023, 2.47000002861023, 2.47000002861023,
3.73000001907349, 2.73000001907349, 2.75, 2.75, 2.25999999046326,
2.4300000667572, 3.07999992370605, 2.9300000667572, 2.9300000667572,
2.73000001907349, 3.07999992370605, 2.73000001907349, 2.41000008583069,
2.41000008583069, 3.57999992370605, 3.04999995231628, 2.97000002861023,
3.36999988555908, 3.53999996185303, 3.23000001907349, 2.73000001907349,
3.07999992370605, 2.9300000667572, 2.9300000667572, 3.07999992370605,
2.73000001907349, 3.72000002861023, 3.80999994277954, 3.05999994277954,
3.21000003814697, 3.04999995231628, 3.77999997138977, 3.77999997138977,
3.77999997138977, 3.74000000953674, 2.98000001907349), label = "Gear Ratio", format.stata = "%6.2f"),
order = structure(c(1, 2, 3, 5, 4, 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, 47, 48, 49, 50, 51, 52, 46, 53, 54, 55, 56, 57,
58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
73, 74), label = "Original order", format.stata = "%8.0g"),
foreign = structure(c(0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1,
0, 0, 1, 1, 0, 0, 0, 1, 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, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1), label = "Foreign", format.stata = "%8.0g", class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(Domestic = 0, Foreign = 1
)), wgtd = structure(c(2930, 3350, 2640, NA, NA, NA, 3250,
4080, 3670, 2230, 3280, 3880, 3400, 4330, 3900, 4290, 2110,
3690, 3180, 3220, 2750, 3430, NA, NA, NA, NA, 2120, 3600,
3600, 3740, NA, 1800, 2650, NA, NA, 4840, 4720, 3830, NA,
2580, 4060, 4130, 3720, 3370, 2830, 3300, 3310, 3690, 3370,
2730, 4030, 4060, NA, 3260, 1800, 2200, 2520, 3330, 3700,
3470, 3210, 3200, 3420, 2690, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA), format.stata = "%9.0g"), wgtf = structure(c(NA,
NA, NA, 2070, 2830, 2650, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 2370, 2020, 2280, 2750, NA,
NA, NA, NA, 2130, NA, NA, 2240, 1760, NA, NA, NA, 1980, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 3420, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1830, 2050, 2410,
2200, 2670, 1930, 2040, 1990, 2160, 3170), format.stata = "%9.0g")), label = "Automobile Models", row.names = c(NA,
-74L), class = c("tbl_df", "tbl", "data.frame"))
I think the easiest way to achieve this is to define a tidy_custom.polr method as described here in the documentation.. For instance, you could do:
library(MASS)
library(AER)
library(modelsummary)
tidy_custom.polr <- function(x, ...) {
s <- coeftest(x)
out <- data.frame(
term = row.names(s),
p.value = s[, "Pr(>|z|)"])
out
}
mod = list(
"LM" = lm(gear ~ hp + mpg, data = mtcars),
"POLR" = polr(as.ordered(gear) ~ hp + mpg, data = mtcars))
modelsummary(mod, stars = TRUE)
Related
Good morning,
I have a ggplot2 bar graph inside a loop, in which the size of bars changes in every run of the loop:
ggplot(draft, aes(x=draft[,2], y=draft[,i])) +
geom_bar(stat="identity",fill="navyblue") +
geom_text(label=draft[,i],size=4, vjust=1.2, colour = "white",fontface=2) +
labs(title = paste("Session trends for",colnames(draft)[i],"-",player))+
theme(axis.text.x = element_text(angle = 0,color="black",size=8),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.title = element_text(color="black", size=10, face="bold",hjust = 0.5),
legend.position = "none",panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "black"))
In the geom_text part (labels of the bars), as you can see, size=4. This is fine for some graphs, but it is too big/small for others. My question is:
Is there any way to adjust the font size automatically to the size of the bar?
Thank you very much in advance.
Reproducible example.
This is the dataframe from where I represent the data:
> dput(draft)
structure(list(Player = c("Shaquil Delos", "Shaquil Delos", "Shaquil Delos",
"Shaquil Delos", "Shaquil Delos", "Shaquil Delos", "Shaquil Delos",
"Shaquil Delos", "Shaquil Delos", "Shaquil Delos", "Shaquil Delos",
"Shaquil Delos", "Shaquil Delos", "Shaquil Delos", "Shaquil Delos",
"Shaquil Delos"), Date = structure(c(19371, 19370, 19369, 19368,
19367, 19364, 19363, 19362, 19361, 19360, 19359, 19356, 19355,
19354, 19353, 19349), class = "Date"), week = c(29, 29, 29, 29,
29, 28, 28, 28, 28, 28, 28, 27, 27, 27, 27, 26), TD = c(6638,
2660, 4761, 4956, 3984, 4001, 3688, 4476, 4616, 4666, 4120, 3782,
3701, 4398, 4275, 3222), Time = c(96, 67, 70, 75, 38, 33, 55,
68, 59, 57, 56, 35, 45, 56, 46, 21), Vmax = c(9.4, 4.7, 8.3,
8.8, 6.9, 9.5, 6.5, 6, 9.2, 7.1, 7.3, 9, 6.5, 6.8, 6.2, 4.6),
Amax = c(4.5, 3.1, 4.1, 4.9, 3.8, 3.8, 4.4, 3.9, 4.5, 4.4,
4, 3.4, 4.3, 3.5, 3.2, 1.5), Dmax = c(-5.9, -2.8, -4.2, -6.2,
-3.9, -2.7, -4.9, -3.5, -2.7, -4.6, -3.5, -2.7, -4.5, -3.7,
-3.8, -1.4), Aerobic = c(462, 44, 589, 280, 175, 546, 333,
831, 303, 959, 225, 125, 194, 188, 534, 665), HSD = c(177,
0, 475, 86, 59, 463, 56, 14, 384, 92, 119, 393, 54, 262,
24, 0), SD = c(100, 0, 78, 39, 0, 149, 0, 0, 125, 3, 6, 141,
0, 0, 0, 0), Nsprints = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), DEC3 = c(7, 0, 2, 5, 0,
0, 8, 0, 0, 3, 0, 0, 3, 0, 0, 0), ACC3 = c(4, 0, 1, 4, 0,
0, 9, 0, 3, 4, 0, 0, 9, 0, 0, 0), RHIE = c(15, 3, 19, 10,
5, 5, 17, 6, 6, 10, 10, 3, 10, 5, 5, 2), MIP1 = c(161, 112,
169, 147, 184, 285, 162, 266, 170, 248, 197, 222, 167, 177,
232, 254), MIP3 = c(359, 309, 375, 345, 535, 738, 431, 688,
479, 709, 531, 549, 476, 461, 662, 724), MIP5 = c(565, 473,
560, 537, 868, 1049, 589, 851, 673, 1152, 845, 875, 682,
619, 983, 1166), ACC = c(78, 14, 64, 46, 27, 20, 51, 32,
29, 56, 40, 12, 45, 26, 21, 0), ACC2 = c(18, 1, 14, 15, 3,
9, 24, 9, 16, 12, 9, 6, 25, 4, 3, 0), DEC = c(67, 11, 42,
48, 23, 14, 36, 22, 14, 49, 32, 7, 30, 20, 15, 0), DEC2 = c(21,
0, 12, 14, 5, 0, 20, 4, 0, 11, 4, 0, 16, 1, 2, 0), Explosive = c(12,
9, 15, 13, 5, 1, 45, 11, 3, 8, 16, 0, 14, 11, 6, 0), TRIMP = c(107,
0, 78, 51, 105, 60, 85, 93, 55, 93, 102, 44, 89, 110, 109,
47), TRIMP4 = c(0, 0, 3, 12, 20, 8, 0, 0, 0, 13, 1, 2, 2,
15, 17, 15), TRIMP5 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0), TD.min = c(69.3, 39.9, 68.1, 66, 103.6, 121.4,
67.2, 65.7, 78.7, 82.1, 77.5, 109.3, 82.5, 78.8, 92.8, 151
), HRavg = c(66, 0, 65, 65, 79, 69, 67, 65, 60, 67, 74, 70,
71, 71, 75, 73), Vmax.perc = c(100, 50, 88, 93, 73, 101,
69, 64, 98, 75, 78, 95, 70, 72, 66, 49), Amax.perc = c(88,
62, 80, 96, 74, 75, 86, 77, 89, 87, 78, 67, 85, 68, 63, 30
), Dmax.perc = c(104, 50, 74, 109, 69, 48, 85, 61, 47, 81,
61, 47, 79, 65, 66, 25)), row.names = c("21", "211", "22",
"19", "191", "2", "20", "212", "201", "213", "221", "1", "11",
"202", "203", "18"), class = "data.frame")
And this is the bars graph I am building:
ggplot(draft, aes(x=draft[,2], y=draft[,1])) +
geom_bar(stat="identity",fill="navyblue") +
geom_text(label=draft[,i],size=4, vjust=1.2, colour = "white",fontface=2) +
labs(title = paste("Session trends for",colnames(draft)[1],"-",player))+
theme(axis.text.x = element_text(angle = 0,color="black",size=8),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.title = element_text(color="black", size=10, face="bold",hjust = 0.5),
legend.position = "none",panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "black"))
I may have misunderstood, but perhaps this type of approach would work for your use-case?
library(tidyverse)
draft <- structure(list(Player = c("Shaquil Delos", "Shaquil Delos", "Shaquil Delos",
"Shaquil Delos", "Shaquil Delos", "Shaquil Delos", "Shaquil Delos",
"Shaquil Delos", "Shaquil Delos", "Shaquil Delos", "Shaquil Delos",
"Shaquil Delos", "Shaquil Delos", "Shaquil Delos", "Shaquil Delos",
"Shaquil Delos"), Date = structure(c(19371, 19370, 19369, 19368,
19367, 19364, 19363, 19362, 19361, 19360, 19359, 19356, 19355,
19354, 19353, 19349), class = "Date"), week = c(29, 29, 29, 29,
29, 28, 28, 28, 28, 28, 28, 27, 27, 27, 27, 26), TD = c(6638,
2660, 4761, 4956, 3984, 4001, 3688, 4476, 4616, 4666, 4120, 3782,
3701, 4398, 4275, 3222), Time = c(96, 67, 70, 75, 38, 33, 55,
68, 59, 57, 56, 35, 45, 56, 46, 21), Vmax = c(9.4, 4.7, 8.3,
8.8, 6.9, 9.5, 6.5, 6, 9.2, 7.1, 7.3, 9, 6.5, 6.8, 6.2, 4.6),
Amax = c(4.5, 3.1, 4.1, 4.9, 3.8, 3.8, 4.4, 3.9, 4.5, 4.4,
4, 3.4, 4.3, 3.5, 3.2, 1.5), Dmax = c(-5.9, -2.8, -4.2, -6.2,
-3.9, -2.7, -4.9, -3.5, -2.7, -4.6, -3.5, -2.7, -4.5, -3.7,
-3.8, -1.4), Aerobic = c(462, 44, 589, 280, 175, 546, 333,
831, 303, 959, 225, 125, 194, 188, 534, 665), HSD = c(177,
0, 475, 86, 59, 463, 56, 14, 384, 92, 119, 393, 54, 262,
24, 0), SD = c(100, 0, 78, 39, 0, 149, 0, 0, 125, 3, 6, 141,
0, 0, 0, 0), Nsprints = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), DEC3 = c(7, 0, 2, 5, 0,
0, 8, 0, 0, 3, 0, 0, 3, 0, 0, 0), ACC3 = c(4, 0, 1, 4, 0,
0, 9, 0, 3, 4, 0, 0, 9, 0, 0, 0), RHIE = c(15, 3, 19, 10,
5, 5, 17, 6, 6, 10, 10, 3, 10, 5, 5, 2), MIP1 = c(161, 112,
169, 147, 184, 285, 162, 266, 170, 248, 197, 222, 167, 177,
232, 254), MIP3 = c(359, 309, 375, 345, 535, 738, 431, 688,
479, 709, 531, 549, 476, 461, 662, 724), MIP5 = c(565, 473,
560, 537, 868, 1049, 589, 851, 673, 1152, 845, 875, 682,
619, 983, 1166), ACC = c(78, 14, 64, 46, 27, 20, 51, 32,
29, 56, 40, 12, 45, 26, 21, 0), ACC2 = c(18, 1, 14, 15, 3,
9, 24, 9, 16, 12, 9, 6, 25, 4, 3, 0), DEC = c(67, 11, 42,
48, 23, 14, 36, 22, 14, 49, 32, 7, 30, 20, 15, 0), DEC2 = c(21,
0, 12, 14, 5, 0, 20, 4, 0, 11, 4, 0, 16, 1, 2, 0), Explosive = c(12,
9, 15, 13, 5, 1, 45, 11, 3, 8, 16, 0, 14, 11, 6, 0), TRIMP = c(107,
0, 78, 51, 105, 60, 85, 93, 55, 93, 102, 44, 89, 110, 109,
47), TRIMP4 = c(0, 0, 3, 12, 20, 8, 0, 0, 0, 13, 1, 2, 2,
15, 17, 15), TRIMP5 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0), TD.min = c(69.3, 39.9, 68.1, 66, 103.6, 121.4,
67.2, 65.7, 78.7, 82.1, 77.5, 109.3, 82.5, 78.8, 92.8, 151
), HRavg = c(66, 0, 65, 65, 79, 69, 67, 65, 60, 67, 74, 70,
71, 71, 75, 73), Vmax.perc = c(100, 50, 88, 93, 73, 101,
69, 64, 98, 75, 78, 95, 70, 72, 66, 49), Amax.perc = c(88,
62, 80, 96, 74, 75, 86, 77, 89, 87, 78, 67, 85, 68, 63, 30
), Dmax.perc = c(104, 50, 74, 109, 69, 48, 85, 61, 47, 81,
61, 47, 79, 65, 66, 25)), row.names = c("21", "211", "22",
"19", "191", "2", "20", "212", "201", "213", "221", "1", "11",
"202", "203", "18"), class = "data.frame")
for (i in 4:31) {
print(ggplot(draft, aes(x=draft[,2], y=draft[,i])) +
geom_bar(stat="identity",fill="navyblue") +
geom_text(label=as.integer(draft[,i]), size=5-nchar(as.integer(draft[3,i])),
vjust=1.2, colour = "white",fontface=2) +
labs(title = paste("Session trends for", colnames(draft)[i],"-", "player"))+
theme(axis.text.x = element_text(angle = 0,color="black",size=8),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.title = element_text(color="black", size=10, face="bold",hjust = 0.5),
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black")))
}
Created on 2023-01-16 with reprex v2.0.2
I am using ggpubr to combine multiple graphs in a single plot, but cannot seem to correctly generate one graph with the title that I would like. I would like the title to say "Customized legend," given that it is a common legend for both graphs. Does anybody know how I can do this?
Here is my data:
data1 = data.frame(var1 = c(1,
1,
1,
1,
2,
2,
2,
2,
3,
3,
3,
3,
4,
4,
4,
4,
5,
5,
5,
5,
6,
6,
6,
6,
7,
7,
7,
7,
8,
8,
8,
8,
9,
9,
9,
9,
10,
10,
10,
10,
11,
11,
11,
11,
12,
12,
12,
12,
13,
13,
13,
13,
14,
14,
14,
14,
15,
15,
15,
15,
16,
16,
16,
16,
17,
17,
17,
17,
18,
18,
18,
18,
19,
19,
19,
19,
20,
20,
20,
20,
21,
21,
21,
21,
22,
22,
22,
22,
23,
23,
23,
23,
24,
24,
24,
24,
25,
25,
25,
25,
26,
26,
26,
26,
27,
27,
27,
27,
28,
28,
28,
28,
29,
29,
29,
29,
30,
30,
30,
30,
31,
31,
31,
31,
32,
32,
32,
32,
33,
33,
33,
33),
var2 = c(1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4),
var3 = c(113,
89,
99,
41,
72,
64,
39,
139,
135,
17,
3,
135,
63,
126,
34,
87,
84,
125,
123,
18,
115,
11,
68,
85,
48,
95,
56,
129,
41,
78,
82,
122,
124,
4,
60,
132,
67,
128,
46,
79,
110,
88,
19,
88,
88,
126,
30,
11,
52,
66,
15,
52,
6,
74,
14,
101,
88,
70,
58,
20,
104,
76,
134,
23,
40,
1,
47,
25,
49,
110,
96,
100,
106,
26,
93,
19,
87,
41,
13,
40,
63,
87,
137,
105,
89,
95,
24,
49,
112,
92,
45,
105,
112,
105,
114,
129,
84,
33,
95,
95,
15,
90,
1,
62,
20,
7,
18,
96,
4,
71,
42,
94,
45,
102,
55,
98,
124,
80,
76,
97,
41,
31,
25,
21,
135,
138,
121,
93,
17,
13,
49,
26))
data2 <- data.frame(var1a = c(1,
1,
1,
1,
2,
2,
2,
2,
3,
3,
3,
3,
4,
4,
4,
4,
5,
5,
5,
5,
6,
6,
6,
6,
7,
7,
7,
7,
8,
8,
8,
8,
9,
9,
9,
9,
10,
10,
10,
10,
11,
11,
11,
11,
12,
12,
12,
12,
13,
13,
13,
13,
14,
14,
14,
14,
15,
15,
15,
15,
16,
16,
16,
16,
17,
17,
17,
17,
18,
18,
18,
18,
19,
19,
19,
19,
20,
20,
20,
20,
21,
21,
21,
21,
22,
22,
22,
22,
23,
23,
23,
23,
24,
24,
24,
24,
25,
25,
25,
25,
26,
26,
26,
26,
27,
27,
27,
27,
28,
28,
28,
28,
29,
29,
29,
29,
30,
30,
30,
30,
31,
31,
31,
31,
32,
32,
32,
32,
33,
33,
33,
33),
var2a = c(1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4),
var3a = c(113,
89,
99,
41,
72,
64,
39,
139,
135,
17,
3,
135,
63,
126,
34,
87,
84,
125,
123,
18,
115,
11,
68,
85,
48,
95,
56,
129,
41,
78,
82,
122,
124,
4,
60,
132,
67,
128,
46,
79,
110,
88,
19,
88,
88,
126,
30,
11,
52,
66,
15,
52,
6,
74,
14,
101,
88,
70,
58,
20,
104,
76,
134,
23,
40,
1,
47,
25,
49,
110,
96,
100,
106,
26,
93,
19,
87,
41,
13,
40,
63,
87,
137,
105,
89,
95,
24,
49,
112,
92,
45,
105,
112,
105,
114,
129,
84,
33,
95,
95,
15,
90,
1,
62,
20,
7,
18,
96,
4,
71,
42,
94,
45,
102,
55,
98,
124,
80,
76,
97,
41,
31,
25,
21,
135,
138,
121,
93,
17,
13,
49,
26))
Here is the code that I am using:
#Open packages
library(ggplot2)
library(ggpubr)
#Set the theme
theme_set(theme_pubr())
#Change necessary columns to factor
data1$var2 <- factor(data1$var2, levels = c(1,2,3,4))
data2$var2a <- factor(data2$var2a, levels = c(1,2,3,4))
#Generate the plots
#Generate plots
plot1 <- ggplot(data1, aes(x = var1, y = var3, group = var2)) +
geom_line(size = 1.5, aes(linetype = var2, color = var2)) +
xlab('x_label') +
ylab('y_label')+
scale_fill_discrete(name = 'customized legend')
plot2 <- ggplot(data2, aes(x = var1a, y = var3a, group = var2a)) +
geom_line(size = 1.5, aes(linetype = var2a, color = var2a)) +
xlab('x_label') +
ylab('y_label')+
scale_fill_discrete(name = 'customized legend')
#Combine both into one picture
fig <- ggarrange(plot1, plot2,
ncol = 2,
nrow = 1,
common.legend = TRUE,
legend = "bottom")
fig
Since you didn't use the fill aesthetic in your ggplot, you should not use scale_fill_discrete. What you need is to set the legend title of linetype and color to "customized legend", since those are the aesthetics that you used.
library(ggplot2)
library(ggpubr)
plot1 <- ggplot(data1, aes(x = var1, y = var3, group = var2)) +
geom_line(size = 1.5, aes(linetype = var2, color = var2)) +
xlab('x_label') +
ylab('y_label') +
labs(linetype = "customized legend", color = "customized legend")
plot2 <- ggplot(data2, aes(x = var1a, y = var3a, group = var2a)) +
geom_line(size = 1.5, aes(linetype = var2a, color = var2a)) +
xlab('x_label') +
ylab('y_label') +
labs(linetype = "customized legend", color = "customized legend")
#Combine both into one picture
ggarrange(plot1, plot2,
ncol = 2,
nrow = 1,
common.legend = TRUE,
legend = "bottom")
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" )
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
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