I have this plot
dat = data.frame(x = c(55,56) , y = c(200,300))
ggplot(dat, aes(x = factor(x, ordered = TRUE), y =y)) +
geom_bar(stat = "identity")+
geom_vline(aes(xintercept = 55, linetype = "solid" ), color = "red")
The vertical line is not appearing. How can I make it appear over the bars? Thank you.
Related
I am confused of this problem for a long time. A simple data frame is constructed as follows
data <- data.frame(
x = 1:5,
y = 5:1,
fill = c(rep("pink", 3), rep("blue", 2)),
shape = c(rep(21, 3), rep(22, 2))
)
Suppose I wand to show the legend of the fill
uniFill <- unique(data$fill)
p <- ggplot(data,
mapping = aes(x = x,
y = y,
fill = fill)) +
geom_point(shape = data$shape) +
# show legend so that I do not call `scale_fill_identity()`
scale_fill_manual(values = uniFill,
labels = uniFill,
breaks = uniFill)
p
The graphics are OK, however, the legend is not correct
I guess, maybe different shapes (21 to 25) cannot be merged? Then, I partition the data into two subsets where the first set has shape 21 and the second has shape 22.
data1 <- data[1:3, ]
data2 <- data[4:5, ]
# > data1$shape
# [1] 21 21 21
# > data2$shape
# [1] 22 22
ggplot(mapping = aes(x = x,
y = y,
fill = fill)) +
geom_point(data = data1, shape = data1$shape) +
geom_point(data = data2, shape = data2$shape) +
scale_fill_manual(values = uniFill,
labels = uniFill,
breaks = uniFill)
Unfortunately, the legend does not change. Then, I changed the shape from a vector to a scalar, as in
ggplot(mapping = aes(x = x,
y = y,
fill = fill)) +
geom_point(data = data1, shape = 21) +
geom_point(data = data2, shape = 22) +
scale_fill_manual(values = uniFill,
labels = uniFill,
breaks = uniFill)
The legend of the fill color is correct finally...
So what happens here? Is it a bug? Is it possible to just add a single layer but with different shapes (21 to 25)?
A possible solution is that one can add component guides(), as in
p +
guides(fill = guide_legend(override.aes = list(fill = uniFill,
shape = 21)))
But I am more interested in why p does not work (legend)
The main reason your legend is not working in your first example is because you did not put your shape in the aesthetics.
I have a couple other suggestions: Do not define colors in your data frame; instead define a column to change the aesthetics using a code. Then define your fill and shape values explicitly. Each of the scales needs to have the same name - in this case "Legend."
Give this edit a try.
data <- data.frame(
x = 1:5,
y = 5:1,
fill = c(rep("p", 3), rep("b", 2))
)
uniFill <- c("p"="pink", "b"="blue")
uniShape <- c("p" = 21, "b" = 22)
p <- ggplot(data,
mapping = aes(x = x,
y = y,
fill = fill,
shape = fill)) +
geom_point() +
# show legend so that I do not call `scale_fill_identity()`
scale_fill_manual("Legend",values = uniFill,
labels = uniFill)+
scale_shape_manual("Legend",values = uniShape,
labels = uniFill)
p
(edit) If your fill and shape aesthetics do not match up, I don't see any other way than to use guides and two legends. Notice that if your attribute column is descriptive, you do not need to set the labels and your code will be cleaner (see shape vs fill aesthetics).
data <- data.frame(
x = 1:5,
y = 5:1,
fill = c(rep("p", 3), rep("b", 2)),
shape = c(rep("circles", 2), rep("squares", 3))
)
uniFill <- c("p"="pink", "b"="blue")
uniShape <- c("circles" = 21, "squares" = 22)
p <- ggplot(data,
mapping = aes(x = x,
y = y,
fill = fill,
shape = shape)) +
geom_point() +
# show legend so that I do not call `scale_fill_identity()`
scale_fill_manual("Legend fill",values = uniFill,
labels = uniFill)+
scale_shape_manual("Legend shape",values = uniShape )+
guides(fill = guide_legend("Legend fill", override.aes = list(shape = 21)))
p
In a chart I have columns and points. I'm trying to unify the legend; I've already put the same name on the scales, but they still appear separate. Could someone please help me solve this issue?
library(ggplot2)
X <- factor(c("a", "b"))
Y1 <- c(10, 15)
Y2 <- c(22, 23)
df <- data.frame(X, Y1, Y2)
ggplot(data = df, aes(x = X,
group = 1)) +
geom_col(aes(y = Y1,
fill = "Y1")) +
geom_line(aes(y = Y2,
color = "Y2")) +
geom_point(aes(y = Y2,
color = "Y2")) +
scale_fill_manual(name = "Legend",
values = "blue") +
scale_color_manual(name = "Legend",
values = "red")
To merge the legends you also have to use the same values in both the fill and color scale. Additionally you have to tweak the legend a bit by removing the fill color for the "Y2" legend key using the override.aes argument of guide_legend:
EDIT Thanks to the comment by #aosmith. To make this approach work for ggplot2 version <=3.3.3 we have to explicitly set the limits of both scales.
library(ggplot2)
X <- factor(c("a", "b"))
Y1 <- c(10, 15)
Y2 <- c(22, 23)
df <- data.frame(X, Y1, Y2)
ggplot(data = df, aes(x = X,
group = 1)) +
geom_col(aes(y = Y1,
fill = "Y1")) +
geom_line(aes(y = Y2,
color = "Y2")) +
geom_point(aes(y = Y2,
color = "Y2")) +
scale_fill_manual(name = "Legend",
values = c(Y1 = "blue", Y2 = "red"), limits = c("Y1", "Y2")) +
scale_color_manual(name = "Legend",
values = c(Y1 = "blue", Y2 = "red"), limits = c("Y1", "Y2")) +
guides(color = guide_legend(override.aes = list(fill = c("blue", NA))))
Another option would to make only one of the legends and then manually change that legend to have it represent all the layers.
In this case, I make a fill legend but set color to constants outside of aes(). I force the inclusion of the line/point layers by adding show.legend = TRUE.
Then I add the extra value to fill, setting both values and limits in scale_fill_manual(). Note I set the Y2 to have a transparent fill.
Finally I make manual changes to the legend to keep the lines and points only for the second legend key with override.aes in guide_legend().
ggplot(data = df, aes(x = X,
group = 1)) +
geom_col(aes(y = Y1,
fill = "Y1")) +
geom_line(aes(y = Y2), color = "red", show.legend = TRUE) +
geom_point(aes(y = Y2), color = "red", show.legend = TRUE) +
scale_fill_manual(name = "Legend",
values = c(Y1 = "blue", Y2 = "transparent"),
limits = c("Y1", "Y2")) +
guides(fill = guide_legend(override.aes = list(shape = c(NA, 19),
linetype = c(0, 1))))
Created on 2021-06-29 by the reprex package (v2.0.0)
I've got a bubble grid chart created but I can't for the life of my change the colors of the fill. I want to use a rainbow gradient based on the values. Below is my code and I've attached image out my output
setwd("C:/Users/Schelly/Desktop/Projects/Jens_tables_and_figures_2020/Bubble_chart")
library(tidyverse)
library(reshape2)
pc <- read.csv("Para_Bubble_data2.csv", header = TRUE)
head(pc)
pcm<-melt(pc, id = c("Sample"))
pcm$Sample <- factor(pcm$Sample,levels=unique(pcm$Sample))
xx = ggplot(pcm, aes(x = Sample, y = variable)) +
geom_point(aes(size = value, fill = value), alpha = 0.75, shape = 21) +
scale_colour_gradientn(colours=rainbow(4))+
scale_size_continuous(limits = c(0.000001, 1), range = c(1,17), breaks = c(.01,.10,.50,.75)) +
labs( x= "", y = "", size = "Relative Abundance (%)", fill = "")
xx
Output of code
You need to specify aes(colour = value) if you want to use scale_color_gradientn:
library(ggplot2)
df <- data.frame(x = factor(rep(1:5, each = 6)),
y = factor(rep(1:6, 5)), val = sample(30))
ggplot(df, aes(x = x, y = y, size = val, colour = val)) +
geom_point() +
scale_color_gradientn(colours = c("red", "yellow", "blue"))
If you want to use fill (to preserve a different outline colour), you need to use scale_fill_gradientn:
ggplot(df, aes(x = x, y = y, size = val)) +
geom_point(aes(size = val, fill = val), alpha = 0.75, shape = 21) +
scale_fill_gradientn(colours = rainbow(4))+
labs( x= "", y = "", size = "Relative Abundance (%)", fill = "")
I have this plot
dat = data.frame(group = rep("A",3),subgroup= c("B","C","D"), value= c(4,5,6),avg = c(4.5,4.5,4.5))
ggplot(dat, aes(x= group, y =value, color = fct_rev(subgroup) ))+
geom_point()+
geom_point(data = dat ,aes(x = group, y = avg), color = "blue",pch = 17, inherit.aes = FALSE)
I need to show 2 legends: 1 for the fct_rev(subgroup) which I already there but there is no legend for "avg".
How can i add a legend that is a blue triangle pch 17 with the title "avg?
thank you
Maybe like this?
ggplot(dat, aes(x= group, y =value, color = fct_rev(subgroup) ))+
geom_point()+
geom_point(data = dat ,aes(x = group, y = avg,shape = "Mean"),
color = "blue", inherit.aes = FALSE) +
scale_shape_manual(values = c('Mean' = 17))
Using data from original post.
Legends do not work like that in ggplot. Why not add a geom_text at the average? I see that you have a column with the average being repeated. This seems like a bad way to handle the data, but irrelevant right now.
My proposed solution:
ggplot(dat)+
geom_point(aes(x= group, y =value, color = subgroup))+
geom_point(aes(x = group, y = avg), color = "blue",pch = 17, inherit.aes = FALSE) +
geom_text(aes(x=1, y = 4.5), label = "avg", nudge_x = .1)
You could also add a hline to symbolize the average, which would aesthetically look nicer.
I have used geom_point in ggplot2 to display values as the area of each point:
geom_point(aes(size = sqrt(z/pi))
However, the legend units are the transformed values, is it possible to have the legend display the original values alongside their respective bubble size?
Edit: sorry I should have provided more information to begin with
library(ggplot2)
data <- data.frame(x = sample(1:10), y = sample(1:10), z = sample(1:10), colour = c("red", "yellow", "green","pink","black","brown","grey","white","purple","beige"))
ggplot(data, aes(x = x, y = y)) + geom_point(aes(size = sqrt(z/pi)), pch = 21) + aes(fill = colour) + scale_fill_brewer(palette = "set1")
Try adding:
+scale_colour_manual(guide = guide_legend(override.aes=aes(size=values)))