Problem generating a graph

I'm trying to make a line graph with ggplot2 with this dataframe, but I'm not able to. I guess it's very easy to achieve, but I'm stuck with it. I think I need to change the data format, but I can't figure out how. Any ideas?

 data.frame(
  stringsAsFactors = FALSE,
       check.names = FALSE,
         Quarter = c("Total"),
          `1º2004` = c(302),
          `2º2004` = c(396),
          `3º2004` = c(364),
          `4º2004` = c(334),
          `1º2005` = c(288),
          `2º2005` = c(286),
          `3º2005` = c(355),
          `4º2005` = c(408),
          `1º2006` = c(291),
          `2º2006` = c(437),
          `3º2006` = c(278),
          `4º2006` = c(559),
          `1º2007` = c(673),
          `2º2007` = c(774),
          `3º2007` = c(585),
          `4º2007` = c(763),
          `1º2008` = c(538),
          `2º2008` = c(449),
          `3º2008` = c(422),
          `4º2008` = c(315),
          `1º2009` = c(216),
          `2º2009` = c(311),
          `3º2009` = c(284),
          `4º2009` = c(324),
          `1º2010` = c(316),
          `2º2010` = c(400),
          `3º2010` = c(202),
          `4º2010` = c(458),
          `1º2011` = c(148),
          `2º2011` = c(239),
          `3º2011` = c(194),
          `4º2011` = c(350),
          `1º2012` = c(148),
          `2º2012` = c(155),
          `3º2012` = c(218),
          `4º2012` = c(352),
          `1º2013` = c(104),
          `2º2013` = c(140),
          `3º2013` = c(108),
          `4º2013` = c(261),
          `1º2014` = c(174),
          `2º2014` = c(159),
          `3º2014` = c(145),
          `4º2014` = c(190),
          `1º2015` = c(214),
          `2º2015` = c(239),
          `3º2015` = c(176),
          `4º2015` = c(207),
          `1º2016` = c(199),
          `2º2016` = c(225),
          `3º2016` = c(236),
          `4º2016` = c(237),
          `1º2017` = c(217),
          `2º2017` = c(220),
          `3º2017` = c(209),
          `4º2017` = c(242),
          `1º2018` = c(210),
          `2º2018` = c(273),
          `3º2018` = c(251),
          `4º2018` = c(223),
          `1º2019` = c(217),
          `2º2019` = c(226),
          `3º2019` = c(194),
          `4º2019` = c(262),
          `1º2020` = c(199),
          `2º2020` = c(128),
          `3º2020` = c(283),
          `4º2020` = c(286),
          `1º2021` = c(227)
)

ggplot expects the variables to be the rows of a column. So specify something like

df=data.frame(
  quarter=c('q1', 'q2',.... ), 
  value = c(11,14,....), 
stringsAsFactors=F
)

Great HanOostdijk, this works perfectly but to get the dataframe as columns I have to transpose the two rows with excel's transpose function. Is there a way to do it in R? Sorry if it is too obvious... Thanks

Hello @jose_perez ,

it is not very obvious, but in R transposing or pivoting is also often done.
See a solution below with the aid of some R packages.
Some notes:

  • the Quarter column is removed because its type is character and the others are numeric
  • it is possible to use the original quarter designation but I prefer to convert it to a date
    (the first day of the quarter). In that way ggplot2 knows how to handle it: shows not all values on the x axis.
df1 = data.frame(
  stringsAsFactors = FALSE,
       check.names = FALSE,
         Quarter = c("Total"),
          `1º2004` = c(302),
          `2º2004` = c(396),
          `3º2004` = c(364),
          `4º2004` = c(334),
          `1º2005` = c(288),
          `2º2005` = c(286),
          `3º2005` = c(355),
         # some lines left out
          `4º2020` = c(286),
          `1º2021` = c(227)
)

library(dplyr)
library(tidyr)
library(tidyselect)
library(ggplot2) 
library(lubridate)
library(magrittr)

df2 <-  dplyr::select(df1,`1º2004`:`1º2021`)
df3 <-  tidyr::pivot_longer(df2,cols=tidyselect::everything())
df4 <- df3 %>%
    dplyr::mutate(date = lubridate::yq(paste(substr(name,3,6),'.',substr(name,1,1),sep='')))

p <- ggplot(df4,aes(date,value)) +
  geom_point()
print(p)

1 Like

This is perfect. Thank you very much. It works like a charm. Regards.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.