I have a code with which I represent a series of data using the function facet_wrap
. Here I attach a minimal example with some fictitious data. The representation is correct, however, given the large number of levels that in the real data I include in the cb variable, I would like to try some kind of 3D representation.
My goal is the following:
Obtain, for each level of
cb
andx
, the difference between the means ofy
between the conditionsz
. That is, in pseudocode, the differencemean(y) de L - mean(y) de M
,mean(y) de L - mean(y) E
andmean(y) de M - mean(y) E
, grouped bycb
andx
. We would therefore obtain 3 new data columns (LM, LE, ME).Now that you would have new variables named
L-M
,L-E
andM-E
(representing the mean differences ofy
), you would like to plot each of them on a 3D graph. One axis would bex
, anothercb
and anotherdif_y
. The three graphs would form a composition with facet_wrap.
My attempts so far have been through trying to generate a new csv file with the new columns, and then trying to operate within a new pipeline. But the process is cumbersome and I'm wondering if there isn't an easy way to do this directly from the original data I share.
For point 2 I was trying to use the library rayshader
, but if another approach is recommended I'm all ears.
Thanks for the feedback and help in advance.
library(dplyr)
library(ggplot2)
library(scales)
tribble( ~y, ~cb, ~z, ~x,
1 , 0, "E" ,1,
2 , 1, "E" ,1,
1.2 , 0, "E" ,1,
3 , 1, "E" ,1,
1.5 , 0, "L" ,1,
2 , 1, "L" ,1,
1 , 0, "L" ,1,
2 , 1, "L" ,1,
2 , 0, "M" ,1,
3 , 1, "M" ,1,
1.5 , 0, "M" ,1,
2 , 1, "M" ,1,
2 , 0, "E" ,2,
2 , 1, "E" ,2,
3 , 0, "E" ,2,
3 , 1, "E" ,2,
0.5 , 0, "M" ,2,
2 , 1, "M" ,2,
2 , 0, "M" ,2,
2 , 1, "M" ,2,
3 , 0, "L" ,2,
1 , 0, "L" ,2,
2 , 0, "L" ,2,
1 , 1, "L" ,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)
For the first part of your question I understand that what you want is a quick way to get the mean differences of the z parameters
Here we finish the first part of the question, the next thing is to make the 3D graph that you mention and for this you will use the rayshader library
Then this graph saved in grafica_2d using the function of the library plot_gg , we generate the graph in 3D