1
votes

I hope this question isn't a duplicate. I tried to find answers per the site's requirements before posting, but since I am so new, the help forums are too foreign to me.

Following Wickham's R for data visualization, I easily used geom_point for an integrated data set, mpg:

smooth fit for integrated data set

simple reference code:

ggplot(data = mpg)+
    geom_smooth(mapping = aes(x=displ, y=hwy))+
    geom_point(mapping = aes(x=displ, y=hwy))

Excited by this cool plot, I tried to do the same for some personal research data, which describes inteferon-beta production over five time points (A,b,c,d,e instead of numerical data).

I used the same code, essentially:

ggplot(data = ifnonly)+
    geom_smooth(mapping = aes(x=HOURS, y=IFNB))+
    geom_point(mapping = aes(x=HOURS, y=IFNB))

IFNb expression at five timepoints

Unfortunately, the line does not display. In fact, nothing displays until I add the geom_point function. What am I missing here? Is there more complex code required or is there some subtlety that I can apply to future uses of this function and ggplot?

1
This is because the x-axis variable is categorical (either factor or character) rather than numeric. The x-axis variable must be numeric for geom_smooth to produce a regression line.eipi10
Also, for future reference, when you're using the same data columns for each geom, you can shorten your code: ggplot(mpg, aes(displ, hwy)) + geom_smooth() + geom_point(). When you provide the mapping variables in the initial ggplot call, they apply to all geoms, unless overridden by new column mapping inside a geom.eipi10

1 Answers

0
votes

I think you should get your desired output with following one line code

          library(ggplot2)
          ggplot(mtcars, aes(disp,mpg))+geom_smooth() # one line code where I have mentioned data is mtcars , and disp as x axis and mpg as y axis you could get following output  
    # please check this link for output 

o/p without geom_point

         library(ggplot2)
          ggplot(mtcars, aes(disp,mpg))+geom_smooth()+geom_point()

o/p with geom_point