Related
I am really new to use R. So I am having a problem to visualize data using ggplot2 package in R.
I would like to create a linear regression graph in which the points within the specific area have the same color and the points outside that area have the same color. Also, I would like to change the background within the specific area to focus on that area as well.
The graph I would like to make will be similar like the below graph.
Target graph
But until now, I only could create the below simple graph.
My current graph
My code to generate the current graph is below.
g <- ggplot(df, aes(x = real, y = predicted))
g + geom_point() +
geom_abline(intercept = 0, slope = 1, color='black') +
theme_classic() +
geom_abline(intercept = 0+s_est, slope = 1, color = 'darkgrey')+
geom_abline(intercept = 0-s_est, slope = 1, color = 'darkgrey') +
ggtitle("Test Set")
The first 100 lines of data are as follows.
structure(list(real = c(3.33, 5.92, 5.3, 6, 6.96, 7.03, 6.6,
7.92, 8.3, 10.52, 6.34, 4.38, 4.59, 9.8, 10.3, 10, 8.25, 6, 7.44,
6.66, 9.09, 9.22, 9.7, 4.82, 6.1, 4.92, 4.29, 3.22, 6.01, 9.05,
9.04, 4.85, 8.22, 6.7, 6.7, 4.62, 4.82, 8.52, 5.24, 8.15, 7,
10, 7, 5.18, 5.93, 8.4, 7.7, 7.24, 9.54, 6.06, 8, 4.35, 4.2,
4.51, 2.48, 9.1, 5.34, 4.19, 8.05, 8.55, 6.55, 11.4, 10.96, 9.64,
4.49, 6, 6.9, 6.17, 9, 6.92, 3.77, 4.22, 8.92, 7.55, 7.6, 6.82,
5.32, 8.39, 5.09, 10.96, 6.68, 9.4, 5.04, 5.59, 9.21, 9.7, 6.98,
6.17, 8.89, 9.74, 6.08, 6.7, 4.41, 3.57, 7.12, 6.09, 6.11, 6.82,
7.3, 6.77), predicted = c(3.3049898147583, 7.57794666290283,
5.81329345703125, 3.71067190170288, 6.35026741027832, 6.59200620651245,
6.32752990722656, 7.13449430465698, 7.78791570663452, 8.61589622497559,
7.72269868850708, 5.33322525024414, 7.26069974899292, 9.23727989196777,
8.27904891967773, 7.55226612091064, 5.94742393493652, 4.07633399963379,
7.67468595504761, 5.64575576782227, 7.85368394851685, 7.73117685317993,
10.2843132019043, 4.96891403198242, 6.29262351989746, 6.03091764450073,
6.71697568893433, 3.50744342803955, 6.46608829498291, 8.20327758789062,
7.52885150909424, 4.58155632019043, 6.1530909538269, 6.49482202529907,
5.28225088119507, 4.44094896316528, 5.503089427948, 7.79408073425293,
5.6220269203186, 7.12402009963989, 6.30716276168823, 7.15596580505371,
7.26271867752075, 5.41359615325928, 5.68268489837646, 6.81329536437988,
7.10254955291748, 8.64251136779785, 8.65674114227295, 5.94885206222534,
9.24687099456787, 5.93400239944458, 5.66134691238403, 6.14793062210083,
2.94440221786499, 9.21078777313232, 5.96825170516968, 4.69157028198242,
7.91313886642456, 6.90836668014526, 6.72082805633545, 9.95611953735352,
9.15732383728027, 6.68948268890381, 3.60811305046082, 7.42742109298706,
6.05647945404053, 6.2350025177002, 8.12950134277344, 7.56590843200684,
5.3975772857666, 3.48417925834656, 7.63604927062988, 8.04048824310303,
7.78053188323975, 7.34217929840088, 7.93345308303833, 8.03125,
5.62498426437378, 4.80621385574341, 5.19631958007812, 7.51661252975464,
5.43919944763184, 5.5195426940918, 6.10152912139893, 8.25357818603516,
5.73111486434937, 7.27180528640747, 8.37008285522461, 7.78157567977905,
7.52273559570312, 4.32158374786377, 6.20211696624756, 4.30103015899658,
7.89811611175537, 6.88143062591553, 6.74230575561523, 6.75651741027832,
6.64747190475464, 6.72232007980347)), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -100L))
s_est = 4.536
Thank you so much for any help.
In your target image it looks like the points are colored by a measure of the absolute error, where points which fall inside the confidence (?) band are colored blue and points which fall outside are colored red. To achieve the same result you could map the absolute error (or whatever measure you prefer) on the color aesthetic. To get the coloring right I use a scale_color_gradient2 where I have set the midpoint to s_est. However, I set an upper bound for the color gradient, i.e. values with an abs error greater or equal to 2 * s_est are assigned the same "red" color. But you you could adjust that if you like.
To get a shading for the area between your ablines I first get rid of your geom_ablines and use a geom_ribbon instead. One drawback is that the ribbon will not extend to the axis but is restricted to the data range. To "fix" that I use a small hack, i.e. I use a separate dataset where I extend the range of real values slightly by 5% of the data range and additionally get rid of the default expansion of the x-scale.
Finally I added a coord_equal to equalize the range or the limits of both scales.
Note: I used a smaller value for s_est as for the example data no value would have fallen outside of the confidence band.
library(ggplot2)
s_est <- 4.536 / 4
# Absolute Error
df$resid <- abs(df$predicted - df$real)
# Range of "real" values used for the ribbon. Manually expand range by 5%
range_ribbon <- diff(range(df$real))
range_ribbon <- range(df$real) + .05 * range_ribbon * c(-1, 1)
ggplot(df, aes(x = real, y = predicted)) +
geom_point(aes(color = resid)) +
geom_abline(intercept = 0, slope = 1, color = "black") +
geom_ribbon(
data = data.frame(real = range_ribbon, predicted = 0),
aes(ymin = real - s_est, ymax = real + s_est),
color = "darkgrey", fill = "darkgrey", alpha = .2
) +
# Remove default expansion of the x scale
scale_x_continuous(expand = c(0, 0)) +
# Color gradient. Limit range to 2 * s_est
scale_color_gradient2(
midpoint = s_est, low = "blue", high = "red",
limits = c(0, 2 * s_est),
oob = scales::oob_squish
) +
labs(title = "Test Set") +
coord_equal()
I am trying to add text onto each bar, can someone please help me show how I can do? I am not a R user so please excuse my abilities. I appreciate your help. I got few responses earlier, like to use annotate or geomtext but i am not sure how to run them.
Here is my code:
library(viridis)
library(hrbrthemes)
library(tidyr)
library(dplyr)
library(tibble)
library(ggplot2)
df <- data.frame(
H1 = c(6.36, 3.03, 6.85, 4.07, 4.69, 6.27, 6.67, 3.11, 5.07, 6.14, 5.93, 6.49),
H2 = c(5.15, 5.00, 5.71, 5.50, 4.99, 5.81, 6.05, 5.76, 5.28, 5.69, 5.69, 5.06),
H3 = c(3.85, 5.13, 4.99, 4.91, 5.01, 5.73, 5.77, 5.94, 5.57, 5.35, 6.00, 4.39),
H4 = c(3.84, 4.80, 5.15, 4.85, 4.99, 5.73, 5.77, 5.45, 5.44, 5.41, 5.81, 4.46),
H5 = c(4.08, 5.17, 4.77, 5.03, 5.00, 5.49, 5.49, 5.80, 5.51, 5.18, 5.76, 4.60),
H6 = c(4.35, 5.59, 5.59, 4.83, 5.52, 5.63, 5.85, 5.74, 5.66, 5.19, 5.79, 4.84), fontface = c("bold"),
names = c("RB", "Ver", "Atl", "POR12PG28-3",
"Valery", "Rio", "CO99076-6R", "Purple",
"AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela"),
specie = c(rep("Appearance", 12), rep("Aroma" , 12), rep("Flavor" , 12),
rep("Overall" , 12), rep("Aftertaste", 12), rep("Texture", 12)),
condition = rep(c("RB", "Ver", "Atl", "POR12PG28-3",
"Valery", "Rio", "CO99076-6R", "Purple",
"AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela") , 6))
df <- df %>%
pivot_longer(starts_with("H"), names_to = "h.names")
df
#one condition per plot
nameframe <- enframe(unique(df$h.names))
specieframe <- enframe(unique(df$specie))
names.labs <- c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")
names(names.labs) <- c("H1", "H2", "H3", "H4", "H5", "H6")
ggplot() +
geom_col(data = df, mapping = aes(x = names, y = value),
position = "dodge") +
coord_flip() +
ylim(c(0,9)) +
scale_y_continuous(breaks=seq(0.0, 9, 3), limits=c(0, 9), labels = c("0", "3", "6", "Like\nExtremely")) +
labs(y = "", x = "") + theme(legend.title = element_blank(), axis.text.y = element_text(face = "bold", size = 11),
axis.text.x = element_text(face = "bold", size = 9)) +
scale_fill_discrete(breaks = c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")) +
facet_wrap(~h.names, labeller = labeller(h.names = names.labs))
#add text onto each bar
p <- p + annotate("text", label = "Test", size = 4, x = 15, y = 5)
print(p)
text(x = H,
y = y,
labels = c("ab", "e", "a", "d", "cd", "ab", "ab", "e", "c", "ab", "b", "ab"),
pos = 2)
Here is how you would add the text you had in your original question to each bar. Please note that I moved data = df, mapping = aes(x = names, y = value up into the ggplot() aesthetic where it will be applied to each layer in the plot. Next, used case_when from the dplyr package to add the bar plot labels as a new column to df. After that you pass in the new column into geom_text like so geom_text(aes(label = bar_labels, hjust = 0)) to apply the labels on the tip of each bar.
library(viridis)
library(tidyr)
library(dplyr)
library(tibble)
library(ggplot2)
df <- data.frame(
H1 = c(6.36, 3.03, 6.85, 4.07, 4.69, 6.27, 6.67, 3.11, 5.07, 6.14, 5.93, 6.49),
H2 = c(5.15, 5.00, 5.71, 5.50, 4.99, 5.81, 6.05, 5.76, 5.28, 5.69, 5.69, 5.06),
H3 = c(3.85, 5.13, 4.99, 4.91, 5.01, 5.73, 5.77, 5.94, 5.57, 5.35, 6.00, 4.39),
H4 = c(3.84, 4.80, 5.15, 4.85, 4.99, 5.73, 5.77, 5.45, 5.44, 5.41, 5.81, 4.46),
H5 = c(4.08, 5.17, 4.77, 5.03, 5.00, 5.49, 5.49, 5.80, 5.51, 5.18, 5.76, 4.60),
H6 = c(4.35, 5.59, 5.59, 4.83, 5.52, 5.63, 5.85, 5.74, 5.66, 5.19, 5.79, 4.84), fontface = c("bold"),
names = c("RB", "Ver", "Atl", "POR12PG28-3",
"Valery", "Rio", "CO99076-6R", "Purple",
"AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela"),
specie = c(rep("Appearance", 12), rep("Aroma" , 12), rep("Flavor" , 12),
rep("Overall" , 12), rep("Aftertaste", 12), rep("Texture", 12)),
condition = rep(c("RB", "Ver", "Atl", "POR12PG28-3",
"Valery", "Rio", "CO99076-6R", "Purple",
"AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela") , 6))
df <- df %>%
pivot_longer(starts_with("H"), names_to = "h.names")
#one condition per plot
nameframe <- enframe(unique(df$h.names))
specieframe <- enframe(unique(df$specie))
names.labs <- c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")
names(names.labs) <- c("H1", "H2", "H3", "H4", "H5", "H6")
#add text onto each bar
df <- df %>%
arrange(desc(names)) %>%
group_by(names) %>%
mutate(
bar_labels = case_when(
names == "Ver" ~ "ab",
names == "Valery" ~ "e",
names == "Rio" ~ "a",
names == "RB" ~ "d",
names == "Purple" ~ "cd",
names == "POR12PG28-3" ~ "ab",
names == "Masquerade" ~ "ab",
names == "CO99076-6R" ~ "e",
names == "CO05068-1RU" ~ "c",
names == "Canela" ~ "ab",
names == "Atl" ~ "b",
names == "AC99330-1P/Y" ~ "ab",
TRUE ~ as.character(NA)
))
ggplot(data = df, mapping = aes(x = names, y = value)) +
geom_col(position = "dodge") +
coord_flip() +
ylim(c(0,9)) +
scale_y_continuous(breaks=seq(0.0, 9, 3), limits=c(0, 9), labels = c("0", "3", "6", "Like\nExtremely")) +
labs(y = "", x = "") + theme(legend.title = element_blank(), axis.text.y = element_text(face = "bold", size = 11),
axis.text.x = element_text(face = "bold", size = 9)) +
scale_fill_discrete(breaks = c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")) +
facet_wrap(~h.names, labeller = labeller(h.names = names.labs)) +
geom_text(aes(label = bar_labels, hjust = 0))
Created on 2021-03-10 by the reprex package (v0.3.0)
I'd like to show the average for my dataset and add a tick mark on the Y-axis corresponding to this mean value - highlighted in red in the below image:
Code
plt <- ggplot(dat, aes(x = time, y = value)) +
geom_point(aes(fill = value), size = 2, alpha = 0.8, shape = 21, stroke = 0.5, color = 'black') +
scale_color_gradientn(colors = RColorBrewer::brewer.pal(4,name = 'OrRd')[-1], aesthetics = 'fill') +
geom_hline(yintercept = dat[, mean(value, na.rm = T)], color = 'black', linetype = '11', size = 1.25) +
guides(fill = F)
I can use scale_y_continuous() to add a specific break point but it messes up the grid lines and I don't know how to customize that specific tick mark (if at all possible):
plt <- plt +
scale_y_continuous(breaks = round(c(seq(from = 0, to = dat[, max(value)], by = 10), dat[, mean(value)]), digits = 1) )
Data
Reduced dataset for reproducing the plot:
structure(list(time = structure(c(1607990400, 1607996400, 1608002400,
1608008400, 1608014400, 1608020400, 1608026400, 1608032400, 1608038400,
1608044400, 1608050400, 1608056400, 1608062400, 1608068400, 1608074400,
1608080400, 1608086400, 1608092400, 1608098400, 1608104400, 1608110400,
1608116400, 1608122400, 1608128400, 1608134400, 1608140400, 1608146400,
1608152400, 1608158400, 1608164400, 1608170400, 1608176400, 1608182400,
1608188400, 1608194400, 1608200400, 1608206400, 1608212400, 1608218400,
1608224400, 1608230400, 1608236400, 1608242400, 1608248400, 1608254400,
1608260400, 1608266400, 1608272400, 1608278400, 1608284400, 1608290400,
1608296400, 1608302400, 1608308400, 1608314400, 1608320400, 1608326400,
1608332400, 1608338400, 1608344400, 1608350400, 1608356400, 1608362400,
1608368400, 1608374400, 1608380400, 1608386400, 1608392400, 1608398400,
1608404400, 1608410400, 1608416400, 1608422400, 1608428400, 1608434400,
1608440400, 1608446400, 1608452400, 1608458400, 1608464400, 1608470400,
1608476400, 1608482400, 1608488400, 1608494400, 1608500400, 1608506400,
1608512400, 1608518400, 1608524400, 1608530400, 1608536400, 1608542400,
1608548400, 1608554400, 1608560400, 1608566400, 1608572400, 1608578400,
1608584400, 1608590400, 1608596400, 1608602400, 1608608400, 1608614400,
1608620400, 1608626400, 1608632400, 1608638400), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), value = c(3.87, 3.57, 4.12, 2.68,
4.85447552447552, 0, 9.85, 2.9, 0.65010183299389, 2.55242704955998,
2.94610169491525, 3.2225, 3.44, 3.2, 3.64666666666667, 3.6, 4.2236312849162,
3.56285714285714, 2.99, 2.54, 2.34, 2.245, 2.05, 2.23666666666667,
4.82, 13.81, 18.08, 4.0375, 3.96, 12.9723756906077, 23.87, 16.2053333333333,
13.0836077705828, 10.91, 5.36238095238095, 2.62, 2.5375, 2.38,
2.72, 2.345, 2.32909090909091, 3.90333333333333, 3.02166666666667,
3.94833333333333, 3.83636363636364, 4.04117647058824, 4.22139146567718,
5.57, 4.82, 3.59666666666667, 3.73873949579832, 2, 2.04, 2.57,
3.00042016806723, 3.905, 5.65, 4.271589958159, 5.28, 7.15639534883721,
5.45, 5.24295336787565, 3.11224489795918, 4.79, 2.6106976744186,
2.25, 2.08264705882353, 2.25, 2.58666666666667, 3.18682008368201,
3.24, 3.10375, 3.35833333333333, 4.39333333333333, 3.765, 7.71,
5.16117647058824, 4.95588235294118, 2.44, 2.34666666666667, 2.345,
2.375, 2.4275, 3.0975, 3.21666666666667, 4.13, 4.44663366336634,
3.60877551020408, 3.83265033407572, 3.8625, 4.2675, 6.765, 2.688,
2.43101242521859, 2.43561435803037, 2.30166666666667, 2.69, 3.18,
5.04, 4.345, 4.86529411764706, 8.57, 6.2, 6.0032, 3.82, 5.03,
7.02, 3.69716216216216, 3.00468438538206)), row.names = c(NA,
-109L), class = c("data.table", "data.frame"))
Quick, dirty, and hacky:
plt + geom_text(aes(x = dat[, min(time, na.rm = T)], y = dat[, mean(value, na.rm = T)], label = round(dat[, mean(value, na.rm = T)],1)), color = 'red', hjust = 2) + coord_cartesian(clip = 'off')
Maybe it gets you somewhere.
I made a barplot with error bars and labels written on the bars.
My problem is: I want the labels to appear on the bars and also next to the error bars. That is, I don't want labels and error bars to overlap.
An example with my code:
i <- data.frame(
nbr =c(15.18 ,11.53 ,13.37 ,9.2, 10.9, 12.23 ,9.53, 9.81, 7.86, 12.79,
22.03 ,17.64 ,18.1, 16.78 ,17.53 ,16.97 ,17.76 ,18.35 ,12.82 ,20.91,
22.09 ,19.18 ,17.54 ,18.45 ,19.83 ,16.99 ,19.69 ,19.45 ,13.07 ,21.41,
12.13 ,9.76, 10.79 ,10.74 ,12.43 ,9.65, 12.18 ,11.63 ,6.74, 12.31,
17.5, 14.75 ,15.2, 13.89 ,15.24 ,17.43 ,15.22 ,14.04,9.49, 15.86,
8.09, 5.86, 6.68, 7.34, 8.01, 6.35, 8.4, 7.4, 3.88, 6.92 ),
SD = c(4.46, 4.19, 2.27, 2.19, 5.10, 7.25, 8.42, 6.47, 6.04, 7.48, 6.38, 6.05, 3.58, 3.85,
6.94, 6.87, 6.32, 4.28, 4.10, 7.34, 7.46, 6.62, 4.28, 5.24, 8.00, 8.10, 7.73, 5.18,
5.53, 7.96, 7.46, 7.05, 4.47, 4.73, 8.15, 6.95, 5.88, 3.20, 4.01, 7.34, 7.24, 6.98,
5.98, 4.53, 4.22, 7.21, 4.02, 4.30, 1.96, 2.11, 4.98, 7.16, 8.45, 6.39, 6.20, 7.03,
6.10, 6.42, 3.77, 3.53),
x2=rep(c("a", "b", "c", "d", "e", "f", "g",
"h", "i", "j"),6),
s = c(rep(c(rep(c("3"),10),
rep(c("4"),10),
rep(c("5"),10),
rep(c("6"),10),
rep(c("7"),10),
rep(c("8"),10)),1)))
ii <- i[order(i$s, i$nbr ), ]
sn <- factor(x = 1:60, labels = ii$x2)
ii$sn <- sn
scale_x_reordered <- function(..., sep = "___") {
reg <- paste0(sep, ".+$")
ggplot2::scale_x_discrete(labels = function(x) gsub(reg, "", x), ...)
}
reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
new_x <- paste(x, within, sep = sep)
stats::reorder(new_x, by, FUN = fun)
}
dummy2 <- data.frame(s = levels(i$s)[-1], Z = c( 4,16,16,8,4))
dummy2$s <- factor(dummy2$s)
ggplot(ii, aes(reorder_within(sn, nbr, s), nbr,
label =x2)) +
geom_bar(stat = 'identity') +
geom_text(aes(y = 0,fontface=2), angle = 90, hjust = -.05, size = 4)+
scale_x_reordered() +
facet_wrap(.~ s, scales = "free_x", ncol=2)+
#geom_text(aes(label=nbr), vjust=1.6, color="white", size=3.5)+
theme(axis.text.x = element_blank(),
axis.title=element_text(size=16),
axis.text=element_text(face = "bold"),
strip.text.x = element_text(size = 14,face="bold")
)+ geom_errorbar(aes(reorder_within(sn, nbr, s),ymin=nbr-SD, ymax=nbr+SD), width=.2, position=position_dodge(.9))
Example of expected parcel:
I want all the labels to be written next to the error bars on the bars.
Thanks for your help !
I found this solution and wanted to share it with you:
geom_text(aes(y = 0,fontface=2), angle = 90, vjust = -1, hjust = -.05, size = 4)
Following data:
df <- data.frame(cbind("Group_ID" = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4), "WBHO" = runif(20, 1.0, 7.0), "SI" = runif(20, 1.0, 7.0), "OORT" = c(2.34, 4.64, NA, 5.32, 3.23, 6.01, 5.43, 4.78, 3.98, 3.80, 4.45, NA, NA, 3.18, 4.87, NA, NA, 5.73, 3.52, 4.89), "LMX" = runif(20, 1.0, 7.0),"RL" = runif(20, 1.0, 7.0),"AL" = c(1.54, NA, 1.08, 6.77, NA, NA, 4.56, NA, 5.34, 4.32, 2.45, 3.86, 6.21, 2.89, 7.32, 6.43, NA, 4.56, 3.89, 6.16),"SL" = runif(20, 1.0, 7.0),"RV" = runif(20, 1.0, 7.0),"PT" = runif(20, 1.0, 7.0),"SD" = runif(20, 1.0, 7.0), "HT" = runif(20, 1.0, 7.0), "RTL" = c(2.45, NA, 6.04, 2.88, 3.49, 2.30, NA, 5.32, 2.39, NA, 3.62, 3.22, 4.87, 2.91, 5.41, NA, NA, 4.78, 6.20, NA), "INB" = runif(20, 1.0, 7.0), "ETB" = runif(20, 1.0, 7.0)))
Now, I want to create a raster, 2D-Grid or Heatmap which gives a nice overview of all the variables for each group ("Group_ID") using the mean (the x-axis showing the groups and the y-axis all the variables), giving a particular field green colour for value 1 to 3, yellow for 3 to 5 and green for 5 to 7. I have the following Code to create a df that combines the variables in one column and has the values and Group-belonging in the other two:
library(dplyr)
library(tidyr)
df %>%
gather(key = "variable", value = "value", - Group_ID) -> df_new
This does not work, however, as there are NAs included. However, I want to keep those rows with NAs. Is there a way with which I can do this in the same step?
Then, I would like to create the raster concerning which I have been given the following code which I am not fully sure how to apply in this case:
library(raster)
r <- raster(ncol=nrow(df_new), nrow=15, xmn=0, xmx=4, ymn=0, ymx=15)
values(r) <- as.vector(as.matrix(df$WBHO, df$SI, df$OORT, df$LMX, df$RL, df$AL, df$SL, df$RV, df$PT, df$SD, df$HT, df$RTL,
df$INB, df$ETB)
plot(r, axes=F, box=F, asp=NA)
axis(1, at=seq(), 0:9)
axis(2, at=seq(), c("", colnames(df_new)), las=1)
Thanks for any help!
We can use the dplyr and tidyr to calculate the mean. After that, we can use the cut function to categorize the values. We can then use the geom_tile from the ggplot2 to plot a heatmap. Specify x to be the variable, y is Group_ID (converted to be factor), and fill to be based on value2. No raster package is required.
It is not clear why do you want two groups (1-3, 5-7), both being green. My example assign red to the group 5-7, but you can make changes easily based on your needs.
library(dplyr)
library(tidyr)
df_new <- df %>%
gather(key = "variable", value = "value", - Group_ID) %>%
group_by(Group_ID, variable) %>%
summarise(value = mean(value, na.rm = TRUE)) %>%
mutate(value2 = cut(value, breaks = c(1, 3, 5, 7), labels = c("Low", "Medium", "High"))) %>%
ungroup()
library(ggplot2)
ggplot(df_new, aes(x = variable, y = factor(Group_ID), fill = value2)) +
geom_tile() +
scale_fill_manual(values = c("Low" = "Green", "Medium" = "Yellow", "High" = "Red")) +
labs(
y = "Group_ID"
)