1

My data looks like this.

  Week Africa Americas  Asia Europe Oceania Burma Cambodia China..Guangxi. China..Yunnan. Laos Thailand Vietnam
1    5      0        6  2912      4       5     0        1              46             26    0        8       2
2    6      0       15 19827     33      12     0        1             127            117    0       19       8
3    7      0       19 42551     49      15     0        1             210            149    0       32      14
4    8      1       22 72721     57      15     0        1             238            171    0       35      16
5    9      1       28 78517    294      15     0        1             251            174    0       35      16
6   10      8      105 86698   2828      31     0        1             252            174    0       43      16

When I want to graph this in ggplot2 is will give me the graph but not the legend indicating whicbh line-color belongs to which group. How can I add that to my graph?

enter image description here

ggplot(data=df, aes(x=Week))+
  geom_line(aes(y=Africa), color= "#8dd3c7")+
  geom_line(aes(y=Americas), color= "#ffffb3")+
  geom_line(aes(y=Asia), color= "#bebada")+
  geom_line(aes(y=Europe), color= "#fb8072")+
  geom_line(aes(y=Oceania), color= "#80b1d3")+
  geom_line(aes(y=Burma), color= "#fdb462")+
  geom_line(aes(y=Cambodia), color= "#b3de69")+
  geom_line(aes(y=China..Guangxi.), color= "#fccde5")+
  geom_line(aes(y=China..Yunnan.), color= "#d9d9d9")+
  geom_line(aes(y=Laos), color= "#bc80bd")+
  geom_line(aes(y=Thailand), color= "#ccebc5")+
  geom_line(aes(y=Vietnam), color= "#ffed6f")

Is the data format I'm providing wrong so I wont do this automatically or what am I doing wrong here?

Cheers, J

2 Answers 2

1

my suggestion is that you first need to change this data frame into a long data frame using the gather function from the tidyr package

df <- df %>% gather(df, key = "country", value = "count", 3:14)

Then in the ggplot you can write code like this:

ggplot(data=df, aes(x=Week, y = count, color = country))+ geom_line()

Long data frames are better because you will have one variable for each column (for example, "country") and ggplot works best with this kind of data frame. The legend will show up automatically

Sign up to request clarification or add additional context in comments.

Comments

1

You can also use pivot_longer() which is a proper method to get the plot with the legend and then scale the colors like this:

library(tidyverse)
#Code
df %>% pivot_longer(-Week) %>%
  ggplot(aes(x=Week,y=value,group=name,color=name))+
  geom_line()+
  scale_color_manual(values=c("Africa"="#8dd3c7",
                              "Americas"="#ffffb3",
                              "Asia"="#bebada",
                              "Europe"="#fb8072",
                              "Oceania"="#80b1d3",
                              "Burma"="#fdb462",
                              "Cambodia"="#b3de69",
                              "China..Guangxi."="#fccde5",
                              "China..Yunnan."="#d9d9d9",
                              "Laos"="#bc80bd",
                              "Thailand"= "#ccebc5",
                              "Vietnam"="#ffed6f"))+
  labs(color='Country')

Output:

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.