Plot plots grouped bars associated with dots connected by rows

Asked

Viewed 46 times

1

I am not able to perform the grouping of the bars of the graph below even using the argument position = "dodge" in the geom_bar.

I found searches only with individual bars, I’m having difficulty creating this graph with points+lines when the bars are grouped.

Follow the command I am using and an example of how I would like to leave the chart.

dt <- data.frame(periodo = c ("junho", "julho", "agosto"), 
                 peso = c(1, 5, 4, 3, 4, 3),  
                 atr = c(0.95, 0.5, 0.7, 0.75, 0.6, 0.8)) 

ggplot() + 
  geom_bar(mapping = aes(x = dt$periodo, y = dt$peso), 
    stat = "identity", 
    fill = "lightblue", 
    position= "dodge", 
    width = 0.8) +
  geom_line(mapping = aes(x = dt$periodo, y = dt$atr),
    size = 1, color = "blue") + 
  geom_point(mapping = aes(x = dt$periodo, y = dt$atr),
    size = 1, color = "black")+
  scale_x_continuous(name = "Período") +
  scale_y_continuous(name = "Peso", 
    sec.axis = sec_axis(~./5, name = "ATR", 
                        labels = function(b) {paste0(round(b*100, 0), "%")})) +
  theme(axis.title.y = element_text(color = "grey"),
        axis.title.y.right = element_text(color = "blue"),
        plot.title = element_text(vjust = 1.5, hjust = 0.5))+
  labs(x = "Período", y = "", 
    title = "Dry - off", 
    subtitle = "Comparativo - Peso x ATR")

[Gráfico desejado1

1 answer

3


To have the bars grouped, you need a variable to indicate to the ggplot which row belongs to which group:

library(ggplot2)

dt$grupo <- rep(c("A", "B"), each = nrow(dt)/2)

# Ordena os níveis da variável período para serem plotados na ordem correta
dt$periodo <- factor(dt$periodo, levels = c("junho", "julho", "agosto"))

ggplot(dt, aes(periodo, peso, fill = grupo)) +
  geom_col(position = "dodge")

inserir a descrição da imagem aqui

I removed the customizations to highlight the relevant parts of the code. Some notes:

  • ggplot works internally with with: enter the object with the data and you can pass the variable names directly.
  • aesthetic elements that are used for various geometries can be indicated in aes global
  • geom_col is the version of gem_bar who uses stat = "identity" by default.

As for the lines/points, the graph that posted for example has an inadequate representation of the data: it shows the values of all groups as a single sequence. To make a better representation, you can separate by group, using position_nudge to adjust the position with the bars:

ggplot(dt, aes(as.integer(periodo), atr, color = grupo)) + # para linhas, a variável do eixo X precisa ser contínua
  geom_col(aes(periodo, peso, fill = grupo, color = NULL),
    position = "dodge") +
  geom_line(
    data = subset(dt, grupo == "A"),
    position = position_nudge(x = -.25)) +
  geom_point(
    data = subset(dt, grupo == "A"),
    position = position_nudge(x = -.25)) +
  geom_line(
    data = subset(dt, grupo == "B"),
    position = position_nudge(x = .25)) +
  geom_point(
    data = subset(dt, grupo == "B"),
    position = position_nudge(x = .25)) +
  scale_color_manual(values = c("darkred", "darkblue"))

inserir a descrição da imagem aqui

Browser other questions tagged

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