A: Subtitles overlaid [ggplot2]

Asked

Viewed 301 times

1

Hello, personal,I’m having 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 Abels I 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 overlay?

Follow the codes I used and the generated image. Grateful!

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()

inserir a descrição da imagem aqui

  • 3

    Unfortunately, your question cannot be reproduced by anyone who tries to answer it. Please, take a look at this link and see how to ask a reproducible question in R, so that people who want to help you can do this in the best possible way.

  • ok. I’ll try to create a playable example. as I am 'beginner' in this environment R, I still have a little difficulty for this, but I will try and put here. thanks!

  • Is not this question who wants?

  • 1

    You can use the command dput() in your data and post the question, so we can work with the same database as you.

  • Rui Barradas, thanks for the tip, is more or less what I want yes, although I have not been able to apply for my case here. I’ll keep trying to follow these leads there. valeus!

  • I’ll try to do that, Flavio. Super thanks for the tip!

Show 1 more comment

1 answer

4

The Warning you receive when making this chart already tells you what is occurring:

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. As far as I know, we can not solve this problem. It is a feature of ggplot2. What I suggest is to replace one of the Fill scales with Colour. See 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()

inserir a descrição da imagem aqui

The T_IP have a white background, but the edges are colored. As far as I know, I suspect there is no way to make these color scales in which both are defined via Fill. Maybe you can do something using manual Scale, but this is beyond my knowledge.

  • 1

    This is already a good solution, Marcus. Thank you for it and for the explanation. Moreover, I have found that I have now managed to ask a reproducible question. Super thanks for the interaction!

  • It’s great to know that my response has helped you in some way. So consider vote and accept the answer, so that in the future other people who experience the same problem have a reference to solve it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.