0

I created with ggplot an interaction plot and added with a different dataframe outliers into the same plot. I want to change the legend's labels (yes and no), but a new legend is added instead of changing them. Here is the Code:

the theme I'm using:

theme_apa(
  legend.pos = "right",
  legend.use.title = FALSE,
  legend.font.size = 12,
  x.font.size = 12,
  y.font.size = 12,
  facet.title.size = 12,
  remove.y.gridlines = TRUE,
  remove.x.gridlines = TRUE
) 

the plot:

InteractionWithOutliers <- ggplot() + 
  geom_line(data=data2, aes(x=Messzeitpunkt, 
                            y = Sum_PCLMean,group = TB2,linetype=TB2),) + 
  scale_color_manual(labels = c("test", "test"),values=c('#000000','#000000'))+
  geom_point(data = outliersDF, aes(Messzeitpunkt,Sum_PCL,
                                    shape=TB2, color=TB2, size=TB2),) +
  geom_point(data = data2, aes(Messzeitpunkt,Sum_PCLMean,         
                               shape=TB2, color=TB2, size=TB2), ) +
  scale_shape_manual(values=c(15, 17))+
  scale_size_manual(values=c(2,2)) +
  ylim(0, 60) +
  scale_x_continuous(breaks = seq(0,2)) +
  geom_errorbar(data=data2,aes(x = Messzeitpunkt,ymin=Sum_PCLMean-Sum_PCLSD, ymax=Sum_PCLMean+Sum_PCLSD), width=.2,)

InteractionWithOutliers + theme_apa() +
  labs(x ="Measurement Period", y = "PTSS mean scores")

Image of the Graph:

enter image description here

Furthermore, when i try to use position dodge to split the position of the interaction plot and the outliers, not everything moves the same way.

Code:

InteractionWithOutliers <- ggplot() + 
  geom_line(data=data2, aes(x=Messzeitpunkt, 
                            y = Sum_PCLMean,group = TB2,linetype=TB2),position = position_dodge(width = 0.4)) + 
  scale_color_manual(labels = c("test", "test"),values=c('#000000','#000000'))+
  geom_point(data = outliersDF, aes(Messzeitpunkt,Sum_PCL,
                                    shape=TB2, color=TB2, size=TB2),position = position_dodge(width = 0.4)) +
  geom_point(data = data2, aes(Messzeitpunkt,Sum_PCLMean,         
                               shape=TB2, color=TB2, size=TB2),position = position_dodge(width = 0.4) ) +
  scale_shape_manual(values=c(15, 17))+
  scale_size_manual(values=c(2,2)) +
  ylim(0, 60) +
  scale_x_continuous(breaks = seq(0,2)) +
  geom_errorbar(data=data2,aes(x = Messzeitpunkt,ymin=Sum_PCLMean-Sum_PCLSD, ymax=Sum_PCLMean+Sum_PCLSD), 
                width=.2,position = position_dodge(width = 0.4))


InteractionWithOutliers + theme_apa() +
  labs(x ="Measurement Period", y = "PTSS mean scores")

enter image description here

Thank you for your help!

Edit: Data for the Outliers:

Messzeitpunkt   Sum_PCL TB2
0               38      no
0               37      yes
0               40      yes
0               41      yes
0               38      yes
1               56      no
1               33      no
2               39      no
2               33      no

Data for the interaction plots:

Messzeitpunkt   Sum_PCLMean TB2 Sum_PCLSD
0               9           no  11
0               12          yes 11
1               9           no  15
1               18          yes 16
2               8           no  12
2               14          yes 12
Timon Doo
  • 143
  • 12
  • It would be easier to help you if you provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Jul 14 '22 at 11:35
  • This said: The issue is that you changed the labels only for the color legend. Legends only get merged if you use the same labels, name, ... i.e. add `labels = c("test", "test")` to your other scales as well or just recode the column outside of ggplot. Concerning the issue with the dodging. It's all about the grouping, i.e. make sure that you map the same variables on the `group` aes in each of your geom layers. – stefan Jul 14 '22 at 11:38
  • Added some Data, thank you for the comments Stefan. I tried to add the same labels, but it seems like I still do something wrong. – Timon Doo Jul 15 '22 at 09:45

1 Answers1

0

Merging legends can sometimes be painful. If your variables are already labelled (like in your example), then you also don't need to stipulate breaks or labels. (see first example).

However, a good rule is - don't add an aesthetic if you don't really need it. Size and color are constant aesthetics in your case, thus you could (and should) add it as a constant aesthetic outside of aes.

P.S. I have slightly changed the plot in order to make the essential more visible. I personally prefer to keep my plots in an order geoms->scales->coordinates->labels->theme, this helps me keeping an overview over the layers.

library(ggplot2)
data2 <- read.table(text = "Messzeitpunkt   Sum_PCL TB2
0               38      no
0               37      yes
0               40      yes
0               41      yes
0               38      yes
1               56      no
1               33      no
2               39      no
2               33      no", head = T)
outliersDF <- read.table(text = "Messzeitpunkt   Sum_PCLMean TB2 Sum_PCLSD
0               9           no  11
0               12          yes 11
1               9           no  15
1               18          yes 16
2               8           no  12
2               14          yes 12", head = T)

 
ggplot() +
  geom_line(data = data2, aes(
    x = Messzeitpunkt,
    y = Sum_PCL, group = TB2, linetype = TB2
  )) +
  geom_point(data = outliersDF, aes(Messzeitpunkt, Sum_PCLMean,
    shape = TB2, color = TB2, size = TB2
  )) +
  geom_point(data = data2, aes(Messzeitpunkt, Sum_PCL,
    shape = TB2, color = TB2, size = TB2
  )) +
  ## if your variable is labelled, no need to specify breaks or labels
  scale_color_manual(values = c("#000000", "#000000")) +
  scale_shape_manual(values = c(15, 17)) +
  scale_size_manual(values = c(2, 2))

  
  ## Better, if you have constant aesthetics, not to use aes(), but
  ## add the values as constants instead
ggplot() +
  geom_line(data = data2, aes(
    x = Messzeitpunkt,
    y = Sum_PCL, group = TB2, linetype = TB2
  )) +
  geom_point(data = outliersDF, aes(Messzeitpunkt, Sum_PCLMean,
    shape = TB2
  ), size = 2) +
  geom_point(data = data2, aes(Messzeitpunkt, Sum_PCL,
    shape = TB2
    ## black color is default, this is just for demonstration
  ), color = "black", size = 2) +
  scale_shape_manual(values = c(15, 17))

Created on 2022-07-15 by the reprex package (v2.0.1)

tjebo
  • 21,977
  • 7
  • 58
  • 94