Adding legend to a single line chart using ggplot - r

I just try to make a line chart and add a legend to it using ggplot in R. The following is my code.
ggplot(mtcars, aes(x=mpg, y=wt)) + geom_line(stat = "identity") + scale_fill_identity(name = "", guide = "legend", labels = c("myLegend"))
and I got the following:
The legend is not shown in the plot and what I want is the following:
which I plot using Matlab. Could anyone tell me how to do it in R? Thank you so much!!

You plot is not showing a legend, because there are no aesthetics mapped to the line. Basically, ggplot sees no reason to add a legend as there's only one line.
A simple way to get a legend is to map the line type to a character string:
ggplot(mtcars, aes(x=mpg, y=wt, lty = 'MyLegend')) + geom_line()
You can have a look at ?scale_linetype for information on how to modify tthat legend.
For example, use + scale_linetype('MyLegendTitle') to change the legend title.

Related

legend ticks outside of the key box in ggplot2

I'm trying to generate a "satisfactory" legend using ggplot2, but what frustrates me is the position of ticks (they are inside the legend key). Here is some code to show what I mean.
library(ggplot2)
ggplot(mtcars, aes(x = cyl, y = gear, fill = mpg)) +
geom_tile() +
coord_equal() +
theme(legend.position = 'top')
What I get is below. The ticks are inside the legend key box.
Is there a way to put the ticks outside of the legend key box? Just like this legend (it look like they did this with ggplot2):
Any suggestion would be appreciated!!!

Duplicate legends in overlayed density plots using ggplot2

I am trying to generate density plot with two overlaid distributions using ggplot2. My data looks like:
diag_elements <- data.frame(x = c(diag(Am.dent), diag(Am.flint)),
group=rep(c("Dent", "Flint"), c(length(diag(Am.dent)), length(diag(Am.flint)))))
And my call to ggplot is:
ggplot(diag_elements) +
geom_density(aes(x=x, colour=group, fill=group), alpha=0.5) +
labs(x = "Diagonal elements of the matrix", y = "Density", fill = "Heterotic Group") +
theme(legend.position = c(0.85, .75))
However, instead of simply renaming the legend with the more complete name specified in fill, this generates a second legend:
Does anyone have any suggestions for getting this same graph, but without the improperly formatted legend?
Thanks!
The other option is guides which allows specific removal of certain legneds. You simply add to your ggplot
+guides(color=FALSE)

ggplot: color points by fill group without creating a duplicated legend

When I try to define colors for groups of points, the legend is wrong and duplicated.
My data looks like this:
My ggplot code to plot multiple groups with error bars is thus:
ggplot(datac, aes(x=metric, y=avg, fill=group,color=col)) +
geom_point(size=5) +
geom_errorbar(position=position_dodge(.9), width=.25, aes(ymin=err_low, ymax=err_high)) +
theme_minimal()+
scale_y_continuous(labels = scales::percent,breaks=c(seq(-1,+1,by=0.05)))
which displays
See how the legend is duplicated and primary legend is shaded with the correct colors. What am I doing wrong?
Thank you!

Control ggplot2 legend look without affecting the plot

I'm plotting lines with ggplot2 like this:
ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line() + theme_bw()
.
I find legend marks to be small so I want them to be bigger. If I change the size, lines on the plot change too:
ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line(size=4) + theme_bw()
.
But I only want to see thick lines in the legend, I want lines on the plot to be thin. I tried to use legend.key.size but it changes the square of the mark, not the width of the line:
library(grid) # for unit
ggplot(iris,aes(Petal.Width,Petal.Length,color=Species))+geom_line()+theme_bw() + theme(legend.key.size=unit(1,"cm"))
I also tried to use points:
ggplot(iris,aes(Petal.Width,Petal.Length,color=Species)) + geom_line() + geom_point(size=4) + theme_bw()
But of course it still affects both plot and legend:
I wanted to use lines for the plot and dots/points for the legend.
So I'm asking about two things:
How to change width of line in the legend without changing the plot?
How to draw lines in the plot, but draw points/dots/squares in the legend?
To change line width only in the legend you should use function guides() and then for colour= use guide_legend() with override.aes= and set size=. This will override size used in plot and will use new size value just for legend.
ggplot(iris,aes(Petal.Width,Petal.Length,color=Species))+geom_line()+theme_bw()+
guides(colour = guide_legend(override.aes = list(size=3)))
To get points in legend and lines in plot workaround would be add geom_point(size=0) to ensure that points are invisible and then in guides() set linetype=0 to remove lines and size=3 to get larger points.
ggplot(iris,aes(Petal.Width,Petal.Length,color=Species))+geom_line()+theme_bw()+
geom_point(size=0)+
guides(colour = guide_legend(override.aes = list(size=3,linetype=0)))

Dividing lines between sections of bar plot with ggplot2 in r

I am using qplot to create a stacked bar plot and would like to place a white line between the sections of each bar as the blues seem to almost blend together. I do not want to change my existing colour scheme to resolve the problem. Any ideas?
library(ggplot2)
qplot(carat, data = diamonds, geom = "histogram", fill = color)
Add the argument colour="white" to create a white outline:
ggplot(mtcars, aes(factor(cyl), fill=am, group=am)) + geom_bar(colour="white")
Here is a workaround to remove the diagonal line from the legend (inspired by a posting on ggplot mailing list). The idea is to plot the geom_bar twice, once suppressing the colours:
ggplot(mtcars, aes(factor(cyl), fill=am, group=am)) +
geom_bar() +
geom_bar(colour="white", show_guide=FALSE)

Resources