Removing ggplot2 legend removes whole data from the plot - r

Here I have 2-dim numeric array dataset and numeric 1-dim array of labels clustring. Then I plot it with the following code:
s = data.frame(x = dataset[,1], y = dataset[,2])
p = ggplot(s, aes(x, y))
p + geom_point(aes(colour = factor(clustering)))
which displays beautiful picture:
Now I want to remove legend completely, so here I've found possible solution:
# Remove legend for a particular aesthetic (fill)
p + guides(fill=FALSE)
# It can also be done when specifying the scale
p + scale_fill_discrete(guide=FALSE)
# This removes all legends
p + theme(legend.position="none")
but none of such commands wont help. It shows empty plot instead:
So how do I remove the legend from my plot?

Try this:
library(ggplot2)
s = data.frame(x = rnorm(20), y = rnorm(20), clustering = rep(c(1, 2), 10))
p <- ggplot(s, aes(x, y))+
guides(fill=FALSE)+
geom_point(aes(colour = factor(clustering)))+
scale_fill_discrete(guide=FALSE)+
theme(legend.position="none")
p
In your code, you are not saving the plot again after each time you add something to it. You can fix this by changing the lines that add to the plot:
# Remove legend for a particular aesthetic (fill)
p = p + guides(fill=FALSE)
But the way I wrote is is more common R formatting.

Use show.legend = FALSE within geom_point. Here is an example using ggplot2's diamonds dataset.
s <- diamonds
p <- ggplot(data = s, aes(x = depth, y = price))
p + geom_point(aes(colour = factor(cut)), show.legend = FALSE)

Just try this:
p + geom_point(aes(colour = factor(clustering)),show.legend=FALSE)

Related

How do I remove this size label on my plot while increasing the point size? [duplicate]

I have a simple data frame that I'm trying to do a combined line and point plot using ggplot2. Supposing my data looks like this:
df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20),
group=c(rep("a",10),rep("b",10)))
And I'm trying to make a plot:
g <- ggplot(df, aes(x=x, y=y, group=group))
g <- g + geom_line(aes(colour=group))
g <- g + geom_point(aes(colour=group, alpha = .8))
g
The result looks fine with one exception. It has an extra legend showing the alpha for my geom_point layer.
How can I keep the legend showing group colors, but not the one that shows my alpha settings?
Aesthetics can be set or mapped within a ggplot call.
An aesthetic defined within aes(...) is mapped from the data, and a legend created.
An aesthetic may also be set to a single value, by defining it outside aes().
In this case, it appears you wish to set alpha = 0.8 and map colour = group.
To do this,
Place the alpha = 0.8 outside the aes() definition.
g <- ggplot(df, aes(x = x, y = y, group = group))
g <- g + geom_line(aes(colour = group))
g <- g + geom_point(aes(colour = group), alpha = 0.8)
g
For any mapped variable you can supress the appearance of a legend by using guide = 'none' in the appropriate scale_... call. eg.
g2 <- ggplot(df, aes(x = x, y = y, group = group)) +
geom_line(aes(colour = group)) +
geom_point(aes(colour = group, alpha = 0.8))
g2 + scale_alpha(guide = 'none')
Which will return an identical plot
EDIT
#Joran's comment is spot-on, I've made my answer more comprehensive
Just add the show.legend = F code after the part where you don't want it.
g <- ggplot(df, aes(x=x, y=y, group=group))
g <- g + geom_line(aes(colour=group))
g <- g + geom_point(aes(colour=group, alpha = .8), show.legend = F)
Another simple option is using the function guides, which makes it possible to remove a particular aesthetic (fill, alpha, color) or multiple from the legend. Here is a reproducible example for removing only alpha legend and removing both alpha and color:
df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20),
group=c(rep("a",10),rep("b",10)))
library(ggplot2)
g <- ggplot(df, aes(x=x, y=y, group=group))
g <- g + geom_line(aes(colour=group))
g <- g + geom_point(aes(colour=group, alpha = .8))
# Remove legend alpha
g + guides(alpha = "none")
# Remove legend alpha and color
g + guides(alpha = "none", color = "none")
Created on 2022-08-21 with reprex v2.0.2
For old versions of ggplot2 (versions before 0.9.2, released in late 2012), this answer should work:
I tried this with a colour_scale and it did not work. It appears that the colour_scale_hue item works like a function with a default parameter TRUE. I added scale_colour_hue(legend=FALSE) and it worked.
I am not sure if this is the case for all color scale items in ggplot

Visualizing crosstab tables with a plot in R - changing colours

I have the following code in R which is modified from here, which plots a crosstab table:
#load ggplot2
library(ggplot2)
# Set up the vectors
xaxis <- c("A", "B")
yaxis <- c("A","B")
# Create the data frame
df <- expand.grid(xaxis, yaxis)
df$value <- c(120,5,30,200)
#Plot the Data
g <- <- ggplot(df, aes(Var1, Var2)) + geom_point(aes(size = value), colour = "lightblue") + theme_bw() + xlab("") + ylab("")
g + scale_size_continuous(range=c(10,30)) + geom_text(aes(label = value))
It produces the right figure, which is great, but I was hoping to custom colour the four dots, ideally so that the top left and bottom right are both one colour and the top right and bottom left are another.
I have tried to use:
+ scale_color_manual(values=c("blue","red","blue","red"))
but that doesn't seem to work. Any ideas?
I would suggest that you colour by a vector in your data frame, as you don't have a column that gives you this, you can either create one, or make a rule based on existing columns (which I have done below):
g <- ggplot(df, aes(Var1, Var2)) + geom_point(aes(size = value, colour = (Var2!=Var1))) + theme_bw() + xlab("") + ylab("")
g + scale_size_continuous(range=c(10,30)) + geom_text(aes(label = value))
The important part is: colour = (Var2!=Var1), note that i put this inside the aesthetic (aes) for the geom_point
Edit: if you wish to remove the legend (you annotate the chart with totals, so I guess you don't really need it), you can add: g + theme(legend.position="none") to remove it

How to get rid of the geom_text legend in ggplot? [duplicate]

I have a simple data frame that I'm trying to do a combined line and point plot using ggplot2. Supposing my data looks like this:
df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20),
group=c(rep("a",10),rep("b",10)))
And I'm trying to make a plot:
g <- ggplot(df, aes(x=x, y=y, group=group))
g <- g + geom_line(aes(colour=group))
g <- g + geom_point(aes(colour=group, alpha = .8))
g
The result looks fine with one exception. It has an extra legend showing the alpha for my geom_point layer.
How can I keep the legend showing group colors, but not the one that shows my alpha settings?
Aesthetics can be set or mapped within a ggplot call.
An aesthetic defined within aes(...) is mapped from the data, and a legend created.
An aesthetic may also be set to a single value, by defining it outside aes().
In this case, it appears you wish to set alpha = 0.8 and map colour = group.
To do this,
Place the alpha = 0.8 outside the aes() definition.
g <- ggplot(df, aes(x = x, y = y, group = group))
g <- g + geom_line(aes(colour = group))
g <- g + geom_point(aes(colour = group), alpha = 0.8)
g
For any mapped variable you can supress the appearance of a legend by using guide = 'none' in the appropriate scale_... call. eg.
g2 <- ggplot(df, aes(x = x, y = y, group = group)) +
geom_line(aes(colour = group)) +
geom_point(aes(colour = group, alpha = 0.8))
g2 + scale_alpha(guide = 'none')
Which will return an identical plot
EDIT
#Joran's comment is spot-on, I've made my answer more comprehensive
Just add the show.legend = F code after the part where you don't want it.
g <- ggplot(df, aes(x=x, y=y, group=group))
g <- g + geom_line(aes(colour=group))
g <- g + geom_point(aes(colour=group, alpha = .8), show.legend = F)
Another simple option is using the function guides, which makes it possible to remove a particular aesthetic (fill, alpha, color) or multiple from the legend. Here is a reproducible example for removing only alpha legend and removing both alpha and color:
df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20),
group=c(rep("a",10),rep("b",10)))
library(ggplot2)
g <- ggplot(df, aes(x=x, y=y, group=group))
g <- g + geom_line(aes(colour=group))
g <- g + geom_point(aes(colour=group, alpha = .8))
# Remove legend alpha
g + guides(alpha = "none")
# Remove legend alpha and color
g + guides(alpha = "none", color = "none")
Created on 2022-08-21 with reprex v2.0.2
For old versions of ggplot2 (versions before 0.9.2, released in late 2012), this answer should work:
I tried this with a colour_scale and it did not work. It appears that the colour_scale_hue item works like a function with a default parameter TRUE. I added scale_colour_hue(legend=FALSE) and it worked.
I am not sure if this is the case for all color scale items in ggplot

ggplot2: how to create correct legend after using scale_xx_manual

I have a plot with three different lines. I want one of those lines to have points on as well. I also want the two lines without points to be thicker than the one without points. I have managed to get the plot I want, but I the legend isn't keeping up.
library(ggplot2)
y <- c(1:10, 2:11, 3:12)
x <- c(1:10, 1:10, 1:10)
testnames <- c(rep('mod1', 10), rep('mod2', 10), rep('meas', 10))
df <- data.frame(testnames, y, x)
ggplot(data=df, aes(x=x, y=y, colour=testnames)) +
geom_line(aes(size=testnames)) +
scale_size_manual("", values=c(0.5,1,1)) +
geom_point(aes(alpha=testnames), size=5, shape=4) +
scale_alpha_manual("", values=c(1, 0, 0))
I can remove the second (black) legend:
ggplot(data = df, aes(x=x, y=y, colour=testnames)) +
geom_line(aes(size=testnames)) +
scale_size_manual("", values=c(0.5,1,1), guide='none') +
geom_point(aes(alpha=testnames), size=5, shape=4) +
scale_alpha_manual("", values=c(1, 0.05, 0.05), guide='none')
But what I really want is a merge of the two legends - a legend with colours, cross only on the first variable (meas) and the lines of mod1 and mod2 thicker than the first line. I have tried guide and override, but with little luck.
You don't need transparency to hide the shapes for mod1 and mod2. You can omit these points from the plot and legend by setting their shape to NA in scale_shape_manual:
ggplot(data = df, aes(x = x, y = y, colour = testnames, size = testnames)) +
geom_line() +
geom_point(aes(shape = testnames), size = 5) +
scale_size_manual(values=c(0.5, 2, 2)) +
scale_shape_manual(values=c(8, NA, NA))
This gives the following plot:
NOTE: I used some more distinct values in the size-scale and another shape in order to better illustrate the effect.

Remove extra legends in ggplot2

I have a simple data frame that I'm trying to do a combined line and point plot using ggplot2. Supposing my data looks like this:
df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20),
group=c(rep("a",10),rep("b",10)))
And I'm trying to make a plot:
g <- ggplot(df, aes(x=x, y=y, group=group))
g <- g + geom_line(aes(colour=group))
g <- g + geom_point(aes(colour=group, alpha = .8))
g
The result looks fine with one exception. It has an extra legend showing the alpha for my geom_point layer.
How can I keep the legend showing group colors, but not the one that shows my alpha settings?
Aesthetics can be set or mapped within a ggplot call.
An aesthetic defined within aes(...) is mapped from the data, and a legend created.
An aesthetic may also be set to a single value, by defining it outside aes().
In this case, it appears you wish to set alpha = 0.8 and map colour = group.
To do this,
Place the alpha = 0.8 outside the aes() definition.
g <- ggplot(df, aes(x = x, y = y, group = group))
g <- g + geom_line(aes(colour = group))
g <- g + geom_point(aes(colour = group), alpha = 0.8)
g
For any mapped variable you can supress the appearance of a legend by using guide = 'none' in the appropriate scale_... call. eg.
g2 <- ggplot(df, aes(x = x, y = y, group = group)) +
geom_line(aes(colour = group)) +
geom_point(aes(colour = group, alpha = 0.8))
g2 + scale_alpha(guide = 'none')
Which will return an identical plot
EDIT
#Joran's comment is spot-on, I've made my answer more comprehensive
Just add the show.legend = F code after the part where you don't want it.
g <- ggplot(df, aes(x=x, y=y, group=group))
g <- g + geom_line(aes(colour=group))
g <- g + geom_point(aes(colour=group, alpha = .8), show.legend = F)
Another simple option is using the function guides, which makes it possible to remove a particular aesthetic (fill, alpha, color) or multiple from the legend. Here is a reproducible example for removing only alpha legend and removing both alpha and color:
df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20),
group=c(rep("a",10),rep("b",10)))
library(ggplot2)
g <- ggplot(df, aes(x=x, y=y, group=group))
g <- g + geom_line(aes(colour=group))
g <- g + geom_point(aes(colour=group, alpha = .8))
# Remove legend alpha
g + guides(alpha = "none")
# Remove legend alpha and color
g + guides(alpha = "none", color = "none")
Created on 2022-08-21 with reprex v2.0.2
For old versions of ggplot2 (versions before 0.9.2, released in late 2012), this answer should work:
I tried this with a colour_scale and it did not work. It appears that the colour_scale_hue item works like a function with a default parameter TRUE. I added scale_colour_hue(legend=FALSE) and it worked.
I am not sure if this is the case for all color scale items in ggplot

Resources