ggplot2: remove some legend title [duplicate] - r

This question already has answers here:
How can I remove the legend title in ggplot2?
(6 answers)
Closed 5 years ago.
I have seen a lot of questions regarding how to remove some elements of the legend (with guides(... = FALSE) for instance, or how to remove the titles in the legend (with theme(legend.title = element_blank())) but I can't find how to remove the title of only one element in the legend.
MWE :
df = data.frame(x = 1:5, y = 2:6, col = c(1,1,1,2,2), alpha = c(1,1,2,2,3))
ggplot(df, aes(x,y)) + geom_point(aes(fill=col, alpha=alpha))
I would like for instance just to remove the alpha title.
EDIT : I know that it is possible to tweak the things manually afterwards by making something like:
p <- ggplot(df, aes(x,y)) + geom_point(aes(fill=col, alpha=alpha))
p$labels$alpha = NULL
p
but I'd like to have it in regular ggplot2 commands, without creating a variable

You can use labs():
ggplot(df, aes(x,y)) +
geom_point(aes(fill=col, alpha=alpha)) +
labs(alpha="")

Related

preserving axes label text when labels are rotated in `ggplot2` [duplicate]

This question already has answers here:
Rotating x label text in ggplot
(1 answer)
How to rotate the axis labels in ggplot2?
(2 answers)
Closed 2 years ago.
When I create a plot and change the axes labels to something other than the default (here, e.g., I am displaying no. of observations for each factor level)
# setup
set.seed(123)
library(ggplot2)
# plot
(p <-
ggplot(mtcars, aes(as.factor(am), wt)) + geom_point() +
scale_x_discrete(labels = c("0\n(n = 19)", "1\n(n = 13)")))
and then rotate the labels, the axes labels revert to the defaults:
# modify the axes label orientation
p + scale_x_discrete(guide = guide_axis(angle = 90))
#> Scale for 'x' is already present. Adding another scale for 'x', which will
#> replace the existing scale.
Is there any way I can both rotate the labels and also preserve the custom text I had entered into those labels?
P.S. And, no, this is not a duplicate of Rotating x label text in ggplot, since I am not struggling to rotate the labels (my question already includes how to do this), but to rotate labels while preserving label text. I think that's a separate issue.
Try this:
# setup
set.seed(123)
library(ggplot2)
# plot
(p <-
ggplot(mtcars, aes(as.factor(am), wt)) + geom_point() +
scale_x_discrete(labels = c("0\n(n = 19)", "1\n(n = 13)"))+
theme(axis.text.x = element_text(angle=90)))

How to Add Lines With A Facet R [duplicate]

This question already has answers here:
facet_wrap add geom_hline
(2 answers)
Closed 5 months ago.
So I have a faceted graph, and I want to be able to add lines to it that change by each facet.
Here's the code:
p <- ggplot(mtcars, aes(x=wt))+
geom_histogram(bins = 20,aes(fill = factor(cyl)))+
facet_grid(.~cyl)+
scale_color_manual(values = c('red','green','blue'))+
geom_vline(xintercept = mean(mtcars$wt))
p
So my question is, how would I get it so that the graph is showing the mean of each faceted sub-graph.
I hope that makes sense and appreciate your time regardless of your answering capability.
You can do this within the ggplot call by using stat_summaryh from the ggstance package. In the code below, I've also changed scale_colour_manual to scale_fill_manual on the assumption that you were trying to set the fill colors of the histogram bars:
library(tidyverse)
library(ggstance)
ggplot(mtcars, aes(x=wt))+
geom_histogram(bins = 20,aes(fill = factor(cyl)))+
stat_summaryh(fun.x=mean, geom="vline", aes(xintercept=..x.., y=0),
colour="grey40") +
facet_grid(.~cyl)+
scale_fill_manual(values = c('red','green','blue')) +
theme_bw()
Another option is to calculate the desired means within geom_vline (this is an implementation of the summary approach that #Ben suggested). In the code below, the . is a "pronoun" that refers to the data frame (mtcars in this case) that was fed into ggplot:
ggplot(mtcars, aes(x=wt))+
geom_histogram(bins = 20,aes(fill = factor(cyl)))+
geom_vline(data = . %>% group_by(cyl) %>% summarise(wt=mean(wt)),
aes(xintercept=wt), colour="grey40") +
facet_grid(.~cyl)+
scale_fill_manual(values = c('red','green','blue')) +
theme_bw()

How to give color to specific points in violin plot in R [duplicate]

This question already has an answer here:
ggplot2: How to specify multiple fill colors for points that are connected by lines of different colors
(1 answer)
Closed 4 years ago.
I created a violin plot in R, and I want to change part of the dots color and size. This change will be according to an attribute of True/False in the data file.
This is how I tried it:
p <- ggplot(data, aes(order,count),levels=data_levels) +
geom_point(aes(colour = actor(data$color)))+
geom_violin(draw_quantiles = c(0.5),adjust = 2,size =0.4)+
geom_jitter(height = 0, width = 0.1,size =0.6)+
coord_flip()
boolColors <- as.character(c("False"="black", "True"="red"))
boolScale <- scale_colour_manual(name="color", values=boolColors)
p1 <- p + boolScale
I tried changing the size with scale_size_manual but it didn't work.
Without the data, it is had to know exactly what is wrong. But here is an example of setting a custom colour scale for points over a violin plot.
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_violin() +
geom_jitter(height = 0, width = 0.1, aes(colour = factor(gear))) +
scale_colour_manual(name="colour", values=c("pink", "purple", "orange"))

ggplot2 does not display legends for features within geom_abline [duplicate]

This question already has an answer here:
How to show abline of geom_abline in legend
(1 answer)
Closed 7 years ago.
I am having trouble getting ggplot to display legends for colors and line types when they are only defined within an geom_abline.
I've added a very simple example below.
It generates the plot as shown (no legends).
I've also tried adding a scale_color_manual to the plot, but that did not appear to do the trick.
Does anybody know how to make ggplot2 display the legends?
library(ggplot2);
xy_data = data.frame(x=runif(100, min=-0.5, max=0.5),
y=runif(100, min=-0.5, max=0.5));
slope_data = data.frame(slope = c(-1, -0.5, 0.5, 1.0),
model = paste0("m", seq(1,4)),
robust = rep(c("Robust", "Non-robust"), 2))
merged_data = merge(xy_data, slope_data)
slope_plot =
ggplot()+
geom_point(data=xy_data, aes(x=x, y=y))+
geom_abline(data=slope_data, aes(intercept=0, slope=slope, color=model, linetype=robust))
ggsave(plot=slope_plot, file="no_legend_slope_plot.png")
You can try:
slope_plot = ggplot() +
geom_point(data=xy_data, aes(x=x, y=y)) +
geom_abline(data=slope_data, aes(intercept=0, slope=slope, color=model, linetype=robust), show_guide=TRUE)

R ggplot remove certain items from legend [duplicate]

This question already has answers here:
Turning off some legends in a ggplot
(2 answers)
Closed 4 years ago.
Is it possible to remove certain items from a legend created with ggplot? I have a plot that is faceted, and point sizes provide another dimension to the plot. Since the plot is faceted I do not need to have certain legend items since it is explained by the facet titles, but the legend is still relevant for the point size.
In the plot below I would like to remove the "AREA" legend items since it is already explained by the faceting, but keep the "TOTAL_VOLUME" legend items that explain the point sizes.
Here is the code used to generate the plot:
library(data.table) # Import libraries
library(ggplot2)
library(scales)
set.seed(1234) # Set Seed
area.list <- LETTERS[seq(1:7)] # 7 Possible areas
date.list <- seq(as.Date("2014/03/01"), by="month", length=13)
# Build a random data set
data <- data.table(AREA = sample(area.list, 80, replace=TRUE),
DATE = sample(date.list, 80, replace=TRUE),
VOLUME = rnorm(n=80, mean=100000,sd=40000),
NON_CONFORMING_VOLUME = rnorm(n=80, mean=30000,sd=5000))
# Summarise data by area and date
data <- data[, list(TOTAL_VOLUME=sum(VOLUME),
TOTAL_NC_VOLUME=sum(NON_CONFORMING_VOLUME)),
by=list(AREA, DATE)]
data$PERCENT_NC <- data$TOTAL_NC_VOLUME / data$TOTAL_VOLUME * 100
p <- ggplot(data = data, aes(x = DATE,
y = PERCENT_NC,
colour = AREA)) +
geom_point(aes(size = TOTAL_VOLUME)) +
geom_line() +
facet_grid(. ~ AREA) +
theme(legend.position="bottom", axis.text.x=element_text(angle=90,hjust=1)) +
ggtitle("Percent Non-Conforming by Area by Month") +
labs(x = "Month", y = "% Non-Conforming") +
scale_size_continuous(labels = comma)
plot(p)
I tried adding show_guide=FALSE to geom_point() but that removes both TOTAL_VOLUME and AREA.
Thank you
You can set the guide for each scale in the following way:
p + guides(size = "legend", colour = "none")

Resources