geom_smooth: not getting the lines and no error message showing

Not getting the lines and no error message showing. Is there someone to guide? Thanks

Below are the codes:
ggplot(DataSet0, aes(dateSale, priceSale))+
geom_point()+
geom_smooth(method = "lm", formula = y~poly(x,1), se = F) +
geom_smooth(method = "lm", formula = y~poly(x,1), se = F, color = "red") +
theme_economist() +
coord_cartesian(ylim = c(0, 45000)) +
ggtitle("Market Selection and Price Trend")

Without some sample data it is very difficult to diagnose the problem. A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here.

Using some mocked-up data the code seems okay. Note, I removed the line

geom_smooth(method = "lm", formula = y~poly(x,1), se = F) +

as it is redundant and commented out the coord_cartesian as I was using data with a mean = 0 and and sd = 1.

library(ggplot2)
library(ggthemes)

dat1 <- data.table(dat =(seq(ymd("2022/01/01"), ymd("2022/06/3"), by = "day")), pp = rnorm(154))

ggplot(dat1, aes(dat, pp))+
  geom_point()+
  geom_smooth(method = "lm", formula = y~poly(x,1), se = F, color = "red") +
  theme_economist() +
  # coord_cartesian(ylim = c(0, 45000)) +
  ggtitle("Market Selection and Price Trend")

dput(DataSet0)
structure(list(dateSale = structure(c(1632009600, 1614211200,
1651449600, 1618617600, 1648080000, 1636934400, 1633737600, 1.62e+09,
1621728000, 1644019200, 1609459200, 1612483200, 1623888000, 1646870400,
1637539200), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
priceSale = c(2e+05, 262000, 255000, 260000, 260000, 280000,
295000, 305000, 310000, 325000, 329000, 329000, 329900, 335000,
356000)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-15L))

Your ylim of 45000 should be 450000

1 Like

Thank you for the data.

The problem is coming from the coord_cartesian function but I do not understand why. Setting scale_y_continuous or ylim are also giving strange results. I do not remember using scale limits in ggplot2 so it it is doing something I do not understan.

This is plotting well.

library(ggplot2)
library(ggthemes)

dat1 <- structure(list(dateSale = structure(c(1632009600, 1614211200,
        1651449600, 1618617600, 1648080000, 1636934400, 1633737600, 1.62e+09,
        1621728000, 1644019200, 1609459200, 1612483200, 1623888000, 1646870400,
        1637539200), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
        priceSale = c(2e+05, 262000, 255000, 260000, 260000, 280000,
        295000, 305000, 310000, 325000, 329000, 329000, 329900, 335000,
        356000)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -15L))
                                                                                      


ggplot(dat1, aes(dateSale, priceSale))+
  geom_point()+
  geom_smooth(method = "lm", formula = y~poly(x,1), se = F, color = "red") +
  theme_economist() +
  scale_y_continuous(0, 40000) +
  ggtitle("Market Selection and Price Trend")

Ah, I see I misread the limits! However I am now getting

library(ggplot2)
library(ggthemes)

dat1 <- structure(list(dateSale = structure(c(1632009600, 1614211200,
        1651449600, 1618617600, 1648080000, 1636934400, 1633737600, 1.62e+09,
        1621728000, 1644019200, 1609459200, 1612483200, 1623888000, 1646870400,
        1637539200), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
        priceSale = c(2e+05, 262000, 255000, 260000, 260000, 280000,
        295000, 305000, 310000, 325000, 329000, 329000, 329900, 335000,
        356000)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -15L))
                                                                                      

ggplot(dat1, aes(dateSale, priceSale))+
  geom_point()+
  geom_smooth(method = "lm", formula = y~poly(x,1), se = F, color = "red") +
  theme_economist() +
  coord_cartesian(0, 450000) +
  ggtitle("Market Selection and Price Trend")
#> Error: Invalid input: time_trans works with objects of class POSIXct only

Created on 2023-01-18 with reprex v2.0.2

What is that doing? dateSale is POSIXct

Strange, this works fine for me

library(tidyverse)
library(ggthemes)

DataSet0 <- structure(list(dateSale = structure(c(1632009600, 1614211200,
1651449600, 1618617600, 1648080000, 1636934400, 1633737600, 1.62e+09,
1621728000, 1644019200, 1609459200, 1612483200, 1623888000, 1646870400,
1637539200), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
priceSale = c(2e+05, 262000, 255000, 260000, 260000, 280000,
295000, 305000, 310000, 325000, 329000, 329000, 329900, 335000,
356000)), class = c("tbl_df", "tbl", "data.frame"), 
row.names = c(NA,-15L))

ggplot(DataSet0, aes(dateSale, priceSale)) +
  geom_point() +
  geom_smooth(method = "lm", formula = y ~ poly(x,1), se = F, color = "red") +
  theme_economist() +
  coord_cartesian(ylim = c(0, 450000)) +
  ggtitle("Market Selection and Price Trend")

Created on 2023-01-18 with reprex v2.0.2

I changed the coord_cartesian(0, 450000) to coord_cartesian(ylim = c(0, 450000)) and it works.

1 Like

I'v got several packages loaded so it's probably a masking. I'll reload R and check it out.

Thanks

edit
Stll the problem. Very strange.

Did you see my earlier reply to change coord_cartesian(0, 450000) to coord_cartesian(ylim = c(0, 450000))?

Yes, I made the change. It looks like it may be some peculiarity of my setup but I cannot think of what.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.