Change title and caption colors in ggplot2

Asked

Viewed 2,893 times

2

How do I change the field pop and continent. I want to put in Portuguese (Population and Continent). The data are in R through the package library(Gapminder).

I used the following command:

library(gapminder)
library(dplyr)
library(ggplot2)
gapminder_2007 <- filter(.data=gapminder,year==2007)
ggplot(data=gapminder_2007,aes(x=gdpPercap,y=lifeExp,color=continent,size=pop)) + 
   labs(title="PIB e Expectativa de Vida em 2007", subtitle="", 
   y="Expectativa de Vida",x="PIB Per capita", caption="") +
   geom_point() + 
   scale_x_log10()

Upshot:

img

1 answer

4

Use the arguments color and size within the function labs:

ggplot(data=gapminder_2007,aes(x=gdpPercap,y=lifeExp,color=continent,size=pop)) + 
  labs(title="PIB e Expectativa de Vida em 2007", subtitle="", 
       y="Expectativa de Vida",x="PIB Per capita", caption="",
       color = "Continente",
       size = "População") +
  geom_point() + 
  scale_x_log10()

Or

ggplot(data=gapminder_2007,aes(x=gdpPercap,y=lifeExp,color=continent,size=pop)) + 
  labs(title="PIB e Expectativa de Vida em 2007", subtitle="", 
       y="Expectativa de Vida",x="PIB Per capita", caption="") +
  geom_point() + 
  scale_x_log10() + 
  guides(color=guide_legend(title="Continente")) + # Argumentos para os títulos das legendas
  guides(size=guide_legend(title="População"))

Edition: Choose country colors: To change country colors you can use any option of scale_color_****

  • scale refers to the name of the function to make changes to colors; `
  • colorrefere-se ao nome do argumento que você colocou noaes` that you want to change;
  • ***** refers to the way you want to inform colors. You can pass the values of the form manual, brewer, and other forms (these are the main ones/which I use).

In the option manual, you must enter the names of the colors manually:

ggplot(data=gapminder_2007,aes(x=gdpPercap,y=lifeExp,color=continent,size=pop)) + 
  labs(title="PIB e Expectativa de Vida em 2007", subtitle="", 
       y="Expectativa de Vida",x="PIB Per capita", caption="",
       color = "Continente",
       size = "População") +
  geom_point() + 
  scale_x_log10() +
  scale_color_manual(values = c("green", "red", "yellow", "gray11", "blue")) #Informa aqui as cores!

Using the color palette Spectral of color brewer:

inserir a descrição da imagem aqui

ggplot(data=gapminder_2007,aes(x=gdpPercap,y=lifeExp,color=continent,size=pop)) + 
  labs(title="PIB e Expectativa de Vida em 2007", subtitle="", 
       y="Expectativa de Vida",x="PIB Per capita", caption="",
       color = "Continente",
       size = "População") +
  geom_point() + 
  scale_x_log10() +
  scale_color_brewer(palette = "Spectral")

inserir a descrição da imagem aqui

  • There is the possibility to change colors by continent in the graphic?

  • Edited question. Please mark how you accept the answer if it worked for you.

Browser other questions tagged

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