ggrepel remove line around labels - r

How can I remove the line around geom_label_repel. Using label.size = 0 appears to have no visible effect. I could set `colour
library(ggplot2)
library(ggrepel)
ggplot(mtcars, aes(wt, mpg, color = wt)) +
geom_point(color = 'red') +
geom_label_repel(aes(label = rownames(mtcars)), label.size = 0, fill = "white") +
theme_classic(base_size = 16)
Entering a geom_text_repel after a blank geom_label_repel occasionally works, but is not reliable: the boxes may appear in a different location to the text.

As eipi10 noted in the comment, set label.size=NA:
library(ggplot2)
library(ggrepel)
ggplot(mtcars, aes(wt, mpg, color = wt)) +
geom_point(color = 'red') +
geom_label_repel(aes(label = rownames(mtcars)), label.size = NA, fill = "white") +
theme_classic(base_size = 16)

You can omit the label boxes using the geom_text_repel geom.
library(ggplot2)
library(ggrepel)
g <- ggplot(mtcars, aes(wt, mpg, color = wt)) +
geom_point(color = 'red') +
theme_classic(base_size = 16)
g + geom_label_repel(aes(label = rownames(mtcars)), fill = "white")
g + geom_text_repel(aes(label = rownames(mtcars)))
Also, according to the help page:
Currently geom_label_repel ... is considerably slower than geom_text_repel.

Related

Override legend for linechart with points

My current legend displays the shapes of the points in the chart, crossed out by the line. Id like to remove this line in the legend and just display the shapes.
The code looks like this:
p <- ggplot(data=cumdf, aes(x=quarters2)) +
geom_line(aes(y = mean_cumsum, colour='Platform participants'), size = 1.5)+
geom_point(aes(y = mean_cumsum, colour='Platform participants', shape='Platform participants'), size=3) +
geom_line(aes(y = mean_interventions, colour='Actions'), size=1.5) +
geom_point(aes(y = mean_interventions, colour='Actions', shape='Actions'), size=3) +
geom_line(aes(y = mean_sales, colour="Adopters"), size=1.5) +
geom_point(aes(y = mean_sales, colour='Adopters', shape='Adopters'), size=3) +
xlab("Quarters") +
ylab("Cumulative occurences") +
scale_shape_manual("", values=c("Platform participants" = 16, "Actions" = 17, "Adopters"=15)) +
scale_colour_manual("",breaks = c("Platform participants", "Actions", "Adopters"),
values = c ("#C80000", "#696969", "#4E33FF")) +
theme_stata(base_size = 15, base_family = "sans", scheme = "s2color") +
scale_x_continuous(n.breaks=14) +
geom_vline(xintercept=3, linetype='dashed', size=1.7)
p
Add show.legend to your geom_line. Since you have multiple calls to geom_line, you need to add it to all of them.
I'll demonstrate using mtcars, updated for a factor.
dat <- transform(mtcars, cyl = factor(cyl))
Before the change:
ggplot(dat, aes(mpg, disp, group = cyl, color = cyl, shape = cyl)) +
geom_line() +
geom_point()
Add show.legend=FALSE:
ggplot(dat, aes(mpg, disp, group = cyl, color = cyl, shape = cyl)) +
geom_line(show.legend = FALSE) +
geom_point()
The answer was found in the comments, by adding show.legend=FALSE to geom.line() .

Add Label in Line chart in R

I am trying to add labels in line graph but am unable to do so.
I want to add lable such that blue line mentiones 'model_1'; red line mentioned 'model_2' and darkgreen line mentioned 'model_3'
Attaching the code below
p1 <- ggplot(data = Auto, aes(x = horsepower, y = mpg)) +
geom_point() +
geom_line(aes(y = fitted(lm_mpg_1)), color = "blue", size = 1) +
geom_line(aes(y = fitted(lm_mpg_2)), color = "red", size = 1) +
geom_line(aes(y = fitted(lm_mpg_3)), color = "darkgreen", size = 1)
I have tried to use geom_text, geom_label and annotate function however they give me error.
The code I tried was:
p1 + geom_text(label = c('model_1','model_2','model_3'))
You don't have any data. You can use dput to share your data. In the meanwhile I have used mtcars as an example below:
# library
library(ggplot2)
# Keep 30 first rows in the mtcars natively available dataset
data=head(mtcars, 30)
# 1/ add text with geom_text, use nudge to nudge the text
ggplot(data, aes(x=wt, y=mpg)) +
geom_point() + # Show dots
geom_text(
label=rownames(data),
nudge_x = 0.25, nudge_y = 0.25,
check_overlap = T
)
ggplot(data, aes(x=wt, y=mpg)) +
geom_point() + # Show dots
geom_label(
label=rownames(data),
nudge_x = 0.25, nudge_y = 0.25,
check_overlap = T
)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p + annotate("text", x = 4, y = 25, label = "Some text")

Changing legend in geom_density

I cannot understand why the legend is not changing given the following code. The same options do the trick with geom_histogram. Thanks in advance for any assistance.
data(mtcars)
ggplot(mtcars, aes(x = disp, color = as.factor(am))) +
geom_density(aes(group = am)) +
theme_classic() +
guides(fill = guide_legend(reverse=TRUE)) +
labs(x = "Displacement", y = "Density") +
scale_fill_manual(name="",values=c("black","gray"),labels=c("Foreign","Domestic"))
You used color in your call of aes(). To modify the scale for this variable you need to use scale_color_manual and not scale_fill_manual.
It's tricky because geom_histogram does use fill, but geom_density uses color.
Working solution :
data(mtcars)
ggplot(mtcars, aes(x = disp, color = as.factor(am))) +
geom_density(aes(group = am)) +
theme_classic() +
guides(fill = guide_legend(reverse=TRUE)) +
labs(x = "Displacement", y = "Density") +
scale_color_manual(name="",values=c("black","gray"),labels=c("Foreign","Domestic"))

Remove legend ggplot 2.2

I'm trying to keep the legend of one layer (smooth) and remove the legend of the other (point). I have tried shutting off the legends with guides(colour = FALSE) and geom_point(aes(color = vs), show.legend = FALSE).
Edit: As this question and its answers are popular, a reproducible example seems in order:
library(ggplot2)
ggplot(data = mtcars, aes(x = mpg, y = disp, group = gear)) +
geom_point(aes(color = vs)) +
geom_point(aes(shape = factor(cyl))) +
geom_line(aes(linetype = factor(gear))) +
geom_smooth(aes(fill = factor(gear), color = gear)) +
theme_bw()
from r cookbook, where bp is your ggplot:
Remove legend for a particular aesthetic (fill):
bp + guides(fill="none")
It can also be done when specifying the scale:
bp + scale_fill_discrete(guide="none")
This removes all legends:
bp + theme(legend.position="none")
There might be another solution to this:
Your code was:
geom_point(aes(..., show.legend = FALSE))
You can specify the show.legend parameter after the aes call:
geom_point(aes(...), show.legend = FALSE)
then the corresponding legend should disappear
As the question and user3490026's answer are a top search hit, I have made a reproducible example and a brief illustration of the suggestions made so far, together with a solution that explicitly addresses the OP's question.
One of the things that ggplot2 does and which can be confusing is that it automatically blends certain legends when they are associated with the same variable. For instance, factor(gear) appears twice, once for linetype and once for fill, resulting in a combined legend. By contrast, gear has its own legend entry as it is not treated as the same as factor(gear). The solutions offered so far usually work well. But occasionally, you may need to override the guides. See my last example at the bottom.
# reproducible example:
library(ggplot2)
p <- ggplot(data = mtcars, aes(x = mpg, y = disp, group = gear)) +
geom_point(aes(color = vs)) +
geom_point(aes(shape = factor(cyl))) +
geom_line(aes(linetype = factor(gear))) +
geom_smooth(aes(fill = factor(gear), color = gear)) +
theme_bw()
Remove all legends: #user3490026
p + theme(legend.position = "none")
Remove all legends: #duhaime
p + guides(fill = FALSE, color = FALSE, linetype = FALSE, shape = FALSE)
Turn off legends: #Tjebo
ggplot(data = mtcars, aes(x = mpg, y = disp, group = gear)) +
geom_point(aes(color = vs), show.legend = FALSE) +
geom_point(aes(shape = factor(cyl)), show.legend = FALSE) +
geom_line(aes(linetype = factor(gear)), show.legend = FALSE) +
geom_smooth(aes(fill = factor(gear), color = gear), show.legend = FALSE) +
theme_bw()
Remove fill so that linetype becomes visible
p + guides(fill = FALSE)
Same as above via the scale_fill_ function:
p + scale_fill_discrete(guide = FALSE)
And now one possible answer to the OP's request
"to keep the legend of one layer (smooth) and remove the legend of the
other (point)"
Turn some on some off ad-hoc post-hoc
p + guides(fill = guide_legend(override.aes = list(color = NA)),
color = FALSE,
shape = FALSE)
If your chart uses both fill and color aesthetics, you can remove the legend with:
+ guides(fill=FALSE, color=FALSE)

categorize with color and size in one ggplot legend

I would like to plot some data with are categorized in ggplot by size and color. For example:
require(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(size = qsec, color = qsec)) +
scale_colour_gradient(limits=c(15, 23), low = "blue", high = "red")
How would I alter this so that both the color and size are represented in the same legend?
First of all, you need to specify guide = 'legend' within scale_color_gradient (the default is "colourbar"). And then of course you need to use the same limits for the color and the size scales.
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(size = qsec, color = qsec)) +
scale_colour_gradient(limits=c(15, 23), low = "blue", high = "red", guide = 'legend') +
scale_size_continuous(limits=c(15, 23))

Resources