R: Overlapping subtitles [ggplot2]

1

Hello, I have a subtitle overlay problem in ggplot. The following error message appears.

Scale for 'fill' is already present. Adding another scale for 'fill', which
will replace the existing scale.

I used two overlapping data.frames whose structures follow:

dput(newdf)
structure(list(Col2 = c(2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 
2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 
2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 
2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2015L, 2015L, 2015L, 
2015L, 2015L, 2015L, 2015L, 2015L, 2016L, 2016L, 2016L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L
), T_Ef = c("Col22", "Col22", "Col22", "Col22", "Col22", "Col22", 
"Col22", "Col22", "Col22", "Col23", "Col23", "Col23", "Col23", 
"Col23", "Col23", "Col23", "Col23", "Col23", "Col23", "Col26", 
"Col26", "Col26", "Col26", "Col26", "Col27", "Col27", "Col27", 
"Col27", "Col28", "Col28", "Col23", "Col23", "Col24", "Col24", 
"Col26", "Col26", "Col26", "Col27", "Col22", "Col23", "Col24", 
"Col22", "Col22", "Col22", "Col22", "Col22", "Col23", "Col23", 
"Col23", "Col24", "Col25", "Col26", "Col26", "Col27", "Col27", 
"Col27", "Col27", "Col27", "Col28", "Col28"), Exist_ = c("Sim", 
"Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", 
"Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", 
"Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", 
"Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", 
"Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", 
"Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", 
"Sim", "Sim", "Sim", "Sim", "Sim"), contar = c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), total = 1:60), class = "data.frame",
row.names = c(NA, 
-60L), .Names = c("Col2", "T_Ef", "Exist_", "contar", "total"
))

and

dput(newdf2)
structure(list(Col2 = c(2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 
2013L, 2013L, 2013L, 2013L, 2013L, 2015L, 2016L, 2016L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L
), T_IP = c("Col11", "Col11", "Col12", "Col12", "Col13", "Col13", 
"Col13", "Col14", "Col15", "Col15", "Col15", "Col12", "Col12", 
"Col12", "Col11", "Col11", "Col11", "Col11", "Col12", "Col12", 
"Col13", "Col14", "Col14", "Col15"), Ex_IP = c("Sim", "Sim", 
"Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", 
"Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", 
"Sim", "Sim", "Sim", "Sim"), Qt = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), total_2 = 1:24), class = "data.frame", row.names = c(NA, 
-24L), .Names = c("Col2", "T_IP", "Ex_IP", "Qt", "total_2"))

The labels I've created are as follows:

dput(Label_ef)
c("Ef1", "Ef2", "Ef3", "Ef4", "Ef5", "Ef6", "Ef7")

dput(IPs) 
c("OP", "Conselhos", "Conferências", "Mov. Sociais", "Outros")

The question would be: how could I fix subtitle overlays?

Here are the codes I used and the generated image. Thanks!

dt_plot = ggplot() +
  geom_jitter(data= newdf, aes(x=Col2, y=total, fill=T_Ef),
            shape=21, size=4, alpha=0.2) +
            scale_fill_discrete(labels= Label_ef) +
  geom_jitter(data=newdf2, aes(x=Col2, y=total_2, fill=T_IP),
          shape=24, size=3, alpha=0.5) + 
          scale_fill_discrete(labels=IPs) +
          theme_classic()

    
asked by anonymous 02.02.2018 / 14:56

1 answer

3

The warning that you receive when doing this graphic already says what is happening:

Scale for 'fill' is already present. Adding another scale for 'fill', which
will replace the existing scale.

That is, adding another fill scale will make the second scale overlap the first one. As far as I know, this problem can not be solved. It is a characteristic of ggplot2 . What I suggest is replace one of the scales with fill by color. Here's how I did this, and then the result I got:

ggplot() +
  geom_jitter(data= newdf, aes(x=Col2, y=total, fill=T_Ef),
              shape=21, size=4, alpha=0.2) +
  scale_fill_discrete(name="T_Ef", labels=Label_ef) +
  geom_jitter(data=newdf2, aes(x=Col2, y=total_2, colour=T_IP),
              shape=24, size=3, alpha=0.5) + 
  scale_colour_discrete(name="T_IP", labels=IPs) +
  labs(x="Ano", y="Total") +
  theme_classic()

The T_IPs have white backgrounds, but have colored borders. As far as I know, I suspect there is no way to make these color scales where both are defined by fill. Maybe you can do something using manual scale, but this is beyond my knowledge.

    
08.02.2018 / 16:08