I have a code with which I graph data similar to the minimal example that I share.
As can be seen, the variable z contains 4 conditions: "HC R", "IC R", "HC W" and "IC W".
Currently, when graphing, each condition appears a different color based on the "Spectral" color palette.
Goal
I would like to get the following:
That the "HC" condition line appear in the orange color that the "Spectral" palette uses.
Have the "I C" condition line appear in the dark green color that the "Spectral" palette uses.
That the "R" condition line appear continuous.
That the line of the condition "W" appeared dashed.
For organizational reasons, it would be preferable to separate two groups of legend titles in the legend: Condition for "H C" and "I C" and Model for "R" and "W".
My attempts so far have been through trying to do z-level spacing and then using linetype to change the line style. But I'm having trouble because the code operates data inside a pipe.
I appreciate suggestions.
library(dplyr)
library(ggplot2)
library(scales)
tribble( ~y, ~cb, ~z, ~x,
1 , 0, "H C R" ,1,
2 , 1, "H C R" ,1,
2 , 0, "I C R" ,1,
3 , 1, "I C R" ,1,
1.5 , 0, "H C W" ,1,
2 , 1, "H C W" ,1,
2 , 0, "I C W" ,1,
2 , 1, "I C W" ,1,
3 , 0, "H C R" ,2,
3 , 1, "H C R" ,2,
0.5 , 0, "I C R" ,2,
2 , 1, "I C R" ,2,
2 , 0, "H C W" ,2,
2 , 1, "H C W" ,2,
1 , 0, "I C W" ,2,
1 , 1, "I C W" ,2)-> datos
datos %>%
group_by(x, z, cb) %>%
summarise(media = mean(y),
desvio = sd(y),
error_est = desvio / sqrt(n()),
intervalo_sup = media + (2*error_est),
intervalo_inf = media - (2*error_est)) %>%
ggplot(aes(x = x, y = media, color = z)) +
geom_line(aes(group = z), size=0.5) +
geom_errorbar(aes(ymax = intervalo_sup,
ymin = intervalo_inf),
width=0.3) +
labs(x = "x", y = "y", color = "Condition") +
scale_color_brewer(type='div', palette="Spectral") +
scale_x_continuous(breaks = seq(1,7, by=1)) +
theme(legend.position="bottom", legend.text=element_text(size=12)) +
guides(color=guide_legend(ncol=2)) +
theme(axis.text=element_text(size=14),
axis.title=element_text(size=14))+
facet_wrap(~cb)
In the following approach, the data is saved before making the graph, filters are used within the
geom
to give different styles to the different levels ofz
and finally manual colors are added withlibrary(RColorBrewer)
andscale_color_manual
.