Problems with a simple line graph

Hi there! I have a dataframe like this:

example<data.frame(country=c("Argentina","Uruguay","Brazil","Argentina","Uruguay","Brazil","Argentina","Uruguay","Brazil"),
                year=c(2018,2019,2020,2018,2019,2020,2018,2019,2020),
                value=c(10,12,13,12,14,17,13,13,20))

And I am trying to create a simple line graph for each country, but my results are not as i expected. What is my mistake?

ggplot(example,aes(factor(year),value),group=country) + geom_line()

image

You have two problems. First, by making the year a factor, you're implicitly saying it's a discrete variable, so there will be no lines between years. Second, even if you remove factor, you still have several y for each x, that makes strange vertical lines (since geom_line() will try to make a line between each consecutive point). So in this case you can first summarize and plot the line only for the mean (for example).

example %>%
  group_by(year) %>%
  summarize(mean_val = mean(value)) %>%
  ggplot(aes(x=year, y=mean_val)) +
  geom_line()

And you can even augment it with error bars and the original points:

example %>%
  group_by(year) %>%
  summarize(mean_val = mean(value),
            sd = sd(value)) %>%
  ggplot(aes(x=year, y=mean_val)) +
  geom_line() +
  geom_errorbar(aes(ymin=mean_val - sd, ymax = mean_val + sd), width = .1) +
  geom_jitter(data = example, mapping = aes(x=year, y=value), color = "green", width = .1)

Thank you for your answer. As i said, i am looking to draw a line for each country, so i can compare their evolution each year, so i would have three lines

Oh right, then you just need to have the group inside the aes(), since you want to map it to a variable.

Also, there is a smal problem with your example data, where you have 1 country per year, which is probably not true.

example <- data.frame(country=c("Argentina","Uruguay","Brazil","Argentina","Uruguay","Brazil","Argentina","Uruguay","Brazil"),
                   year=c(2018,2018,2018,2019,2019,2019,2020,2020,2020),
                   value=c(10,12,13,12,14,17,13,13,20))

ggplot(example,aes(year,value,group=country)) + geom_line()
#same with color
ggplot(example,aes(x=year,y=value, color = country)) + geom_line()

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