0

I have a dataframe with 6 features like this:

                X1   X2   X3   X4   X5   X6
Modern Dog     9.7 21.0 19.4  7.7 32.0 36.5
Golden Jackal  8.1 16.7 18.3  7.0 30.3 32.9
Chinese Wolf  13.5 27.3 26.8 10.6 41.9 48.1
Indian Wolf   11.5 24.3 24.5  9.3 40.0 44.6
Cuon          10.7 23.5 21.4  8.5 28.8 37.6
Dingo          9.6 22.6 21.1  8.3 34.4 43.1 

I want to draw a line plot like this:

enter image description here

I'm trying this:

plot(df$X1, type = "o",col = "red", xlab = "Month", ylab = "Rain fall")

lines(c(df$X2, df$X3, df$X4, df$X5, df$X6), type = "o", col = "blue")

But it's only plotting a single variable. I'm sorry if this question is annoying, i'm totally new to R and i just don't know how to get this done. I would really appreciate any help on this.

Thanks in advance

Miguel 2488
  • 1,410
  • 1
  • 20
  • 41
  • Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Apr 13 '19 at 21:28
  • These might help https://stackoverflow.com/a/48871624/786542 & https://stackoverflow.com/a/52789737/786542 – Tung Apr 13 '19 at 21:31
  • Thank you @Tung, my dataframe is posted in the question, is the whole dataframe, not a head() function – Miguel 2488 Apr 13 '19 at 21:41
  • My line chart is not based on time, but on features. I want to reproduce the same line chart of the pic i added – Miguel 2488 Apr 13 '19 at 21:42

1 Answers1

2

The easiest way would be to convert your dataset to a long format (e.g. by using the gather function in the tidyr package), and then plotting using the group aesthetic in ggplot.

I recreate your dataset, assuming your group variable is named "Group":

df <- read.table(text = "
Group                X1   X2   X3   X4   X5   X6
Modern_Dog     9.7 21.0 19.4  7.7 32.0 36.5
Golden_Jackal  8.1 16.7 18.3  7.0 30.3 32.9
Chinese_Wolf  13.5 27.3 26.8 10.6 41.9 48.1
Indian_Wolf   11.5 24.3 24.5  9.3 40.0 44.6
Cuon          10.7 23.5 21.4  8.5 28.8 37.6
Dingo          9.6 22.6 21.1  8.3 34.4 43.1 ",
  header = TRUE, stringsAsFactors = FALSE)

Then convert the dataset to long format and plot:

library(tidyr)
library(ggplot2)

df_long <- df %>% gather(X1:X6, key = "Month", value = "Rainfall")
ggplot(df_long, aes(x = Month, y = Rainfall, group = Group, shape = Group)) +
  geom_line() +
  geom_point() +
  theme(legend.position = "bottom")

enter image description here

See also the answers here: Group data and plot multiple lines.

Weihuang Wong
  • 12,868
  • 2
  • 27
  • 48
  • Hi. Thank you very much for your answer!! This looks like what i need, but when i'm executing your code, i'm getting the following error: `object 'Group' not found`. Do you knopw how i can fix this?? Thanks again :) – Miguel 2488 Apr 13 '19 at 23:08
  • 1
    I missed out a step: I assume your group variable is named `Group`. See my edit above. – Weihuang Wong Apr 13 '19 at 23:11
  • Hey, thanks a lot, now i got it right!! Just one more question please. How could i display the plot with the features ordered in ascending order by the column X4? – Miguel 2488 Apr 13 '19 at 23:36
  • 1
    This is a separate issue, and the comments section isn't designed for extended discussion. I would suggest you open a new question for this issue. – Weihuang Wong Apr 13 '19 at 23:38
  • Ok. Could you add a little edit to your answer if not please? If not, thanks anyway, you really helped me already – Miguel 2488 Apr 13 '19 at 23:39
  • 1
    You might want to refer to this question: https://stackoverflow.com/q/18413756/6455166 – Weihuang Wong Apr 13 '19 at 23:46
  • It's ok, i got it!! the correct way to go is using the `reorder` function when passing the x values to ggplot. Thanks a lot Weihuang Wong!! :) – Miguel 2488 Apr 13 '19 at 23:51