0

I have a dataset which looks like this one below.

month   Yi      Yi+1
1   0.014310185 13.43838262
2   0.014310185 15.7792948
3   0.176113783 16.14479846
4   3.143663699 16.54060078
5   3.755478277 16.75810501
6   3.767263653 17.03156884
7   3.767263653 17.03156884
8   3.829219647 17.03156884
9   4.375269901 17.78482322
10  8.707536696 18.47995179
11  10.28741362 21.33942187
12  10.66218286 21.82637774

I have 15 columns of Y (Yi to Yi+14). Column Yi, for instance, corresponds to the volume of precipitation over the 12 months of the year Yi. I have to barplot the volume of precipitation of all years (with their months) side by side on the x axis. In the end, I have to get something like this:
![enter image description here][1]

I have already tried the melt and group_by functions to reshape my data frame following this commands:

df  <- read_excel("df.xls", col_names = FALSE, skip = 1)
colnames(df) <- c("month", "Yi", paste0("Yi+", 1:14)

df.melt <- melt(tab.df, id = c("month", "Yi", paste0("Yi+", 1:14))

bar <- group_by(df.melt, aes(x = 1:length(value), y = value, fill=factor(month))) +
geom_bar(position="dodge", stat="identity"))

ggplot(bar, aes(x=variable, y=mean, fill=factor(month)))

but it did not work. any suggestions how to do that?

Basilique
  • 150
  • 1
  • 11
  • The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific problem you're having in a Minimal, Complete, and Verifiable example. For more information, please see [How to Ask](https://stackoverflow.com/help/how-to-ask) and [take the tour](https://stackoverflow.com/tour). – Gonçalo Peres Dec 27 '18 at 17:21
  • 1
    I edited my question according to your remarks! Thanks! – Basilique Dec 27 '18 at 17:49
  • 1
    `melt` and `group_by` are not base R functions. Please include all `library` lines. – Parfait Dec 27 '18 at 18:30

2 Answers2

1

An alternative could be to use geom_col and facet by year.

library(data.table) # for melt
library(ggplot2)

# Took the data example from @Istrel
set.seed(2018)
df <- data.frame(month = 1:12, matrix(abs(rnorm(12 * 15)), nrow = 12))
colnames(df) <- c("month", "Yi", paste0("Yi+", 1:14))
setDT(df) # just to be sure, convert to data.table; use setDF(df) to switch back
df_m <- data.table::melt(df, "month")

ggplot(data = df_m,
       aes(x = month, 
           y = abs(value),
           fill = as.factor(month))) +
  geom_col() +
  facet_grid(cols = vars(variable),
             space = "free_x",
             scales = "free_x",
             switch = "x") +
  # Some graph adjustments:
  scale_y_continuous(expand = c(0, 0)) +  # remove space between plot area and x axis
  labs(x = "Year", y = "Climate variable") +
  scale_fill_discrete(name = "Months") + # legend title
  theme(
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank(),
    panel.grid = element_blank(),
    panel.spacing = unit(0.1, "cm")  # adjust spacing between facets
  )

enter image description here

Hope this is also helpful.

Valentin_Ștefan
  • 6,130
  • 2
  • 45
  • 68
  • Thanks Valentin. when I run your script it give this error: `Error in value[[3L]](cond) : The melt generic in data.table has been passed a tbl_df (not a data.table) but the reshape2 package is not installed to process this type. Please either install reshape2 and try again, or pass a data.table to melt instead. > ` – Basilique Dec 27 '18 at 19:03
  • Hi. Use `setDT(your_tbl_df)` to switch it to a data.table and if you need the revert, use `setDF` function. I also updated my answer. – Valentin_Ștefan Dec 27 '18 at 19:04
  • Thanks, the error `data.table` is fixed. However, there is still this problem: `Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : there is no package called ‘stringr’`. when I try to install `stringr`, I find this error : `Error in library.dynam(lib, package, package.lib) : shared object ‘stringi.so’ not found ERROR: lazy loading failed for package ‘stringr’ * removing ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/stringr’ Warning in install.packages : installation of package ‘stringr’ had non-zero exit status` – Basilique Dec 27 '18 at 19:14
  • I used this (from [link](https://stackoverflow.com/questions/34201742/installation-of-packages-stringr-and-stringi-had-non-zero-exit-status)) to install `stringr` : `install.packages("stringi", dependencies=TRUE, INSTALL_opts = c('--no-lock')) install.packages("stringr", dependencies=TRUE, INSTALL_opts = c('--no-lock'))` It worked perfectly after the installation of stringr! Thanks – Basilique Dec 27 '18 at 20:07
0

You can melt the data frame and plot values against sequence 1:180. Then you can assign custom labels to x-axis in order to denote years.

library(reshape2)
library(ggplot2)

# Create example df with random values
df <- data.frame(month = 1:12, matrix(rnorm(12 * 15), 12))
colnames(df) <- c("month", "Yi","Yi+1", "Yi+2", "Yi+3", "Yi+4", "Yi+5","Yi+6",  "Yi+7", "Yi+8", "Yi+9", "Yi+10","Yi+11","Yi+12","Yi+13","Yi+14")

df_m <- melt(df, "month")

# Prepare labels (at each 6 month for 15 years)
x_labs <- c("Yi","Yi+1", "Yi+2", "Yi+3", "Yi+4", "Yi+5","Yi+6", "Yi+7",
            "Yi+8", "Yi+9", "Yi+10","Yi+11","Yi+12","Yi+13","Yi+14")

ggplot(df_m, aes(x = 1:length(value), y = value, fill=factor(month))) +
    geom_bar(position="dodge", stat="identity") +
    # add custom labels at each 6-th month
    scale_x_continuous(labels = x_labs, breaks = seq(6, 15 * 12 - 6, by = 12)) +
    xlab("year???")
Istrel
  • 2,508
  • 16
  • 22
  • Thanks @Istrel. The problem is that I cannot install the package `reshape2` since the installation of its dependent package `stringr` cannot be carried out. I could just install `reshape` and with `reshape` when I use `melt (df,"month")` this error occurs : `Error in match.names(clabs, names(xi)) : names do not match previous names` – Basilique Dec 27 '18 at 18:42
  • Sorry, cannot reproduce your error. I have tried `reshepe` package and the code works fine. – Istrel Dec 27 '18 at 19:10
  • Thanks Istrel. I tried with your random values and your script worked perfectly. But with my data.frame there is this error : `Error in match.names(clabs, names(xi)) : names do not match previous names ` – Basilique Dec 27 '18 at 19:19
  • Can you post `str(df)` output? – Istrel Dec 27 '18 at 19:21
  • `Classes ‘data.table’ and 'data.frame': 12 obs. of 16 variables: $ Yi : num 0.0143 0.0143 ... $ Yi+1 : num 13.4 15.8 .. $ Yi+2 : num 22.7 23.2 .. $ Yi+3 : num 36.5 37.6 .. $ Yi+4 : num 45.8 45.9 .. $ Yi+5 : num 49.2 49.3 .. $ Yi+6 : num 51.5 51.5 .. $ Yi+7 : num 57.5 57.6 .. $ Yi+8 : num 74.6 75.6 .. $ Yi+9 : num 77.2 77.5 .. $ Yi+10: num 87.1 87.4 .. $ Yi+11: num 89.4 89.7 ... $ Yi+12: num 91.8 91.8 .. $ Yi+13: num 96.1 96.7 .. $ Yi+14: num 99.3 99.3 .. $ month: num 1 2 3 4 5 6 7 ... - attr(*, ".internal.selfref")=` – Basilique Dec 27 '18 at 19:41
  • All seems correct. Still cannot reproduce the error. Maybe just rerun an R process and try from scratch. – Istrel Dec 27 '18 at 19:47