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() .
Related
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"))
I am having some trouble with ggplot and stat_summary.
Please consider following data:
head(mtcars)
data<-mtcars
data$hp2<-mtcars$hp+50
Please consider following code:
ggplot(mtcars, aes(x = cyl, y = hp)) +
stat_summary(aes(y = hp, group = 1), fun.y=mean, colour="red", geom="line",group=1) +
stat_summary(fun.y=mean, colour="red", geom="text", show_guide = FALSE, vjust=-0.7, aes( label=round(..y.., digits=0)))
The code will produce line plot with means of hp and text labels for means ans well. If we would like to add another line/curve we simply have to add:
ggplot(mtcars, aes(x = cyl, y = hp)) +
stat_summary(aes(y = hp, group = 1), fun.y=mean, colour="red", geom="line",group=1) +
stat_summary(fun.y=mean, colour="red", geom="text", show_guide = FALSE, vjust=-0.7, aes( label=round(..y.., digits=0)))+
stat_summary(aes(y = hp2), fun.y=mean, colour="blue", geom="line",group=1)
Now comes the tricky part:
How to use stat_summary with geom="text" but for the hp2 i.e. how to technically force stat_summary to calculate means on hp2 and print the text labels? It seems that I can only use it for the "main" y.
This type of problem, that asks for graphs of related vector columns, is almost always a wide-to-long data format reshaping problem.
library(ggplot2)
data_long <- reshape2::melt(data[c('cyl', 'hp', 'hp2')], id.vars = 'cyl')
head(data_long)
ggplot(data_long, aes(x = cyl, y = value, colour = variable)) +
stat_summary(fun.y = mean, geom = "line", show.legend = FALSE) +
stat_summary(fun.y = mean, geom = "text", show.legend = FALSE, vjust=-0.7, aes( label=round(..y.., digits=0))) +
scale_color_manual(values = c("red", "blue"))
I want to make a grupal frequency distribution plots and anotate the mean value. Most direct way is:
library(dplyr)
library(ggplot2)
load(mtcars)
mtcars0=mtcars%>%group_by(cyl)%>%mutate(MeanMpg=round(mean(mpg),2))
mtcars1=mtcars%>%group_by(cyl)%>%summarize(MeanMpg=round(mean(mpg),2))
p <- ggplot(mtcars0, aes(mpg, fill=cyl)) +
facet_wrap(. ~ cyl) +
geom_density(alpha=.2) +
geom_vline(data=mtcars1, aes(xintercept=MeanMpg), linetype="dashed", size=1) +
annotate("text", label = labels, size = 4, x = 15, y = 0.26)
p
the problem shows when i want to put mean values exactly on the middle of the mean line:
p <- ggplot(mtcars0, aes(mpg, fill=cyl)) +
facet_wrap(. ~ cyl) +
geom_density(alpha=.2) +
geom_vline(data=mtcars1, aes(xintercept=MeanMpg), linetype="dashed", size=1) +
annotate("text", label = labels, size = 4, x = mtcars1$MeanMpg, y = 0.26)
> p
Error: Aesthetics must be either length 1 or the same as the data (9): label
Ihn this case R multiply the text and i get an error.
How I can achive to put the label on each mean position in the facets?
You can use your mtcars1 data to specify the position of the labels:
ggplot(mtcars0, aes(mpg, fill=cyl)) +
facet_wrap(. ~ cyl) +
geom_density(alpha=.2) +
geom_vline(data = mtcars1, aes(xintercept = MeanMpg), linetype="dashed", size=1) +
geom_text(data = mtcars1, aes(x = MeanMpg, y = 0.25, label = MeanMpg))
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.
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)