0

I am trying to create a line plot with ggplot2.The plot is right but there is an issue with the years on x-axis. I want all years in the data frame given below to be presented in the plot but only few values from years column is present in the plot. Is there any solution? The data is given below: year attacks killed 1999 1 2 2000 1 1 2001 3 3 2002 1 1 2003 2 65 2004 1 1 2005 1 1 2006 1 1 2007 1 1 2008 1 1 2009 1 1 2010 4 75 2011 4 61 2012 9 32 2013 2 180 2014 4 70 2015 4 13 2016 3 7 2017 9 25 The code to make the plot is given below:

data <- read.table("data.txt", header = T)
p <- ggplot(moomins,aes(year))+
+ geom_line(aes(y= attacks,color = "Attacks"))+
+ geom_line(aes(y= killed,color = "Killed"))
p + coord_flip(xlim = c(1999,2017 ))
print(p)

The plot I am getting is given below:enter image description here

2
  • Your data is really difficult to use to reproduce the code in that format. You are better off providing the output of dput() into the question. Commented Nov 5, 2017 at 21:31
  • 1
    My guess is that if you include breaks = 1999:2017 within the scale_x_continuous argument you should fix it: ggplot2.tidyverse.org/reference/scale_continuous.html Commented Nov 5, 2017 at 21:37

3 Answers 3

1

Try this:

df %>% 
  ggplot(aes(year)) +
  geom_line(aes(y = attacks, color = "Attacks")) +
  geom_line(aes(y = killed, color = "Killed")) +
  scale_x_continuous(breaks = seq(min(df$year), 
                                  max(df$year)))

Which gives:

enter image description here

I used the following data:

df <- c("
        year    attacks killed
        1999    1   2
        2000    1   1
        2001    3   3
        2002    1   1
        2003    2   65
        2004    1   1
        2005    1   1
        2006    1   1
        2007    1   1
        2008    1   1
        2009    1   1
        2010    4   75
        2011    4   61
        2012    9   32
        2013    2   180
        2014    4   70
        2015    4   13
        2016    3   7
        2017    9   25
        ")
df <- read.table(text = df, header = TRUE)
df <- as_tibble(df)
Sign up to request clarification or add additional context in comments.

Comments

0

You can add to the ggplot the following: (I think you were inconsistent in naming---moomin is your dataframe name, right?)

p + 
  scale_x_continuous(breaks = seq(min(moomin$year), 
                                  max(moomin$year), by = 1))

Let me know if this doesn't work for you. You might want to rotate your tick labels unless your plot sizes are going to be pretty big, because the labels will start to overlap!

1 Comment

Your solution is producing the same plot that I have drawn on the top but thanks for the help.
0

Factor your years

fyear<-factor(year)

And use fyear as your x

p <- ggplot(moomins,aes(fyear))+
+ geom_line(aes(y= attacks,color = "Attacks"))+
+ geom_line(aes(y= killed,color = "Killed"))
p + coord_flip(xlim = c(1999,2017 ))
print(p)

1 Comment

I checked your solution but it did not worked right but thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.