I am plotting some data series along with regression lines using this code:
ggplot(dt1.melt, aes(x=lower, y=value, group=variable, colour=variable)) +
geom_point(shape=1) +
geom_smooth(method=lm,
se=FALSE)
However, I need to constrain the regression line to be through the origin for all series - in the same way as abline(lm(Q75~-1+lower,data=dt1)) would achieve on a standard R plot.
Can anyone explain how to do this in ggplot ?
You need to specify this in the formula argument to geom_smooth:
... + geom_smooth(method=lm, se=FALSE, formula=y~x-1)
Related
I am trying to plot the interaction effects from a multiple linear regression using ggplot2. However, the slope of the lines plotted do not match what they should be based on the estimates returned by the lm function.
Here is my code:
lm.sense <- lm(sense_of_belonging ~ active*mathEAL + MathID + comfort_speaking, data=Data)
library(ggplot2)
p.sense <- ggplot(lm.sense, aes(y=sense_of_belonging, x=active, color=mathEAL)) + geom_smooth(method="lm", se=FALSE)```
Does ggplot not hold the other variables constant?
ggplot2 works with data.frames and doesn't naturally know what to do with an lm object. (Try plot(lm.sense) to see what base R offers here.)
Your ggplot call is using the underlying data from Data (tucked away inside your lm.sense object) to make a plot where x = active and y = sense_of_belonging. It uses that underlying data to do a linear regression that doesn't relate to the mathEAL, MathID, and comfort_speaking variables. Compare these: (they have the same result)
lm.mtcars <- lm(mpg ~ wt + cyl, data = mtcars)
ggplot(lm.mtcars, aes(mpg, wt)) +
geom_point() + geom_smooth(method="lm", se=FALSE)
ggplot(mtcars, aes(mpg, wt)) +
geom_point() + geom_smooth(method="lm", se=FALSE)
Depending on what you want to do, you could show some of the impact of other variables within your geom_smooth by referencing those:
ggplot(mtcars, aes(mpg, wt, color = as.character(cyl))) +
geom_point() + geom_smooth(method="lm", se=FALSE, fullrange = TRUE)
It would help to understand what kind of output you're hoping to generate to give more specific suggestions.
I want to plot a negative binomial distribution and a Poisson distribution to fit my real data, but I don't know how to plot a legend, who can help me with that, thanks a lot. My code and picture is as follows:
ggplot() +
geom_density(aes(a),color="red",lwd=2) +
geom_density(aes(x=rpois(50,1.57)),color="purple",lwd=2) +
geom_smooth() +
geom_density(aes(x=rnbinom(100,size=0.2,mu=1.57)),color="blue",lwd=2) +
geom_smooth() +
coord_cartesian(xlim=c(0,10)) + labs(x="count")
And my data was uploaded here:
https://www.jianguoyun.com/p/DSHXKgMQm5CLBhiKjCc.
The easiest way to add a legend is to map a variable to color. For example
ggplot() +
geom_density(aes(a, color="data"),lwd=2) +
geom_density(aes(x=rpois(50,1.57), color="poisson"),,lwd=2) +
geom_smooth() +
geom_density(aes(x=rnbinom(100,size=0.2,mu=1.57),color="binomial"),lwd=2) +
geom_smooth() +
coord_cartesian(xlim=c(0,10)) + labs(x="count")
If I plot this
dodge <- position_dodge(.35)
ggplot(mediat, aes(x=t, y=Value, colour=factor(act),group=id )) +
geom_point(position=dodge) + geom_errorbar(aes(ymin=Value-sdt, ymax=Value+sdt),
width=0, position=dodge) + theme_bw() + geom_smooth(method="lm",se=FALSE,
fullrange=TRUE)
I get this
As you can see the regression line is not plotted.
with +stat_smooth(method=lm, fullrange=TRUE, se = FALSE) the result is the same.
I've found that removing the "group=id" I can get the regression lines but
then
ggplot(mediat, aes(x=t, y=Value, colour=factor(act) ))+ geom_point(position=dodge) +
geom_errorbar(aes(ymin=Value-sdt, ymax=Value+sdt), width=0, position=dodge) +
theme_bw() + geom_smooth(method="lm",se=FALSE, fullrange=TRUE)
As you can see, now it plot the lines but I loose the dodge function by groups.
How can I get both things at once?. I mean, regression lines by "id" on the first uncluttered plot?
Any other solution with base plot, lattice or any other common package would also be welcome.
Regards
I am triying to obtain a histogram. This is my code:
ggplot(data, aes(x=skus, fill=as.factor(stars))) +
+ geom_histogram(binwidth=.5, alpha=.5, position="identity") +
+ geom_vline(data=cdf, aes(xintercept=rating.mean, colour=as.factor(stars)),
+ linetype="dashed", size=1)
When I execute this code I obtain the next graphic:
This is not a histogram. What is my code mistake?
Thanks!
Just I have detected my mistake. I am defined the binwidth as .5. I only have to increment this rate to obtain a good histogram like binwidth=50
In ggplot2, the following command p <- qplot(wt, mpg, data=mtcars, colour=factor(cyl)) taken from here plots a scatter plot with each point coloured according to factor
I would like to fit all data with a geom_smooth irrespective of factor but keeping the colour of individual points according to factor. p + geom_smooth(method="lm") does a linear fit on each factor. How do I do this?
You can do this fairly easily by stepping back from the 'qplot' wrapper function and using the 'ggplot' and geometry functions directly.
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(aes(colour=factor(cyl))) +
geom_smooth(method="lm")
Step 1: Set your initial 'ggplot' settings. These are the settings that you want to be defaults for the geometry functions.
ggplot(mtcars, aes(x=wt, y=mpg))
In this case, we are using the 'mtcars' data for all geometries with 'wt' assigned to the x-axis and 'mpg' assigned to the y-axis. By specifying these at the beginning, we lessen the risk of messing something up when copy-pasting into the geometry functions.
Step 2: Draw the point geometry, using the factors of 'cyl' to color the points. This is what the original 'qplot' function was doing, but we're specifying it a little more explicitly.
geom_point(aes(colour=factor(cyl)))
Step 3: Draw the smoothed linear model. This is exactly what the OP wrote before, but now that the aesthetic of coloring is no longer part of the defaults, the model draws as intended.
geom_smooth(method="lm")
Chain it all together with the + et voila!
For reference: You could just as easily do this by being explicit in each layer, like so:
ggplot() +
geom_point(data=mtcars, aes(x=wt, y=mpg, colour=factor(cyl))) +
geom_smooth(data=mtcars, method="lm", aes(x=wt, y=mpg))
In my opinion, you'll find ggplot a lot easier if you start to use the ggplot() function rather than qplot. The control of aesthetics makes a lot more sense. In this case, you just build your base:
p <- ggplot(mtcars, aes(wt, mpg))
Then build the two geoms on top:
p + geom_point(aes(colour = factor(cyl))) +
geom_smooth(method = "lm")
Let me know if that wasn't what you're after.
I agree with previous answers from #alexwhan and #Dinre that the ggplot() + geom_point(...) + ... is the best approach to this problem
However, If you just would like to modify your solution try
p + geom_smooth(method = 'lm', aes(colour = NA), colour = 'magenta')