0

i want to show the stat_smooth(method='lm') line on my plot but it does not show whatever i do... please help...

data <- read.csv(file = "C:/R/seoul.csv")

price_mul <- data$price/10
data$price_mul <- c(price_mul)

date_cut <- substr(data$date, 5,6) 
data$date_cut <- c(date_cut)

area<-"서울특별시 성동구"
data1 <- data[grep(area,data$시군구),]


library(dplyr) 
a <- data1 %>% filter(py < 20)


library(ggplot2)
ggplot(data=a, aes(x = a$date_cut, y = a$price_mul)) +
  scale_x_discrete(limits = c("06", "07", "08", "09", "10", "11", "12", "01", "02", "03", "04", "05")) +
  geom_point(size=1.5, color="darkred") + 
  ylim(0, max(data1$price_mul)) + labs(x = "기간 (2018.06 ~ 2019.05)", y = "거래금액(십만원)", size = 10) + 
  ggtitle(paste(area, "\n 20평미만 아파트 매매가 추이")) + theme(plot.title = element_text(face = "bold", hjust = 0.5, size = 16, color = "black")) + stat_smooth(method='lm')

enter image description here

aosmith
  • 34,856
  • 9
  • 84
  • 118
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. The problem appears to be that you are using a discrete scale for your x-axis. It doesn't really make sense to plot a regression line for a discrete x. – MrFlick Jun 17 '19 at 15:28
  • 1
    Maybe it's possible to solve the problem by adding `group = 1` within aes call: `aes(x = date_cut, y = price_mul, group = 1)` – pogibas Jun 17 '19 at 15:28
  • What's the class of `data$date_cut` (`class(data$date_cut)`)? Try to use `as.numeric()`, `as.Posix.ct()` or similar on it if it isn't such a class already...! – Simon Jun 17 '19 at 15:36
  • Also, sometimes it's just a matter of removing the `a$` of `aes(x = a$date_cut, y = a$price_mul)`. The `data=a` argument already tells `ggplot` where to find those variables. – Rui Barradas Jun 17 '19 at 15:43

0 Answers0