Nadine M.
Nadine M.

Reputation: 107

Programming joint legend in ggplot2 for multiple regressions with grouping variable

I have four regression plots which are not only based on a x and y variable but also grouped by a grouping variable z (v_187_corr). So, the legend contains all the groups which are stored in v_187. I now want to have all my 4 graphs in a 2x2 window and place one common legend with all the group names.

(I found some examples which were however still quite different to my example).

par(mfrow = c(2, 2))


# First Plot: Passung auf Zufriedenheit
p1 <- ggplot(df.hlm_cc_select, aes(zPssg_sd, zZufri))

p1 + geom_jitter(aes(colour = v_187_corr))+
  labs(title="Zufriedenheit erklärt durch Direktheit",x="Direktheit", y = "Zufriedenheit") +   
    scale_color_viridis(discrete = TRUE, option = "A")+
    scale_fill_viridis(discrete = TRUE) +
    theme_dark() +
    theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'),
         axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid')) +
    #geom_smooth(aes(x = zagree_group_withinmeet_levelrat, y = zZufri), inherit.aes = FALSE, se = FALSE)+
    geom_smooth(method=lm, color="black")



# Second Plot: Homogenität Problemart auf Zufriedenheit

p2 <- ggplot(df.hlm_cc_select, aes(zmean.aggr.prb_sd, zZufri))

p2 + geom_jitter(aes(colour = v_187_corr))+
  labs(title="Zufriedenheit erklärt durch Homogenität (Problemart)",x="Homogenität (Problemart)", y = "Zufriedenheit") +   
    scale_color_viridis(discrete = TRUE, option = "A")+
    scale_fill_viridis(discrete = TRUE) +
    theme_dark() +
    theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'),
         axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid')) +
    #geom_smooth(aes(x = zagree_group_withinmeet_levelrat, y = zZufri), inherit.aes = FALSE, se = FALSE)+
    geom_smooth(method=lm, color="black")


# Third Plot: Homogenität Level auf Zufriedenheit
p3 <- ggplot(df.hlm_cc_select, aes(zagree_group_withinmeet_levelrat, zZufri))

p3 + geom_jitter(aes(colour = v_187_corr))+
  labs(title="Satisfaction explained by Homogeneity (Level)",x="Homogeneity (Level)", y = "Satisfaction") +   
    scale_color_viridis(discrete = TRUE, option = "A")+
    scale_fill_viridis(discrete = TRUE) +
    theme_dark() +
    theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'),
         axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid')) +
    #geom_smooth(aes(x = zagree_group_withinmeet_levelrat, y = zZufri), inherit.aes = FALSE, se = FALSE)+
    geom_smooth(method=lm, color="black")


# Forth Plot: Intensität auf Zufriedenheit
p4 <- ggplot(df.hlm_cc_select, aes(zmeans_levelsums, zZufri))

p4 + geom_jitter(aes(colour = v_187_corr))+
  labs(title="Zufriedenheit erklärt durch Intensität",x="Intensität", y = "Zufriedenheit") +   
    scale_color_viridis(discrete = TRUE, option = "A")+
    scale_fill_viridis(discrete = TRUE) +
    theme_dark() +
    theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'),
         axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid')) +
    #geom_smooth(aes(x = zagree_group_withinmeet_levelrat, y = zZufri), inherit.aes = FALSE, se = FALSE)+
    geom_smooth(method=lm, color="black")

Upvotes: 0

Views: 131

Answers (1)

JMilner
JMilner

Reputation: 531

Assuming your data looks something like the following:

df.hlm_cc_select <- data.frame(zZufri = runif(10,0,1),
                               zmeans_levelsums = runif(10,0,1),
                               zagree_group_withinmeet_levelrat = runif(10,0,1),
                               zmean.aggr.prb_sd = runif(10,0,1),
                               zPssg_sd = runif(10,0,1),
                               v_187_corr = runif(10,0,1))

You can use the tidyr package (among other options such as reshape2::melt and data.table::melt) to gather all variables into one column (we will call this value).

In this process, you must exclude zZufri (your x axis?) and your colour variable v_187_corr using -zZufri, -v_187_corr. The key column indicates which variable is associated with each value.

df <- gather(df.hlm_cc_select,key="key",value="value",-zZufri, -v_187_corr)

Then using ggplot you can just add facet_wrap(~key) which says create a facet plot one for each unique value in key

ggplot(df,aes(x=value,y=zZufri,col=v_187_corr))+
  geom_jitter()+
  scale_color_viridis(discrete = TRUE, option = "A")+
  scale_fill_viridis(discrete = TRUE) +
  theme_dark() +
  theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'),
        axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid')) +
  inherit.aes = FALSE, se = FALSE)+
  geom_smooth(method=lm, color="black")+
  facet_wrap(~key)

There will only be one legend for all plots.

Upvotes: 1

Related Questions