nick.yili9393
nick.yili9393

Reputation: 27

Fix Error: Discrete value supplied to continuous scale in ggpubr::ggline

I run into the error when trying to plot a line chart using ggpubr::ggline. Below are the codes:

#getting the dataset
pd_RecThematicity <- read.csv("Lower_CRF_PDP/pd_RecThematicity.csv")
#plotting
ptg_RecThematicity<- ggline(pd_RecThematicity, 
                            x="RecThematicity", 
                            y="yhat",
                            facet.by="variant")+
  scale_x_continuous(name = "Recipient Thematicity")+
  scale_y_continuous(name = "Predicted odd of variants",
                     labels = c("0","0.5", "1"),
                     limits = c(0,1), 
                     breaks = c(0,0.5,1))+
                     geom_smooth() +
theme_classic2(base_size=12, base_family="Arial")
ggsave("pdp_RecThematicity.png",ptg_RecThematicity, width=6, height=4, units="in", dpi = 1000, type = "cairo")

And then R returns the error "Error: Discrete value supplied to continuous scale". However, I can confirm that the factors entered in both x and y axis are numeric (and R recognizes them as numeric when I check them in view(pd_RecThematicity)). The dataset can be downloaded in this OSF page.

Upvotes: 1

Views: 203

Answers (1)

tjebo
tjebo

Reputation: 23737

ggpubr::ggline internally converts your x to a factor (why? no idea).

you can change this with numeric.x.axis = TRUE

library(ggpubr)
#> Loading required package: ggplot2
pd_RecThematicity <- read.csv("https://osf.io/download/6b79z/")
#plotting
ggline(pd_RecThematicity, 
                            x="RecThematicity", 
                            y="yhat", 
       numeric.x.axis = TRUE,
                            facet.by="variant") +
  scale_x_continuous(name = "Recipient Thematicity")+
  scale_y_continuous(name = "Predicted odd of variants",
                     labels = c("0","0.5", "1"),
                     limits = c(0,1), 
                     breaks = c(0,0.5,1))+
  geom_smooth() +
  theme_classic2(base_size=12, base_family="Arial")
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Created on 2022-07-15 by the reprex package (v2.0.1)

P.S. - I really recommend investing some time to learn learning vanilla ggplot. This doesn't make much longer code, but gives you much more flexibility.

Upvotes: 3

Related Questions