为什么我的图形中有奇怪的垂直线?

时间:2019-11-06 10:33:57

标签: r ggplot2

我试图绘制一个简单的折线图,但是每年都有这些奇怪的垂直线(年份是我的x轴)。任何建议将不胜感激。

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))

1 个答案:

答案 0 :(得分:2)

您可能想要的代码是

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

这些线将所有点连接在一起,而不仅仅是在您按颜色定义的组内。