ggplot2 - How to add additional text labels - r

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"))

Related

mean line in every facet_wrap

ggplot(data = results, aes(x = inst, y = value, group = inst)) +
geom_boxplot() +
facet_wrap(~color) +
#geom_line(data = mean,
#mapping = aes(x = inst, y = average, group = 1))
theme_bw()
When I run the code above with the code line commented, it runs and gives the graph below but I want a joining mean lines on the boxplots based on its own color category for each group in facet wraps. Any ideas how can I do that?
Your code is generally correct (though you'll want to add color = color to the aes() specification in geom_line()), so I suspect your mean dataset isn't set up correctly. Do you have means grouped by both your x axis and faceting variable? Using ggplot2::mpg as an example:
library(dplyr) # >= v1.1.0
library(ggplot2)
mean_dat <- summarize(mpg, average = mean(hwy), .by = c(cyl, drv))
ggplot(mpg, aes(factor(cyl), hwy)) +
geom_boxplot() +
geom_line(
data = mean_dat,
aes(y = average, group = 1, color = drv),
linewidth = 1.5,
show.legend = FALSE
) +
facet_wrap(~drv) +
theme_bw()
Alternatively, you could use stat = "summary" and not have to create a means dataframe at all:
ggplot(mpg, aes(factor(cyl), hwy)) +
geom_boxplot() +
geom_line(
aes(group = 1, color = drv),
stat = "summary",
linewidth = 1.5,
show.legend = FALSE
) +
facet_wrap(~drv) +
theme_bw()
# same result as above

Remove legend key from plot [duplicate]

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)

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"))

Ignoring unknown aesthetics: fill

There was a warning when entering values ​​in the boxplot using geom_text.
As a result of studying, it is judged that there is no 'fill' option in aes of geom_text.
I want to know what I should do to get rid of the warning message.
means <- aggregate(d13C~Species, data=scat, meam)
means$d13C <- round(means$d13C,2)
ggplot(data=scat,
mapping=aes(x=scat$Species, y=scat$d13C, fill=scat$Species)) +
geom_boxplot() +
stat_summary(fun.y=mean, colour='darkred', geom="point",
shape=3, size=3, show.legend=F) +
geom_text(data=means,
aes(x=means$Species, y = d13C+1, label=d13C, fill=Species))
Try the following
means <- aggregate(d13C ~ Species, data = scat, mean) # there was a typo here
means$d13C <- round(means$d13C, 2)
ggplot(data = scat, aes(x = Species, y = d13C)) +
geom_boxplot(aes(fill = Species)) +
stat_summary(
fun.y = mean,
colour = 'darkred',
geom = "point",
shape = 3,
size = 3,
show.legend = F
) +
geom_text(data = means, aes(x = Species, y = d13C + 1, label = d13C))
If not working properly, please share a minimal reproducible dataset.
A general advise: Don't write ggplot(data = scat, aes(x = scat$Species, y = scat$d13C)) + ... but use the bare column names in aes.

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)

Resources