cxnrachel
cxnrachel

Reputation: 23

Why do I have weird vertical lines in my graph?

I'm trying to plot a simple line chart, but I have these weird vertical lines at every year (year is my x-axis). Any advice would be appreciated.

p1 <- ggplot(data=hdb_table, aes(x=year, y=resale_price, color=flat_type)) +
          geom_line()

enter image description here

dput(head(hdb_table)) enter image description here

structure(list(year = c(2015, 2015, 2015, 2015, 2015, 2015), 
date = c("2015-01", "2015-01", "2015-01", "2015-01", "2015-01", 
"2015-01"), month_no = c("01", "01", "01", "01", "01", "01"
), month_name = c("Jan", "Jan", "Jan", "Jan", "Jan", "Jan"
), region = c("North-East", "North-East", "North-East", "North-East", 
"North-East", "North-East"), town = c("ANG MO KIO", "ANG MO KIO", 
"ANG MO KIO", "ANG MO KIO", "ANG MO KIO", "ANG MO KIO"), 
estate_type = c("Mature Estate", "Mature Estate", "Mature Estate", 
"Mature Estate", "Mature Estate", "Mature Estate"), flat_type = c("3 ROOM", 
"3 ROOM", "3 ROOM", "3 ROOM", "3 ROOM", "3 ROOM"), block = c("174", 
"541", "163", "446", "557", "603"), street_name = c("ANG MO KIO AVE 4", 
"ANG MO KIO AVE 10", "ANG MO KIO AVE 4", "ANG MO KIO AVE 10", 
"ANG MO KIO AVE 10", "ANG MO KIO AVE 5"), storey_range = c("07 TO 09", 
"01 TO 03", "01 TO 03", "01 TO 03", "07 TO 09", "07 TO 09"
), floor_area_sqm = c(60, 68, 69, 68, 68, 67), flat_model = c("Improved", 
"New Generation", "New Generation", "New Generation", "New Generation", 
"New Generation"), lease_commence_date = c(1986, 1981, 1980, 
1979, 1980, 1980), age = c(33, 38, 39, 40, 39, 39), remaining_lease = c(70, 
65, 64, 63, 64, 64), resale_price = c(255000, 275000, 285000, 
290000, 290000, 290000)), class = c("tbl_df", "tbl", "data.frame"),row.names= c(NA, -6L))

Upvotes: 0

Views: 1069

Answers (1)

BeckyPat
BeckyPat

Reputation: 86

The code you probably want is

p1 <- ggplot(data=hdb_table, aes(x=year, y=resale_price, color=flat_type, group = flat_type)) +
          geom_line() 

The lines are joining up all points, rather than just within groups you've defined by color.

Upvotes: 2

Related Questions