Adding legend to existing lines in a plot in r - r

I'm trying to add a legend for my regression plot with several coloured lines.
What I've done is
plot(Leaves ~ Elevation)
plot(Elevation, Leaves, xlab="Elevation",ylab="Presence")
g=glm(Leaves~Elevation,family=binomial,data=Leaf)
curve(predict(g,data.frame(Elevation=x),type="resp"),add=TRUE, col="blue")
I do this for every line and end up with up with a plot with several coloured lines and i know have to add a legend for the lines.
But i donĀ“t know how to add some colour explanations figures to the legend, and all I've seen so far I have to start over again with doing the lines as well. Is there not an easy way to add colour explanation legends to already existing lines.

Related

R: How to center the tick labels (putting labels between tick marks) using ggplot?

I was using R ggplot for data visualization, and I met the problem centering the tick labels between tick marks on x/y axes. Here's a similar question but the implementation is matplotlib.
This is what I had (the wrong version):
And this is what I expected:
THANKS A LOT!!!
It is hard to reproduce your plot without the data you used to create the plot or the code you used to generate the plot
You can move the x-axis labels to the right by adding the following code to your plot
+ theme(axis.text.x=element_text(hjust=-0.5))
Additionally, one option might be just to rotate the x-axis labels instead of moving them. You can do this by adding the following code
+ theme(axis.text.x=element_text(angle = 90))

Mixed geom_line & geom_point plot: remove marker from color scale

I often have to use plots mixing lines and points (ggplot2), with the colors of the line representing one variable (here, "Dose"), and the shape of the points another one (here, "Treatment). Figure 1 shows what I typically get:
Figure 1: what I get
I like having different legends for the two variables, but would like to remove the round markers from the color scale, to only show the colors (see legend mockup below, made with Gimp). Doing so would allow me to have a clean legend, with colors and shapes clearly segregated.
Figure 2 (mockup): what I would like
Would anyone know if there is a way to do that? Any help would be much appreciated.
Note: the plots above show means and error bars, but I have the same problem with any plot mixing geom_line and geom_point, even simple ones.
Thanks in advance !

How do I Create a Faceted Bar Graph with Different Discrete X Axis in R ggplot

I have a bar graph which looks like the following:
Problem: If I facet it by the same variable as the color, the x-axis has space for plotting all the bars even though I don't need them.
My Solution: I used multiplot function from the Rmisc to separately make bar graphs for each partner but then a lot of individual customization is needed to make the graphs go cohesively together.
Question: Is there another way that I can use to get closer to plot 2 without the extra spaces for the variables that don't apply.
I'm using ggplot to plot.
use:
+ facet_wrap(~variable_to_facet_by,
scales = 'free')
as part of your ggplot code and that should get you what you want.

Add trend line for every facet using ggplot?

I have created a similar plot following the instructions from this link regarding the plot with facets:
(source: statmethods.net)
I would like to add a trend line for every facet of the chart and also would like to arrange the facets for the y axis (3 gears, 4gears and 5 gears) manually. Same thing for x axis (4cyl, 6vyl and 8cyl) facets so they are arranged the way I want it rather than being sorted/arranged automatically.
I tried looking for a solution here in StackExchange and Googling around and found ways to add trendiness for charts that do not involve facets, but not how add trend lines for charts with multiple facets.

Horizontal grid not matching y axis ticks

I have the following data:
x=c(2.880262,3.405859,3.613575,3.744480,3.682059,3.694075,3.758320,4.034290,4.202741,4.309383,4.996279,5.981309,5.103148,4.926363,4.696024,5.522913,5.330382,4.434304,5.154567,6.247156,8.612752,9.996526,9.606994,10.303496,5.954970,5.688171,6.340349,6.252854,6.355642,5.988570,7.317148,11.664384,14.231579,16.489029,23.100640,20.280043,21.562793,24.311327,23.735198,23.796386,23.118181,23.269722,19.886981,20.000975,19.967642,24.278910,17.447721,14.536114,20.646378,19.096832,20.258060,19.803196)
y=1:52
w=c(-2784,-2897,-2897,-2066,-2466,-2466,-2466,-2466,-2102,-2102,-2102,-2202,-2094,-2094,-2094,-2094,-1691,-1691,-1691,-1691,-1691,-1674,-1774,-1774,-2019,-2019,-2019,-2019,-2019,-1988,-1988,-1988,-1988,-1988,-1888,-1888,-1888,-1888,-1888,-1888,-1888,-1488,-2051,-2051,-2051,-2051,-2315,-2315,-2315)
v=1:49
When I try to plot these, my grid does not match the tick marks. Is there a way to fix this in base?
plot(y,x,type='l',col='blue',log='y')
grid(NA,NULL)
Resulting plot:
And the other plot:
plot(v,w,type='l',yaxt='n')
grid(NA,NULL)
axis(2,pretty(w),format(pretty(w)/1000,big.mark=','))
Result:
I put both up because I am using different techniques to label the y axis, and one is a log chart while the other is not. By the way, I have hundreds of other data sets that are placing the grid lines by the tick marks. It is just these two that are not matching grids to ticks.
For the first plot, just use equilogs=F.
For the second plot, since you are using non-default axis ticks, I think you'll have to resort to abline like it says in ?grid. Good luck!

Resources