Hide legend for a single geom in ggplot2 - r

I map the same variable (color) to color in two different geoms. I want them either to appear in separate legends (DHJ and EFI) or preferably just skip the second legend (for E, F, and I) altogether. Currently, R mixes the two together and gives me a legend that lists DEFHIJ in alphabetical order all mixed together.
Basically, I want to graph today's points onto some smoothed lines that use a standard dataset. I don't want there to be a legend for the smoothed lines - we are all familiar with them and they are standard on all our graphs. I just want a legend for the points only.
I've tried show.legend = FALSE as suggested elsewhere, but that doesn't seem to have an effect. guides(color = FALSE) removes the entire legend.
Reprex:
library(tidyverse)
set1 <- diamonds %>%
filter(color %in% c("D", "H", "J"))
set2 <- diamonds %>%
filter(color %in% c("E", "F", "I"))
ggplot() +
geom_point(data = set1,
aes(x = x, y = y, color = color)) +
geom_smooth(data = set2,
show.legend = FALSE,
aes(x = x, y = y, color = color))
Here is the graph that is produced. It has all 6 letters in the legend, instead of only DHJ.

If you want the legend to show only the colors from one dataset you can do so by setting the breaks in scale_color_discrete() to those values.
... +
scale_color_discrete(breaks = unique(set1$color) )
If you aren't using the colors of the lines, since this is standard background info, you could add the lines by using group ingeom_smooth() instead of color. (Also see linetype if you wanted to be able to tell the lines apart.)
ggplot() +
geom_point(data = set1,
aes(x = x, y = y, color = color)) +
geom_smooth(data = set2,
aes(x = x, y = y, group = color))

Related

ggplot2 legend discrete but the color is continuous [duplicate]

I have data with about 100 ordered categories. I would like to plot each category as a line separately, with the line colors ranging from a low value (say, blue) to a high value (say, red).
Here's some sample data, and a plot.
# Example data: normal CDFs
library(ggplot2)
category <- 1:100
X <- seq(0, 1, by = .1)
df <- data.frame(expand.grid(category, X))
names(df) <- c("category", "X")
df <- within(df, {
Y <- pnorm(X, mean = category / 100)
category <- factor(category)
})
# Plot with ggplot
qplot(data = df, x = X, y = Y, color = category, geom = "line")
This produces a pretty rainbow thing (below)
but I'd rather have a gradient from blue to red. Any ideas how I can do that?
The default gradient functions for ggplot expect a continuous scale. The easiest work around is to convert to continuous like #Roland suggested. You can also specify whatever color scale you want with scale_color_manual. You can get the list of colors ggplot would have used with
cc <- scales::seq_gradient_pal("blue", "red", "Lab")(seq(0,1,length.out=100))
This returns 100 colors from blue to red. You can then use them in your plot with
qplot(data = df, x = X, y = Y, color = category, geom = "line") +
scale_colour_manual(values=cc)
Since a discrete legend is useless anyway, you could use a continuous color scale:
ggplot(data = df, aes(x = X, y = Y, color = as.integer(category), group = category)) +
geom_line() +
scale_colour_gradient(name = "category",
low = "blue", high = "red")
Another option using scale_colour_gradientn like this:
library(ggplot2)
ggplot(data = df, aes(x = X, y = Y, color = as.integer(category), group = category)) +
geom_line() +
scale_colour_gradientn(name = 'category', colours = c('blue', 'red'))
Created on 2022-10-20 with reprex v2.0.2
The viridis scales offer this natively:
scale_color_viridis_d()

GGplot: Two stacked bar plots side by side (not facets)

I am trying to recreate this solution using ggplot2 in R: Combining two stacked bar plots for a grouped stacked bar plot
diamonds %>%
filter(color=="D"|color=="E"|color=="F") %>%
mutate(dummy=rep(c("a","b"),each=13057)) %>%
ggplot(aes(x=color,y=price))+
geom_bar(aes(fill=clarity),stat="identity",width=.25)+
facet_wrap(~cut)
I added a new variable to the diamonds dataset called dummy. dummy has two values: a and b. Let's say I want to compare these two values by creating a bar graph that has two stacked bars right next to each other (one for each value of dummy) for each value of color. How can I manipulate this such that there are two stacked bars for each value of color?
I think it would involve position dodge and/or a separate legend, but I've been unsuccessful so far. I do not want to add another facet - I want these both on the x-axis within each facet.
Similiar to the approach in the post you have linked one option to achieve your desired result would be via two geom_col and by converting the x axis variable to a numeric like so. However, doing so requires to set the breaks and labels manually via scale_x_continuous. Additionally I made use of the ggnewscale package to add a second fill scale:
library(ggplot2)
library(dplyr)
d <- diamonds %>%
filter(color == "D" | color == "E" | color == "F") %>%
mutate(dummy = rep(c("a", "b"), each = 13057))
ggplot(mapping = aes(y = price)) +
geom_col(data = filter(d, dummy == "a"), aes(x = as.numeric(color) - .15, fill = clarity), width = .3) +
scale_fill_viridis_d(name = "a", guide = guide_legend(order = 1)) +
scale_x_continuous(breaks = seq_along(levels(d$color)), labels = levels(d$color)) +
ggnewscale::new_scale_fill() +
geom_col(data = filter(d, dummy == "b"), aes(x = as.numeric(color) + .15, fill = clarity), width = .3) +
scale_fill_viridis_d(name = "b", option = "B", guide = guide_legend(order = 2)) +
facet_wrap(~cut)

ggplotly in r generates different legend from ggplot

I have created the following dataframe in R. The first step is to import the necessary libraries
library(ggplot2)
library(plotly)
library(dplyr)
We create the dataframe here as follows
DF_1<-data.frame("A"= c(1:10))
DF_1$B<-c("D", "C")
DF_1$C<-DF_1$A^2
Next we create a plot as follows
p2<-ggplot(DF_1, aes(x=A, y=C, group=B, fill=B)) +
geom_line(size=.5) + geom_ribbon(data=subset(DF_1),aes(x=A,ymax=C),ymin=0,alpha=0.3) +
scale_fill_manual(name='Legend', values=c("green4", "red"), labels=c("D", "C" ))+theme_bw()
When p2 is rendered, the legend displays correctly. When I nest p2 in ggplotly, the legend changes to two dark lines.
p3<-ggplotly(p2, dynamicTicks = T)
p3= layout(p3, xaxis = list(type = "log"))
Is it possible to retain the legends of p2 in p3. I request someone to take a look
Looks like ggplotly is a more sensible than ggplot2 in how one sets the aesthetics. Simply moving the fill aes from the global setup in ggplot into geom_ribbon gives the correct legend:
library(ggplot2)
library(plotly)
library(dplyr)
DF_1<-data.frame("A"= c(1:10))
DF_1$B<-c("D", "C")
DF_1$C<-DF_1$A^2
ggplot(DF_1, aes(x = A, y = C, group=B)) +
geom_line(size=.5) +
geom_ribbon(aes(x = A, ymin = 0, ymax = C, fill = B), alpha=0.3) +
scale_fill_manual(name='Legend', values=c("green4", "red"), labels=c("D", "C" ))+theme_bw()
ggplotly(dynamicTicks = T) %>%
layout(xaxis = list(type = "log"))

plotting stacked points using ggplot

I have a data frame and I would like to stack the points that have overlaps exactly on top of each other.
here is my example data:
value <- c(1.080251e-04, 1.708859e-01, 1.232473e-05, 4.519876e-03,2.914256e-01, 5.869711e-03, 2.196347e-01,4.124873e-01, 5.914052e-03, 2.305623e-03, 1.439013e-01, 5.407597e-03, 7.530298e-02, 7.746897e-03)
names = letters[1:7]
data <- data.frame(names = rep(names,), group = group, value = value, stringsAsFactors = T)
group <- c(rep("AA", 7) , rep("BB", 7))
I am using the following command:
p <- ggplot(data, aes(x = names, y = "", color = group)) +
geom_point(aes(size = -log(value)), position = "stack")
plot(p)
But the stacked circle outlines out of the grid. I want it close or exactly next to the bottom circle. do you have any idea how I can fix the issue?
Thanks,
The y-axis has no numeric value, so use the group instead. And we don't need the color legend now since the group labels are shown on the y-axis.
ggplot(data, aes(x = names, y = group, color = group)) +
geom_point(aes(size = -log(value))) +
guides(color=FALSE)

ggplot2 legend: combine discrete colors and continuous point size

There are similar posts to this, namely here and here, but they address instances where both point color and size are continuous. Is it possible to:
Combine discrete colors and continuous point size within a single legend?
Within that same legend, add a description to each point in place of the numerical break label?
Toy data
xval = as.numeric(c("2.2", "3.7","1.3"))
yval = as.numeric(c("0.3", "0.3", "0.2"))
color.group = c("blue", "red", "blue")
point.size = as.numeric(c("200", "11", "100"))
description = c("descript1", "descript2", "descript3")
df = data.frame(xval, yval, color.group, point.size, description)
ggplot(df, aes(x=xval, y=yval, size=point.size)) +
geom_point(color = df$color.group) +
scale_size_continuous(limits=c(0, 200), breaks=seq(0, 200, by=50))
Doing what you originally asked - continuous + discrete in a single legend - in general doesn't seem to be possible even conceptually. The only sensible thing would be to have two legends for size, with a different color for each legend.
Now let's consider having a single legend. Given your "In my case, each unique combination of point size + color is associated with a description.", it sounds like there are very few possible point sizes. In that case, you could use both scales as discrete. But I believe even that is not enough as you use different variables for size and color scales. A solution then would be to create a single factor variable with all possible combinations of color.group and point.size. In particular,
df <- data.frame(xval, yval, f = interaction(color.group, point.size), description)
ggplot(df, aes(x = xval, y = yval, size = f, color = f)) +
geom_point() + scale_color_discrete(labels = 1:3) +
scale_size_discrete(labels = 1:3)
Here 1:3 are those descriptions that you want, and you may also set the colors the way you like. For instance,
ggplot(df, aes(x = xval, y = yval, size = f, color = f)) +
geom_point() + scale_size_discrete(labels = 1:3) +
scale_color_manual(labels = 1:3, values = c("red", "blue", "green"))
However, we may also exploit color.group by using
ggplot(df, aes(x = xval, y = yval, size = f, color = f)) +
geom_point() + scale_size_discrete(labels = 1:3) +
scale_color_manual(labels = 1:3, values = gsub("(.*)\\..*", "\\1", sort(df$f)))

Resources