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 wrote this code to transpose the Data frame, the odd part of this is this code works quite a lot of times and at times it doesn't work, which is baffling.
Book5<- read_excel("C:/X/X/X/X/1.1 Croston_Aplha-0.1.xlsx")
X1 <- transpose(Book5,keep.names="rn")
The error I get for this is:
Error in transpose(Book5, keep.names = "rn") :
unused argument (keep.names = "rn")
Dput for the data frame is :
structure(list(`Row Labels` = c("2019-01-01", "2019-02-01", "2019-03-01",
"2019-04-01", "2019-05-01", "2019-06-01", "2019-07-01", "2019-08-01",
"2019-09-01", "2019-10-01", "2019-11-01", "2019-12-01", "2020-01-01",
"2020-02-01", "2020-03-01", "2020-04-01", "2020-05-01", "2020-06-01",
"2020-07-01", "2020-08-01", "2020-09-01", "2020-10-01", "2020-11-01",
"2020-12-01", "2021-01-01", "2021-02-01", "2021-03-01", "2021-04-01",
"2021-05-01", "2021-06-01", "2021-07-01", "2021-08-01", "2021-09-01",
"2021-10-01", "2021-11-01", "2021-12-01", "2022-01-01", "2022-02-01",
"2022-03-01", "2022-04-01", "2022-05-01", "2022-06-01", "2022-07-01",
"2022-08-01", "2022-09-01", "2022-10-01"), `XYZ|146` = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 18, 16, 16, 17, 12, 22, 6,
7, 6, 0, 15, 0, 17, 17, 5, 19, 16, 7, 25, 19, 34, 26, 41, 50,
29, 42, 20, 14, 16, 27, 10, 28, 21), `XYZ|666` = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 45,
9, 21, 33, 3, 8, 11, 16, 3, 17, 14, 59, 26, 35, 26, 15, 7, 4,
4, 2, 7, 6, 2), `XYZ|574` = c(0, 0, 0, 0, 0, 0, 0, 0, 74, 179,
464, 880, 324, 184, 90, 170, 140, 96, 78, 83, 83, 121, 245, 740,
332, 123, 117, 138, 20, 42, 70, 70, 42, 103, 490, 641, 488, 245,
142, 95, 63, 343, 57, 113, 100, 105), `XYZ|851` = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 206, 1814, 2324, 772, 1116, 1636, 1906, 957,
829, 911, 786, 938, 1313, 2384, 1554, 1777, 1635, 1534, 1015,
827, 982, 685, 767, 511, 239, 1850, 1301, 426, 261, 201, 33,
0, 0, 0, 0, 0, 0)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-46L))
Can someone tell me why is this acting like this? The expected output is given below:
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 try SOM map analyze with package Kohonen. I used this tutorial : https://www.shanelynn.ie/self-organising-maps-for-customer-segmentation-using-r/ . This is my code :
require(kohonen)
data = matrix(
c(6, 6, 80, 280, 404, 0, 158, 158197, 158197233,
6, 13, 80, 280, 404, 0, 158, 158197, 158197233,
6, 13, 80, 283, 404, 0, 158, 158197, 158197233,
6, 35, 80, 321, 301, 0, 158, 158197, 158197233,
6, 35, 80, 3131, 200, 0, 158, 158197, 158197233,
6, 35, 80, 20073, 200, 0, 158, 158197, 158197233,
6, 35, 80, 183, 200, 0, 158, 158197, 158197233,
6, 35, 80, 25, 302, 0, 158, 158197, 158197233,
6, 35, 80, 13744, 200, 0, 158, 158197, 158197233,
6, 35, 80, 280, 404, 0, 158, 158197, 158197233,
9, 33, 80, 2859, 200, 0, 158, 158197, 15819736,
9, 33, 80, 463, 200, 0, 158, 158197, 15819736,
9, 33, 80, 2065, 200, 0, 158, 158197, 15819736,
9, 33, 80, 1298, 200, 0, 158, 158197, 15819736,
9, 33, 80, 86659, 200, 0, 158, 158197, 15819736,
9, 33, 80, 942, 200, 0, 158, 158197, 15819736,
9, 33, 80, 2027, 200, 0, 158, 158197, 15819736,
9, 33, 80, 2979, 200, 0, 158, 158197, 15819736,
9, 33, 80, 127390, 200, 0, 158, 158197, 15819736,
9, 33, 80, 1150, 200, 0, 158, 158197, 15819736,
9, 33, 80, 1150, 200, 0, 158, 158197, 15819736,
9, 33, 80, 942, 200, 0, 158, 158197, 15819736,
9, 40, 80, 287, 200, 0, 158, 158197, 15819736,
9, 40, 80, 90, 200, 0, 158, 158197, 15819736,
9, 40, 80, 2, 200, 0, 158, 158197, 15819736,
9, 40, 80, 3314, 200, 0, 158, 158197, 15819736,
9, 40, 80, 483, 200, 0, 158, 158197, 15819736,
9, 40, 80, 2, 200, 0, 158, 158197, 15819736,
9, 40, 80, 543, 200, 0, 158, 158197, 15819736,
9, 40, 80, 439, 200, 0, 158, 158197, 15819736,
9, 40, 80, 689, 200, 0, 158, 158197, 15819736,
9, 40, 80, 797, 200, 0, 158, 158197, 15819736,
9, 40, 80, 732, 200, 0, 158, 158197, 15819736,
9, 40, 80, 5403, 200, 0, 158, 158197, 15819736,
9, 40, 80, 496, 200, 0, 158, 158197, 15819736,
9, 40, 80, 743, 200, 0, 158, 158197, 15819736,
9, 40, 80, 3049, 200, 0, 158, 158197, 15819736,
9, 40, 80, 3064, 200, 0, 158, 158197, 15819736,
9, 40, 80, 3042, 200, 0, 158, 158197, 15819736,
9, 40, 80, 3077, 200, 0, 158, 158197, 15819736,
12, 8, 80,305 ,301 , 0, 142, 1424, 14245,
12, 8, 80, 10531,200 , 0, 142, 1424, 14245,
12, 8, 80, 445,400 , 0, 142, 1424, 14245,
12, 8, 80, 445,400 , 0, 142, 1424, 14245,
12, 8, 80, 445,400 , 0, 142, 1424, 14245,
12, 8, 80, 324,400 , 0, 142, 1424, 14245,
12, 8, 80, 445,400 , 0, 142, 1424, 14245,
0, 0, 80,0 ,0 , 0, 42, 424, 4245,
12, 25, 80, 171,200 , 0, 42, 424, 4245,
12, 25, 80, 2970,200 , 0,42, 424, 4245,
12, 25, 80, 171,200 , 0, 42, 424, 4245,
12, 25, 80, 2970,200 , 0, 42, 424, 4245,
12, 25, 80, 171,200 , 0, 42, 424, 4245,
12, 25, 80, 2970,200 , 0, 42, 424, 4245,
15, 32, 80, 2860,200 , 0, 88, 88212, 8821237,
15, 32, 80, 2859,200 , 0, 88, 88212, 8821237,
15, 32, 80, 86659,200 , 0, 88, 88212, 8821237,
15, 32, 80, 22495,200 , 0, 88, 88212, 8821237,
15, 32, 80, 949,200 , 0, 88, 88212, 8821237,
15, 32, 80, 1298,200 , 0, 88, 88212, 8821237,
15, 32, 80, 2027,200 , 0, 88, 88212, 8821237,
15, 32, 80, 2979,200 , 0, 88, 88212, 8821237,
15, 32, 80, 103646,200 , 0, 88, 88212, 8821237,
15, 32, 80, 406,200 , 0, 88, 88212, 8821237,
9, 34, 80, 1169,200 , 0, 88, 88212, 8821237,
9, 34, 80, 212,200 , 0, 88, 88212, 8821237,
9, 34, 80, 2250,200 , 0, 88, 88212, 8821237,
9, 34, 80, 8496,200 , 0, 88, 88212, 8821237,
9, 34, 80, 0,200 , 0, 88, 88212, 8821237,
9, 34, 80, 946,200 , 0, 88, 88212, 8821237,
9, 34, 80, 716263,200 , 0, 88, 88212, 8821237,
9, 34, 80, 63,200 , 0, 88, 88212, 8821237,
9, 34, 80, 63,200 , 0, 88, 88212, 8821237,
9, 34, 80, 1573,200 , 0, 88, 88212, 8821237,
9, 34, 80, 63,200 , 0, 88, 88212, 8821237,
9, 34, 80, 974,200 , 0, 88, 88212, 8821237,
9, 34, 80, 63,200 , 0, 88, 88212, 8821237,
9, 34, 80, 926,200 , 0, 88, 88212, 8821237,
9, 34, 80, 1150,200 , 0, 88, 88212, 8821237,
9, 34, 80, 800,200 , 0, 88, 88212, 8821237,
9, 34, 80, 225,200 , 0, 88, 88212, 8821237,
9, 34, 80, 79,200 , 0, 88, 88212, 8821237,
9, 34, 80, 408,200 , 0, 88, 88212, 8821237,
9, 34, 80, 535,200 , 0, 88, 88212, 8821237,
3, 21, 80, 208,404 , 0, 207, 20746, 2074613,
3, 49, 80, 1298,200 , 0, 207, 20746, 2074613,
5, 17, 80, 302,230 , 0, 207, 20746, 2074613,
8, 28, 80, 2857,200 , 0, 5, 5188, 518810,
13, 08, 80, 2860,200 , 0, 5, 5188, 518810,
14, 08, 80, 2860,200 , 0, 5, 5188, 518810,
15, 58, 80, 208,404 , 0, 66, 66249, 6624966,
15, 58, 80, 463,200 , 0, 66, 66249, 6624966,
15, 58, 80, 2065,200 , 0, 66, 66249, 6624966,
15, 58, 80, 2065,200 , 0, 66, 66249, 6624966,
13, 05, 80, 608,200 , 0, 88, 88212, 8821240,
13, 08, 80, 608,200 , 0, 88, 88212, 8821240,
13, 11, 80, 608,200 , 0, 88, 88212, 8821240,
13, 14, 80, 608,200 , 0, 88, 88212, 8821240,
13, 17, 80, 608,200 , 0, 88, 88212, 8821240,
13, 20, 80, 608,200 , 0, 88, 88212, 8821240 ),
nrow=100,
ncol=9,
byrow = TRUE)
data_train <- data[, c(1,2,4,5,7,8,9)]
#data_train <- data[, c(2,4,5,8)]
data_train_matrix <- as.matrix(scale(data_train))
som_grid <- somgrid(xdim = 3, ydim=4, topo="hexagonal")
som_model <- som(data_train_matrix,
grid=som_grid,
rlen=500,
alpha=c(0.05,0.01),
keep.data = TRUE )
#training proces
plot(som_model, type="changes")
#nodes
plot(som_model, type="count", main="Node Counts")
#distance
#plot(som_model, type="dist.neighbours", main = "SOM neighbour distances")
#codes and weight vectors
#plot(som_model, type="codes")
#heatmap
plot(som_model, type = "property", property = getCodes(som_model)[,4], main="Heat map - parameter 4")
And this is my map visualization :
Map is OK. My question is: is there some way how to find which data was in white node ? I look at getCodes(som_model) but there is just map numbers. So I known that white node is V4. But what data was in node V4 ? I look into all som_model values, but it dont help. Any ideas ?
> getCodes(som_model)[,4]
V1 V2 V3 V4 V5 V6 V7 V8
-0.727734454 -0.183272487 -0.342681905 2.361366190 -0.343764866 -0.343764866 1.298987948 -0.343532184
V9 V10 V11 V12
-0.343764307 1.350552793 -0.003492471 -0.343764866
I want result for example like this :
> inV4
[1] 2 25 0
Is possible do this in Kohonen package?
UPDATE :
It look it is not possible do it easy in Kohonen pack. So I try this :
# find which node is white
q <- getCodes(som_model)[,4]
for (i in 1:length(q)){
if(q[i]>2){
t<- q[i]
}
}
# find name od node
node <- names(t)
#remove "V" letter from node name
mynode <- gsub("V","",node)
#find which node has which input ???
mydata2 <- som_model$unit.classif
print (myadat2)
#choose just imputs which go to right node
result <- vector('list',length(mydata2))
for (i in 1:length(mydata2)){
result <- som_model$unit.classif== mynode
}
#remove FALSE results
result2 <- which(result == TRUE)
#write all input line
for (i in 1:length(result2)){
print (data[result2[i],])
}
But I am not sure if this is right way. And I am not sure if this give me right result inputs. Is there any way how to check it ?
Sorry if this is too late to help you. It may help someone else.
Because you did not set the random seed, we cannot exactly reproduce your result. When I ran your code, I got the plot
and I got the codes:
getCodes(som_model)[,4]
V1 V2 V3 V4 V5 V6 V7
-0.3437649 -0.3437649 2.3146262 0.4037323 -0.3437649 -0.6034393 -0.3434484
V8 V9 V10 V11 V12
0.6730257 0.2089917 -0.3437649 -0.1038754 2.6302823
For me, the white node is V12. You can get the data points that are in V12 by looking at the unit classifications that are part of the som object.
som_model$unit.classif
[1] 12 12 12 9 9 9 9 9 9 12 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
[26] 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 3 2 3 3 3 3 3 6 6 6
[51] 6 6 6 6 1 1 1 1 1 1 1 1 1 1 5 5 5 5 5 5 7 5 5 5 5
[76] 5 5 5 5 5 5 5 5 5 8 8 8 6 6 6 4 4 4 4 2 2 2 2 2 2
To get which points are in V12, you can simply use:
which(som_model$unit.classif == 12)
[1] 1 2 3 10
Iam working on a triangle mesh for which i need to color each vertext with a specific color. I followed the example provided here
Using texture for triangle mesh without having to read/write an image file
which helped a lot, but i am stuck with tecturing the vertices, the example above describes coloring each vertex by creating a palette from numColors, I tried the same for my set of specific colors array i have, the palette is created but the vertices are not colored in the order that i wanted to, Any example on this would really help, how to color each vertex in a triangle mesh with a provided specific set of colors (for each vertex) by cerating a color palette.
Thanks
Here is my example
import java.io.File;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.input.ScrollEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.DrawMode;
import javafx.scene.shape.MeshView;
import javafx.scene.shape.TriangleMesh;
import javafx.scene.shape.VertexFormat;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
public class Sample1 extends Application {
private static final double MODEL_SCALE_FACTOR = 10;
private static final double MODEL_X_OFFSET = 0; // standard
private static final double MODEL_Y_OFFSET = 0; // standard
private static int VIEWPORT_SIZE = 800;
private double mouseOldX, mouseOldY = 0;
private Rotate rotateX = new Rotate(0, Rotate.X_AXIS);
private Rotate rotateY = new Rotate(0, Rotate.Y_AXIS);
private Rotate rotateZ = new Rotate(0, Rotate.Z_AXIS);
private Group root = new Group();
//Xform sceneRoot;
PerspectiveCamera camera;
private TriangleMesh mesh;
// Color[] colorArray;
#Override
public void start(Stage primaryStage) {
camera = new PerspectiveCamera(false);
camera.setTranslateX(0);
camera.setTranslateY(0);
camera.setTranslateZ(0);
camera.setNearClip(0.1);
camera.setFarClip(1000.0);
camera.getTransforms().addAll(rotateX, rotateY, new Translate(0, 0, 0));
root.setRotationAxis(Rotate.Y_AXIS);
root.setRotate(200);
rotateX.setPivotX(VIEWPORT_SIZE / 2 + MODEL_X_OFFSET);
rotateX.setPivotY(VIEWPORT_SIZE / 2 + MODEL_Y_OFFSET);
rotateX.setPivotZ(VIEWPORT_SIZE / 2);
rotateY.setPivotX(VIEWPORT_SIZE / 2 + MODEL_X_OFFSET);
rotateY.setPivotY(VIEWPORT_SIZE / 2 + MODEL_Y_OFFSET);
rotateY.setPivotZ(VIEWPORT_SIZE / 2);
rotateZ.setPivotX(VIEWPORT_SIZE / 2 + MODEL_X_OFFSET);
rotateZ.setPivotY(VIEWPORT_SIZE / 2 + MODEL_Y_OFFSET);
rotateZ.setPivotZ(VIEWPORT_SIZE / 2);
root.setScaleX(MODEL_SCALE_FACTOR);
root.setScaleY(MODEL_SCALE_FACTOR);
root.setScaleZ(MODEL_SCALE_FACTOR);
createModel();
Scene scene = new Scene(root, 800, 500);
scene.setFill(Color.rgb(10, 10, 40));
scene.setCamera(camera);
scene.setOnScroll(new EventHandler<ScrollEvent>() {
#Override
public void handle(ScrollEvent event) {
double zoomFactor = 1.05;
double deltaY = event.getDeltaY();
if (deltaY < 0) {
zoomFactor = 2.0 - zoomFactor;
}
// System.out.println(zoomFactor);
root.setScaleX(root.getScaleX() * zoomFactor);
root.setScaleY(root.getScaleY() * zoomFactor);
root.setScaleZ(root.getScaleZ() * zoomFactor);
event.consume();
}
});
scene.setOnMousePressed(event -> {
mouseOldX = event.getSceneX();
mouseOldY = event.getSceneY();
});
scene.setOnMouseDragged(event -> {
rotateX.setAngle(rotateX.getAngle() - (event.getSceneY() - mouseOldY));
rotateY.setAngle(rotateY.getAngle() + (event.getSceneX() - mouseOldX));
mouseOldX = event.getSceneX();
mouseOldY = event.getSceneY();
});
primaryStage.setTitle("Sample Mesh");
primaryStage.setScene(scene);
primaryStage.centerOnScreen();
primaryStage.show();
}
private void createModel() {
mesh = new TriangleMesh(VertexFormat.POINT_NORMAL_TEXCOORD);
addPoints();
addFaces();
addNormals();
PhongMaterial mat = new PhongMaterial();
mat.setDiffuseMap(colorPalette(colorArray));
int numVertices = mesh.getPoints().size() / 3;
int numColors = colorArray.length;
IntStream.range(0, numVertices).boxed()
.forEach(i -> mesh.getTexCoords()
.addAll(getTextureLocation(i * numColors / numVertices, numColors)));
MeshView meshView = new MeshView(mesh);
meshView.setMaterial(mat);
meshView.setDrawMode(DrawMode.FILL);
meshView.setTranslateX(VIEWPORT_SIZE / 2 + MODEL_X_OFFSET);
meshView.setTranslateY(VIEWPORT_SIZE / 2 + MODEL_Y_OFFSET);
meshView.setTranslateZ(VIEWPORT_SIZE / 2);
root.getChildren().addAll(meshView);
}
private void addPoints() {
mesh.getPoints().addAll(25.255093f, 0.86116815f, 0.9920882f,
28.010185f, 0.6422461f, 0.68806815f,
22.5f, 0.88371015f, 1.0604522f,
20.0f, 0.88371015f, 1.0604522f,
17.5f, 0.88371015f, 1.0604522f,
28.010185f, 0.9249993f, 0.0f,
28.010185f, 0.6422461f, -0.68806815f,
25.255093f, 1.2263886f, 0.0f,
25.255093f, 0.86116815f, -0.9920882f,
22.5f, 1.25f, 0.0f,
20.0f, 1.25f, 0.0f,
17.5f, 1.25f, 0.0f,
22.5f, 0.88371015f, -1.0604522f,
20.0f, 0.88371015f, -1.0604522f,
17.5f, 0.88371015f, -1.0604522f,
28.010185f, -1.4802974E-16f, 0.9898141f,
25.255093f, -7.401487E-17f, 1.4115738f,
25.255093f, -0.86116815f, 0.9920882f,
28.010185f, -0.6422461f, 0.68806815f,
22.5f, 0.0f, 1.5f,
20.0f, 0.0f, 1.5f,
17.5f, 0.0f, 1.5f,
22.5f, -0.88371015f, 1.0604522f,
20.0f, -0.88371015f, 1.0604522f,
17.5f, -0.88371015f, 1.0604522f,
30.0f, 0.0f, 0.0f,
28.010185f, -1.3158199E-16f, -0.9898141f,
25.255093f, -6.5790995E-17f, -1.4115738f,
28.010185f, -0.9249993f, 0.0f,
28.010185f, -0.6422461f, -0.68806815f,
25.255093f, -1.2263886f, 0.0f,
25.255093f, -0.86116815f, -0.9920882f,
22.5f, 0.0f, -1.5f,
20.0f, 0.0f, -1.5f,
17.5f, 0.0f, -1.5f,
22.5f, -1.25f, 0.0f,
20.0f, -1.25f, 0.0f,
17.5f, -1.25f, 0.0f,
22.5f, -0.88371015f, -1.0604522f,
20.0f, -0.88371015f, -1.0604522f,
17.5f, -0.88371015f, -1.0604522f,
15.0f, 0.88371015f, 1.0604522f,
12.5f, 0.88371015f, 1.0604522f,
10.0f, 0.88371015f, 1.0604522f,
7.5f, 0.88371015f, 1.0604522f,
4.744907f, 0.86116815f, 0.9920882f,
1.989814f, 0.6422461f, 0.68806815f,
15.0f, 1.25f, 0.0f,
12.5f, 1.25f, 0.0f,
10.0f, 1.25f, 0.0f,
15.0f, 0.88371015f, -1.0604522f,
12.5f, 0.88371015f, -1.0604522f,
10.0f, 0.88371015f, -1.0604522f,
7.5f, 1.25f, 0.0f,
4.744907f, 1.2263886f, 0.0f,
1.989814f, 0.9249993f, 0.0f,
1.989814f, 0.6422461f, -0.68806815f,
7.5f, 0.88371015f, -1.0604522f,
4.744907f, 0.86116815f, -0.9920882f,
15.0f, 0.0f, 1.5f,
12.5f, 0.0f, 1.5f,
10.0f, 0.0f, 1.5f,
15.0f, -0.88371015f, 1.0604522f,
12.5f, -0.88371015f, 1.0604522f,
10.0f, -0.88371015f, 1.0604522f,
7.5f, 0.0f, 1.5f,
4.744907f, -7.401487E-17f, 1.4115738f,
1.989814f, -1.4802974E-16f, 0.9898141f,
7.5f, -0.88371015f, 1.0604522f,
4.744907f, -0.86116815f, 0.9920882f,
1.989814f, -0.6422461f, 0.68806815f,
15.0f, 0.0f, -1.5f,
12.5f, 0.0f, -1.5f,
10.0f, 0.0f, -1.5f,
15.0f, -1.25f, 0.0f,
12.5f, -1.25f, 0.0f,
10.0f, -1.25f, 0.0f,
15.0f, -0.88371015f, -1.0604522f,
12.5f, -0.88371015f, -1.0604522f,
10.0f, -0.88371015f, -1.0604522f,
0.0f, -2.9605948E-16f, 0.0f,
7.5f, 0.0f, -1.5f,
4.744907f, -6.5790995E-17f, -1.4115738f,
1.989814f, -1.3158199E-16f, -0.9898141f,
7.5f, -1.25f, 0.0f,
4.744907f, -1.2263886f, 0.0f,
1.989814f, -0.9249993f, 0.0f,
1.989814f, -0.6422461f, -0.68806815f,
7.5f, -0.88371015f, -1.0604522f,
4.744907f, -0.86116815f, -0.9920882f);
}
private void addFaces() {
mesh.getFaces().addAll(80, 80, 80, 55, 55, 55, 56, 56, 56,
80, 80, 80, 56, 56, 56, 83, 83, 83,
83, 83, 83, 87, 87, 87, 80, 80, 80,
87, 87, 87, 86, 86, 86, 80, 80, 80,
80, 80, 80, 86, 86, 86, 70, 70, 70,
80, 80, 80, 70, 70, 70, 67, 67, 67,
67, 67, 67, 46, 46, 46, 80, 80, 80,
46, 46, 46, 55, 55, 55, 80, 80, 80,
55, 55, 55, 54, 54, 54, 58, 58, 58,
55, 55, 55, 58, 58, 58, 56, 56, 56,
56, 56, 56, 58, 58, 58, 82, 82, 82,
56, 56, 56, 82, 82, 82, 83, 83, 83,
83, 83, 83, 82, 82, 82, 87, 87, 87,
82, 82, 82, 89, 89, 89, 87, 87, 87,
87, 87, 87, 89, 89, 89, 86, 86, 86,
89, 89, 89, 85, 85, 85, 86, 86, 86,
86, 86, 86, 85, 85, 85, 69, 69, 69,
86, 86, 86, 69, 69, 69, 70, 70, 70,
70, 70, 70, 69, 69, 69, 66, 66, 66,
70, 70, 70, 66, 66, 66, 67, 67, 67,
67, 67, 67, 66, 66, 66, 46, 46, 46,
66, 66, 66, 45, 45, 45, 46, 46, 46,
46, 46, 46, 45, 45, 45, 55, 55, 55,
45, 45, 45, 54, 54, 54, 55, 55, 55,
54, 54, 54, 53, 53, 53, 57, 57, 57,
54, 54, 54, 57, 57, 57, 58, 58, 58,
58, 58, 58, 57, 57, 57, 81, 81, 81,
58, 58, 58, 81, 81, 81, 82, 82, 82,
82, 82, 82, 81, 81, 81, 89, 89, 89,
81, 81, 81, 88, 88, 88, 89, 89, 89,
89, 89, 89, 88, 88, 88, 85, 85, 85,
88, 88, 88, 84, 84, 84, 85, 85, 85,
85, 85, 85, 84, 84, 84, 68, 68, 68,
85, 85, 85, 68, 68, 68, 69, 69, 69,
69, 69, 69, 68, 68, 68, 65, 65, 65,
69, 69, 69, 65, 65, 65, 66, 66, 66,
66, 66, 66, 65, 65, 65, 45, 45, 45,
65, 65, 65, 44, 44, 44, 45, 45, 45,
45, 45, 45, 44, 44, 44, 54, 54, 54,
44, 44, 44, 53, 53, 53, 54, 54, 54,
53, 53, 53, 49, 49, 49, 52, 52, 52,
53, 53, 53, 52, 52, 52, 57, 57, 57,
57, 57, 57, 52, 52, 52, 73, 73, 73,
57, 57, 57, 73, 73, 73, 81, 81, 81,
81, 81, 81, 73, 73, 73, 88, 88, 88,
73, 73, 73, 79, 79, 79, 88, 88, 88,
88, 88, 88, 79, 79, 79, 84, 84, 84,
79, 79, 79, 76, 76, 76, 84, 84, 84,
84, 84, 84, 76, 76, 76, 64, 64, 64,
84, 84, 84, 64, 64, 64, 68, 68, 68,
68, 68, 68, 64, 64, 64, 61, 61, 61,
68, 68, 68, 61, 61, 61, 65, 65, 65,
65, 65, 65, 61, 61, 61, 44, 44, 44,
61, 61, 61, 43, 43, 43, 44, 44, 44,
44, 44, 44, 43, 43, 43, 53, 53, 53,
43, 43, 43, 49, 49, 49, 53, 53, 53,
49, 49, 49, 48, 48, 48, 51, 51, 51,
49, 49, 49, 51, 51, 51, 52, 52, 52,
52, 52, 52, 51, 51, 51, 72, 72, 72,
52, 52, 52, 72, 72, 72, 73, 73, 73,
73, 73, 73, 72, 72, 72, 79, 79, 79,
72, 72, 72, 78, 78, 78, 79, 79, 79,
79, 79, 79, 78, 78, 78, 76, 76, 76,
78, 78, 78, 75, 75, 75, 76, 76, 76,
76, 76, 76, 75, 75, 75, 63, 63, 63,
76, 76, 76, 63, 63, 63, 64, 64, 64,
64, 64, 64, 63, 63, 63, 60, 60, 60,
64, 64, 64, 60, 60, 60, 61, 61, 61,
61, 61, 61, 60, 60, 60, 43, 43, 43,
60, 60, 60, 42, 42, 42, 43, 43, 43,
43, 43, 43, 42, 42, 42, 49, 49, 49,
42, 42, 42, 48, 48, 48, 49, 49, 49,
48, 48, 48, 47, 47, 47, 50, 50, 50,
48, 48, 48, 50, 50, 50, 51, 51, 51,
51, 51, 51, 50, 50, 50, 71, 71, 71,
51, 51, 51, 71, 71, 71, 72, 72, 72,
72, 72, 72, 71, 71, 71, 78, 78, 78,
71, 71, 71, 77, 77, 77, 78, 78, 78,
78, 78, 78, 77, 77, 77, 75, 75, 75,
77, 77, 77, 74, 74, 74, 75, 75, 75,
75, 75, 75, 74, 74, 74, 62, 62, 62,
75, 75, 75, 62, 62, 62, 63, 63, 63,
63, 63, 63, 62, 62, 62, 59, 59, 59,
63, 63, 63, 59, 59, 59, 60, 60, 60,
60, 60, 60, 59, 59, 59, 42, 42, 42,
59, 59, 59, 41, 41, 41, 42, 42, 42,
42, 42, 42, 41, 41, 41, 48, 48, 48,
41, 41, 41, 47, 47, 47, 48, 48, 48,
47, 47, 47, 11, 11, 11, 14, 14, 14,
47, 47, 47, 14, 14, 14, 50, 50, 50,
50, 50, 50, 14, 14, 14, 34, 34, 34,
50, 50, 50, 34, 34, 34, 71, 71, 71,
71, 71, 71, 34, 34, 34, 77, 77, 77,
34, 34, 34, 40, 40, 40, 77, 77, 77,
77, 77, 77, 40, 40, 40, 74, 74, 74,
40, 40, 40, 37, 37, 37, 74, 74, 74,
74, 74, 74, 37, 37, 37, 24, 24, 24,
74, 74, 74, 24, 24, 24, 62, 62, 62,
62, 62, 62, 24, 24, 24, 21, 21, 21,
62, 62, 62, 21, 21, 21, 59, 59, 59,
59, 59, 59, 21, 21, 21, 41, 41, 41,
21, 21, 21, 4, 4, 4, 41, 41, 41, 41,
41, 41, 4, 4, 4, 47, 47, 47, 4, 4, 4,
11, 11, 11, 47, 47, 47, 11, 11, 11,
10, 10, 10, 13, 13, 13, 11, 11, 11,
13, 13, 13, 14, 14, 14, 14, 14, 14,
13, 13, 13, 33, 33, 33, 14, 14, 14,
33, 33, 33, 34, 34, 34, 34, 34, 34,
33, 33, 33, 40, 40, 40, 33, 33, 33,
39, 39, 39, 40, 40, 40, 40, 40, 40,
39, 39, 39, 37, 37, 37, 39, 39, 39,
36, 36, 36, 37, 37, 37, 37, 37, 37,
36, 36, 36, 23, 23, 23, 37, 37, 37,
23, 23, 23, 24, 24, 24, 24, 24, 24,
23, 23, 23, 20, 20, 20, 24, 24, 24,
20, 20, 20, 21, 21, 21, 21, 21, 21,
20, 20, 20, 4, 4, 4, 20, 20, 20,
3, 3, 3, 4, 4, 4, 4, 4, 4,
3, 3, 3, 11, 11, 11, 3, 3, 3,
10, 10, 10, 11, 11, 11, 10, 10, 10,
9, 9, 9, 12, 12, 12, 10, 10, 10,
12, 12, 12, 13, 13, 13, 13, 13, 13,
12, 12, 12, 32, 32, 32, 13, 13, 13,
32, 32, 32, 33, 33, 33, 33, 33, 33,
32, 32, 32, 39, 39, 39, 32, 32, 32,
38, 38, 38, 39, 39, 39, 39, 39, 39,
38, 38, 38, 36, 36, 36, 38, 38, 38,
35, 35, 35, 36, 36, 36, 36, 36, 36,
35, 35, 35, 22, 22, 22, 36, 36, 36,
22, 22, 22, 23, 23, 23, 23, 23, 23,
22, 22, 22, 19, 19, 19, 23, 23, 23,
19, 19, 19, 20, 20, 20, 20, 20, 20,
19, 19, 19, 3, 3, 3, 19, 19, 19,
2, 2, 2, 3, 3, 3, 3, 3, 3, 2, 2, 2,
10, 10, 10, 2, 2, 2, 9, 9, 9,
10, 10, 10, 9, 9, 9, 7, 7, 7,
8, 8, 8, 9, 9, 9, 8, 8, 8,
12, 12, 12, 12, 12, 12, 8, 8, 8,
27, 27, 27, 12, 12, 12, 27, 27, 27,
32, 32, 32, 32, 32, 32, 27, 27, 27,
38, 38, 38, 27, 27, 27, 31, 31, 31,
38, 38, 38, 38, 38, 38, 31, 31, 31,
35, 35, 35, 31, 31, 31, 30, 30, 30,
35, 35, 35, 35, 35, 35, 30, 30, 30,
17, 17, 17, 35, 35, 35, 17, 17, 17,
22, 22, 22, 22, 22, 22, 17, 17, 17,
16, 16, 16, 22, 22, 22, 16, 16, 16,
19, 19, 19, 19, 19, 19, 16, 16, 16,
2, 2, 2, 16, 16, 16, 0, 0, 0,
2, 2, 2, 2, 2, 2, 0, 0, 0,
9, 9, 9, 0, 0, 0, 7, 7, 7,
9, 9, 9, 7, 7, 7, 5, 5, 5,
6, 6, 6, 7, 7, 7, 6, 6, 6,
8, 8, 8, 8, 8, 8, 6, 6, 6,
26, 26, 26, 8, 8, 8,
26, 26, 26, 27, 27, 27, 27, 27, 27,
26, 26, 26, 31, 31, 31, 26, 26, 26,
29, 29, 29, 31, 31, 31, 31, 31, 31,
29, 29, 29, 30, 30, 30, 29, 29, 29,
28, 28, 28, 30, 30, 30, 30, 30, 30,
28, 28, 28, 18, 18, 18, 30, 30, 30,
18, 18, 18, 17, 17, 17, 17, 17, 17,
18, 18, 18, 15, 15, 15, 17, 17, 17,
15, 15, 15, 16, 16, 16, 16, 16, 16,
15, 15, 15, 0, 0, 0,
15, 15, 15, 1, 1, 1, 0, 0, 0,
0, 0, 0, 1, 1, 1, 7, 7, 7,
1, 1, 1, 5, 5, 5, 7, 7, 7,
5, 5, 5, 25, 25, 25, 6, 6, 6,
6, 6, 6, 25, 25, 25, 26, 26, 26,
26, 26, 26, 25, 25, 25, 29, 29, 29,
29, 29, 29, 25, 25, 25, 28, 28, 28,
28, 28, 28, 25, 25, 25, 18, 18, 18,
18, 18, 18, 25, 25, 25, 15, 15, 15,
15, 15, 15, 25, 25, 25, 1, 1, 1,
1, 1, 1, 25, 25, 25, 5, 5, 5);
}
private void addNormals() {
mesh.getNormals().addAll(0.07288012f, 0.768564f, 0.6356083f, 0.21982567f, 0.7893725f, 0.5732083f, 0.012779057f,
0.76441664f, 0.6445959f, 0.0f, 0.76822126f, 0.6401844f, 0.0f, 0.76822126f, 0.6401844f, 0.25024077f, 0.96818364f,
0.0f, 0.21982567f, 0.7893725f, -0.5732083f, 0.07571304f, 0.9971296f, 0.0f, 0.07288012f, 0.768564f, -0.6356083f,
0.0087126205f, 0.99996203f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.012779057f, 0.76441664f, -0.6445959f, 0.0f,
0.76822126f, -0.6401844f, 0.0f, 0.76822126f, -0.6401844f, 0.22647266f, 0.0f, 0.9740175f, 0.07080862f, 0.0f, 0.9974899f,
0.07288012f, -0.768564f, 0.6356083f, 0.21982567f, -0.7893725f, 0.5732083f, 0.011401603f, 0.0f, 0.999935f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, 1.0f, 0.012779057f, -0.76441664f, 0.6445959f, 0.0f, -0.76822126f, 0.6401844f, 0.0f, -0.76822126f, 0.6401844f,
1.0f, 0.0f, 0.0f, 0.22647266f, 0.0f, -0.9740175f, 0.07080862f, 0.0f, -0.9974899f, 0.25024077f, -0.96818364f, 0.0f, 0.21982567f,
-0.7893725f, -0.5732083f, 0.07571304f, -0.9971296f, 0.0f, 0.07288012f, -0.768564f, -0.6356083f, 0.011401603f, 0.0f, -0.999935f,
0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0087126205f, -0.99996203f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.012779057f, -0.76441664f,
-0.6445959f, 0.0f, -0.76822126f, -0.6401844f, 0.0f, -0.76822126f, -0.6401844f, 0.0f, 0.76822126f, 0.6401844f, 0.0f, 0.76822126f,
0.6401844f, 0.0f, 0.76822126f, 0.6401844f, -0.009348294f, 0.76747775f, 0.6410074f, -0.07317282f, 0.73539054f, 0.67368126f, -0.2320432f,
0.6365708f, 0.73548186f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.76822126f, -0.6401844f, 0.0f, 0.76822126f, -0.6401844f,
0.0f, 0.76822126f, -0.6401844f, -0.0030446206f, 0.99999535f, 0.0f, -0.044230007f, 0.99902135f, 0.0f, -0.19177821f, 0.9814383f, 0.0f,
-0.2320432f, 0.6365708f, -0.73548186f, -0.009348294f, 0.76747775f, -0.6410074f, -0.07317282f, 0.73539054f, -0.67368126f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, -0.76822126f, 0.6401844f, 0.0f, -0.76822126f, 0.6401844f, 0.0f, -0.76822126f, 0.6401844f,
-0.020870605f, 0.0f, 0.9997822f, -0.1069364f, 0.0f, 0.99426585f, -0.2851421f, 0.0f, 0.95848525f, -0.009348294f, -0.76747775f, 0.6410074f,
-0.07317282f, -0.73539054f, 0.67368126f, -0.2320432f, -0.6365708f, 0.73548186f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f,
0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -0.76822126f, -0.6401844f, 0.0f, -0.76822126f, -0.6401844f, 0.0f, -0.76822126f,
-0.6401844f, -1.0f, 0.0f, -4.363512E-17f, -0.020870605f, 0.0f, -0.9997822f, -0.1069364f, 0.0f, -0.99426585f, -0.2851421f, 0.0f,
-0.95848525f, -0.0030446206f, -0.99999535f, 0.0f, -0.044230007f, -0.99902135f, 0.0f, -0.19177821f, -0.9814383f, 0.0f, -0.2320432f,
-0.6365708f, -0.73548186f, -0.009348294f, -0.76747775f, -0.6410074f, -0.07317282f, -0.73539054f, -0.67368126f);
}
Color[] colorArray = {Color.rgb(0, 45, 255, 1.0),
Color.rgb(0, 81, 255, 1.0),
Color.rgb(0, 194, 255, 1.0),
Color.rgb(0, 255, 101, 1.0),
Color.rgb(0, 255, 80, 1.0),
Color.rgb(0, 104, 255, 1.0),
Color.rgb(0, 95, 255, 1.0),
Color.rgb(0, 53, 255, 1.0),
Color.rgb(0, 59, 255, 1.0),
Color.rgb(0, 137, 255, 1.0),
Color.rgb(0, 255, 120, 1.0),
Color.rgb(0, 255, 79, 1.0),
Color.rgb(0, 203, 255, 1.0),
Color.rgb(0, 255, 100, 1.0),
Color.rgb(0, 255, 79, 1.0),
Color.rgb(0, 51, 255, 1.0),
Color.rgb(0, 21, 255, 1.0),
Color.rgb(0, 0, 255, 1.0),
Color.rgb(0, 38, 255, 1.0),
Color.rgb(0, 241, 255, 1.0),
Color.rgb(0, 255, 83, 1.0),
Color.rgb(0, 255, 80, 1.0),
Color.rgb(0, 167, 255, 1.0),
Color.rgb(0, 255, 104, 1.0),
Color.rgb(0, 255, 80, 1.0),
Color.rgb(0, 148, 255, 1.0),
Color.rgb(0, 65, 255, 1.0),
Color.rgb(0, 35, 255, 1.0),
Color.rgb(0, 61, 255, 1.0),
Color.rgb(0, 52, 255, 1.0),
Color.rgb(0, 7, 255, 1.0),
Color.rgb(0, 15, 255, 1.0),
Color.rgb(0, 248, 255, 1.0),
Color.rgb(0, 255, 83, 1.0),
Color.rgb(0, 255, 80, 1.0),
Color.rgb(0, 101, 255, 1.0),
Color.rgb(0, 255, 126, 1.0),
Color.rgb(0, 255, 80, 1.0),
Color.rgb(0, 176, 255, 1.0),
Color.rgb(0, 255, 103, 1.0),
Color.rgb(0, 255, 80, 1.0),
Color.rgb(0, 255, 117, 1.0),
Color.rgb(0, 255, 152, 1.0),
Color.rgb(0, 255, 100, 1.0),
Color.rgb(255, 246, 0, 1.0),
Color.rgb(255, 28, 0, 1.0),
Color.rgb(255, 52, 0, 1.0),
Color.rgb(0, 255, 104, 1.0),
Color.rgb(0, 255, 152, 1.0),
Color.rgb(0, 255, 143, 1.0),
Color.rgb(0, 255, 117, 1.0),
Color.rgb(0, 255, 152, 1.0),
Color.rgb(0, 255, 102, 1.0),
Color.rgb(155, 255, 0, 1.0),
Color.rgb(255, 36, 0, 1.0),
Color.rgb(255, 55, 0, 1.0),
Color.rgb(255, 65, 0, 1.0),
Color.rgb(255, 255, 0, 1.0),
Color.rgb(255, 42, 0, 1.0),
Color.rgb(0, 255, 128, 1.0),
Color.rgb(0, 255, 151, 1.0),
Color.rgb(0, 255, 59, 1.0),
Color.rgb(0, 255, 117, 1.0),
Color.rgb(0, 255, 151, 1.0),
Color.rgb(0, 255, 95, 1.0),
Color.rgb(255, 134, 0, 1.0),
Color.rgb(255, 5, 0, 1.0),
Color.rgb(255, 39, 0, 1.0),
Color.rgb(255, 216, 0, 1.0),
Color.rgb(255, 0, 0, 1.0),
Color.rgb(255, 11, 0, 1.0),
Color.rgb(0, 255, 129, 1.0),
Color.rgb(0, 255, 152, 1.0),
Color.rgb(0, 255, 62, 1.0),
Color.rgb(0, 255, 105, 1.0),
Color.rgb(0, 255, 151, 1.0),
Color.rgb(0, 255, 141, 1.0),
Color.rgb(0, 255, 117, 1.0),
Color.rgb(0, 255, 151, 1.0),
Color.rgb(0, 255, 97, 1.0),
Color.rgb(255, 93, 0, 1.0),
Color.rgb(255, 146, 0, 1.0),
Color.rgb(255, 19, 0, 1.0),
Color.rgb(255, 53, 0, 1.0),
Color.rgb(178, 255, 0, 1.0),
Color.rgb(255, 0, 0, 1.0),
Color.rgb(255, 12, 0, 1.0),
Color.rgb(255, 25, 0, 1.0),
Color.rgb(255, 226, 0, 1.0),
Color.rgb(255, 0, 0, 1.0),};
private Image colorPalette(Color[] colors) {
int numColors = colors.length;
int width = (int) Math.sqrt(numColors);
int height = numColors / width;
WritableImage img = new WritableImage(width, height);
PixelWriter pw = img.getPixelWriter();
//float[] colors = buffer.array();
AtomicInteger count = new AtomicInteger();
IntStream.range(0, height).boxed()
.forEach(y -> IntStream.range(0, width).boxed()
.forEach(x -> pw.setColor(x, y, getColor(count))));
// save for testing purposes
try {
ImageIO.write(SwingFXUtils.fromFXImage(img, null), "png", new File("palette" + ".png"));
} catch (IOException ex) {
}
return img;
}
private Color getColor(AtomicInteger count) {
return colorArray[count.getAndIncrement()];
}
private float[] getTextureLocation(int iPoint, int numColors) {
int width = (int) Math.sqrt(numColors);
int height = numColors / width;
int y = iPoint / width;
int x = iPoint - width * y;
float[] textureArray = new float[]{(((float) x) / ((float) width)), (((float) y) / ((float) height))};
return textureArray;
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
This is my output:
and this is the expected output:
As pointed out in the question you mentioned, you need:
A texture image, basically a small png where each pixel has a color that will be later looked up.
You have created one, but it seems the colors are randomly distributed:
A way to map the vertices of the mesh to a pixel in the image.
The faces in your mesh define the indices of the points, normals and textures.
I.e., your face 0 is 80, 80, 80, 55, 55, 55, 56, 56, 56, and that means that your texture indices are 80, 55, 56.
According to your mapping (getTextureLocation), these indices have coordinates:
55 [0.11111111, 0.6]
56 [0.22222222, 0.6]
80 [0.8888889, 0.8]
which are the colors:
As you can see that these vertices are using exactly that color:
With this random coloring, for each pair (x, y) within the face 0, the texture is interpolated between those values.
You can verify this by enabling PickResult on mouse pressed:
scene.setOnMousePressed(event -> {
PickResult pickResult = event.getPickResult();
if (pickResult != null) {
System.out.println("face: " + pickResult.getIntersectedFace());
System.out.println("point: " + pickResult.getIntersectedPoint());
System.out.println("text: " + pickResult.getIntersectedTexCoord());
}
}
For instance, for a point in-between 55 and 80, it gives [x = 0.51, y = 0.71], which is expected: {(0.9 + 0.1)/2, (0.6+0.8)/2)}. In other words, it will go from color(55) to color(80), using all the colors in that range:
Obviously, this is not what you want, but it is doing what you have told it to do.
The trick here is to have a texture with a linear gradient of colors, so when the interpolation is done, the differences are small.
For instance, this:
private Color getColor(AtomicInteger count, int numColors) {
int iColor = count.getAndIncrement();
java.awt.Color c = java.awt.Color.getHSBColor((float) iColor / (float) numColors, 1.0f, 1.0f);
return Color.rgb(c.getRed(), c.getGreen(), c.getBlue());
}
will give you this image:
and this result:
Which is closer to what you want, but not there yet.
The final trick is using a mapping between your texture indices, which are based on the vertices, based on some mathematical function.
Using a function f(x, y, z) based on the vertex coordinates, it should give you a value between the minimum and maximum colors. This color has an index, and that index is the one that you should use for the texture.
As a quick use case here, I'll use f(x, y, z) = x, based on your desired result.
Since in your case, x goes from 0 to 30, you can tweak the texture location mapping easily:
float[] points = new float[mesh.getPoints().size()];
mesh.getPoints().toArray(points);
IntStream.range(0, numVertices).boxed()
.forEach(i -> {
double x = points[3 * i];
int fact = (int) (x / 30d * numVertices);
mesh.getTexCoords().addAll(getTextureLocation(fact, numColors));
});
with this result, based in the gradient I defined:
Now it is up to you to generate the proper texture image and texture mapping.
References
For further references, have a look at FXyz3D library. A bunch of primitive 3D shapes are created using a TexturedMesh class that allows texturing a mesh with colors, 3D or 1D mapping, and face or pattern coloring.
Without an example it is difficult to tell what is going wrong. But I think you are on the wrong track anyway. The question is what do you expect to happen when the resulting triangles of the triangle mesh are filled. Just think about how many colored areas you would have to create and manage in your texture if you allow arbitrary combinations of color triples for the vertices of one triangle.