Caption in ggplot does not appear

Asked

Viewed 393 times

3

I do not know what I am doing wrong, because the caption does not appear in the graph I build. Follow my code:

ND<-c("D0","D1","D2","D3e4") #diferentes níveis de desfolha (D)
Vreal<-c(0.1985479,0.2060983,0.1611603,0.1214494) #volume real das árvores
Vest_g=c(0.1892300,0.1964590,0.1529276,0.1296224) #vol estimado sem considerar D
Vest_d=c(0.1970788,0.2039802,0.1607308,0.1186836) #vol estimado considerando D
EP_Vreal=c(0.007480623,0.009235496,0.010960471,0.008099734) #Erro padrão da média
EP_Vest_g=c(0.007224098,0.008119300,0.009906934,0.008238645)
EP_Vest_d=c(0.005887057,0.007898459,0.010907461,0.007151453)

Volume<-data.frame(ND,Vreal,Vest_g,Vest_d,EP_Vreal,EP_Vest_g,EP_Vest_d)

 # plot dataframe
ggplot(Volume, aes(x = ND)) +
  labs(x = "Níveis de desfolha",
       y = "Vol. ind. total com casca (m³)") +
       theme(legend.position="top")+
       scale_fill_discrete(name="Legenda",
                           labels=c("Vreal", "Vest_g", "Vest_d")) +
  geom_bar(
    aes(y = Vreal),
    stat = "identity",
    fill = "seagreen1",
    width = .2,
    position = position_nudge(x = -.20),
    ) +
    geom_errorbar(aes(ymin = Vreal-EP_Vreal, ymax = Vreal+EP_Vreal), position = position_nudge(x = -.20), width = 0.1) +
  geom_bar(
    aes(y = Vest_g),
    stat = "identity",
    fill = "darkturquoise",
    width = .2,
    position = position_nudge(x = 0)
    ) +
    geom_errorbar(aes(ymin = Vest_g-EP_Vest_g, ymax = Vest_g+EP_Vest_g), position = position_nudge(x = 0), width = 0.1) +
  geom_bar(
    aes(y = Vest_d),
    stat = "identity",
    fill = "cyan",
    width = .2,
    position = position_nudge(x = .20)
    ) +
    geom_errorbar(aes(ymin = Vest_d-EP_Vest_d, ymax = Vest_d+EP_Vest_d), position = position_nudge(x = .20), width = 0.1)

Result: graphic without caption

Gráfico de saída

  • What do you want to do? Just add the caption?

  • Hello, yes, I want to add the caption. I tried many things and could not.

1 answer

4


ggplot is not putting subtitles automatically because, the way you’re doing the graph, he thinks there’s no need. By creating each geometry you are specifying color as an aesthetic attribute and not as a variable that should be mapped into an aesthetic (within the aes()).

The difference between mapping a variable in color and giving a value to an aesthetic attribute is that in the second case the generated image does not gain any information and, therefore, it does not need a legend.

ggplot(mtcars, aes(mpg, wt)) +
  geom_point(col = "cyan")

inserir a descrição da imagem aqui

The thing changes a bit of figure when a variable is mapped into some aesthetic, say, a color. In this case the color will bring an information and for this reason it should be decoded with the aid of a legend.

ggplot(mtcars, aes(mpg, wt)) +
  geom_point(aes(col = factor(cyl)))

inserir a descrição da imagem aqui

ggplot works best with long tables than wide. In this case the first step is to create a table in the long format.

library(tidyverse)

medidas <- Volume %>% 
  select(ND, starts_with("V")) %>% 
  gather(medida, valor, -ND)

desvios <- Volume %>% 
  select(ND, starts_with("EP")) %>% 
  gather(tipo, desvio, -ND) %>% 
  select(-ND)

tidy_vol <- bind_cols(medidas, desvios)

Like this data.frame it is even possible to simplify the code for creating the chart

ggplot(tidy_vol, aes(ND, valor, fill = medida)) +
  geom_col(position = "dodge") +
  geom_errorbar(aes(ymin = valor-desvio, ymax = valor+desvio), 
                position = position_dodge(0.9), width = 0.5)

inserir a descrição da imagem aqui

To use the colors you want, just include the colors in the color ladder used by the chart:

ggplot(tidy_vol, aes(ND, valor, fill = medida)) +
  geom_col(position = "dodge") +
  geom_errorbar(aes(ymin = valor-desvio, ymax = valor+desvio), 
                position = position_dodge(0.9), width = 0.5) + 
  scale_fill_manual(values = c("seagreen1", "darkturquoise", "cyan"))

inserir a descrição da imagem aqui

  • 1

    Tomás Barcelos, thank you so much for your help.

Browser other questions tagged

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