0

My data:

structure(list(YEAR = structure(1:10, .Label = c("1980", "1981", 
"1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989"
), class = "factor"), lupas = c(1185, 822, 1340, 853, 3018, 1625, 
966, 1505, 1085, 1754)), .Names = c("YEAR", "lupas"), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"), drop = TRUE)

I've group the data with group_by from dplyr.

str(df1)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   10 obs. of  2 variables:
 $ YEAR : Factor w/ 10 levels "1980","1981",..: 1 2 3 4 5 6 7 8 9 10
 $ lupas: num  1185 822 1340 853 3018 ...
 - attr(*, "drop")= logi TRUE

I'm trying to plot a graph of the years, and the "lupas" each year. Like a time series graph.

This is my code, but gives an error:

ggplot(df1,
   aes(x = YEAR, y = lupas)) +
geom_line()


geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

And this graph:

enter image description here

After reading this thread: ggplot2 each group consists of only one observation

I've changed my code to:

ggplot(df1,
   aes(x = YEAR, y = lupas)) +
geom_line(position=position_dodge(.1))

Now i get this message:

ymax not defined: adjusting position using y instead
geom_path: Each group consist of only one observation. Do you need  to   adjust the group aesthetic?
Community
  • 1
  • 1
Omar Gonzales
  • 3,806
  • 10
  • 56
  • 120
  • Why do you want a discrete x-axis? Why not `ggplot(df1,aes(x = as.numeric(as.character(YEAR)), y = lupas)) + geom_line() + scale_x_continuous(breaks=as.numeric(as.character(df1$YEAR)))`? – Stibu Jul 21 '15 at 20:53
  • @Stibu, i thought it was the answer. But, i see i needed the aes - group. – Omar Gonzales Jul 21 '15 at 22:17

1 Answers1

2

Maybe this helps:

library(ggplot2)
p <- ggplot(df1, aes(x = YEAR, y = lupas, group = 1)) +  
    geom_point(color = "black") + geom_line(color = "blue")

enter image description here

Edit

To display only specific ticks on the x-axis one can use scale_x_discrete() in combination with breaks:

p <- ggplot(df1, aes(x = YEAR, y = lupas, group = 1)) +  
  geom_point(color = "black") + geom_line(color = "blue") +
  scale_x_discrete(breaks = c("1980", "1985", "1989"))
RHertel
  • 23,412
  • 5
  • 38
  • 64
  • thanks. I need to look for more info about the aes group. Just one more question. Could you edit it, so in the X axis, there is just: 1980, 1985 abd 1989? thanks – Omar Gonzales Jul 21 '15 at 22:15