Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to plot a line graph using data from two different dataframes

I have these two dataframes:

decils_general:

structure(list(grup_CNAE_Red = c("Total", "Total", "Total", "Total", 
"Total", "Total", "Total", "Total", "Total", "Total"), variable = structure(1:10, levels = c("decil1_inflacio", 
"decil2_inflacio", "decil3_inflacio", "decil4_inflacio", "decil5_inflacio", 
"decil6_inflacio", "decil7_inflacio", "decil8_inflacio", "decil9_inflacio", 
"decil10_inflacio"), class = "factor"), value = c(9.3, 8.9, 8.6, 
8.4, 8.4, 8.4, 8.2, 8.1, 8.2, 7.8)), row.names = c(NA, -10L), class = "data.frame")

and decils_primera:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

structure(list(grup_despesa_DGAPE = c("Total", "Total", "Total", 
"Total", "Total", "Total", "Total", "Total", "Total", "Total"
), variable = structure(1:10, levels = c("decil1_inflacio", "decil2_inflacio", 
"decil3_inflacio", "decil4_inflacio", "decil5_inflacio", "decil6_inflacio", 
"decil7_inflacio", "decil8_inflacio", "decil9_inflacio", "decil10_inflacio"
), class = "factor"), value = c(5.8, 5.7, 5.6, 5.3, 5, 4.9, 4.8, 
4.4, 4.4, 3.9)), row.names = c(NA, -10L), class = "data.frame")

I’m trying to plot a graph similar to this one:

enter image description here

So that variable should be the x axis and value the y axis.

I have tried the following, but I’m getting nowhere.

plot_decils_general_primera <- ggplot() +
  geom_line(data = decils_general, aes(x=variable, y=value)) +
  geom_line(data = decils_primera, aes(x=variable, y=value))

>Solution :

The issue is that the variable mapped on x is a discrete variable in which case you have to explicitly set the group aes and you should get a note about that:

geom_line(): Each group consists of only one observation.
ℹ Do you need to adjust the group aesthetic?

To fix that add e.g. group=1 to each of your geom_line which tells ggplot2 to treat all observations as one group which for simplicity we call 1.

library(ggplot2)
library(dplyr)

ggplot() +
  geom_line(data = decils_general, aes(x = variable, y = value, group = 1)) +
  geom_line(data = decils_primera, aes(x = variable, y = value, group = 1))

However, overall I would suggest to bind your datasets into one which in general simplifies the plotting code as you don’t need duplicated layers:

decils <- list(general = decils_general, primera = decils_primera) |>
  dplyr::bind_rows(.id = "dataset")

ggplot(decils, aes(x = variable, y = value, color = dataset, group = dataset)) +
  geom_line() +
  geom_point(aes(fill = dataset), shape = 21, alpha = .5, size = 3) +
  geom_label(aes(label = value, vjust = ifelse(dataset == "general", 0, 1)),
    fill = NA, label.size = 0, label.padding = unit(10, "pt"),
    show.legend = FALSE
  )

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading