How can I change the black facet labels (A and B) into the color of factors (A and B) red and blue respectively ? There are plenty of examples on how to change the background label colour but not the text color.
library(data.table)
A = data.table(x = 1:4, y = 1:4, z = c('A','A','B','B'))
ggplot(A) + geom_point(aes(x = x, y = y, color = z)) + facet_grid(~z) + theme_bw()
One option to achieve your desired result would be via the ggtext package which via element_markdown allows to style theme elements via HTML, CSS and markdown:
library(ggplot2)
library(ggtext)
cols <- data.frame(
z = c("A", "B"),
color = scales::hue_pal()(2)
)
A <- merge(A, cols, by = "z", all.x = TRUE)
A$facet <- paste0("<span style='color: ", A$color, "'>", A$z, "</span>")
ggplot(A) +
geom_point(aes(x = x, y = y, color = z)) +
facet_grid(~facet) +
theme_bw() +
theme(strip.text.x = ggtext::element_markdown(face = "bold"))
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
As you can see on the image, R automatically assigns the values 0, 0.25... 1 for the size of the point. I was wondering if I could replace the 0, 0.25... 1 and make these text values instead while keeping the actual numerical values from the data.
library(ggplot2)
library(scales)
data(SLC4A1, package="ggplot2")
SLC4A1 <- read.csv(file.choose(), header = TRUE)
# bubble chart showing position of polymorphisms on gene, the frequency of each of these
# polymorphisms, where they are prominent on earth, and p-value
SLC4A1ggplot <- ggplot(SLC4A1, aes(Position, log10(Frequency)))+
geom_jitter(aes(col=Geographical.Location, size =(p.value)))+
labs(subtitle="Frequency of Various Polymorphisms", title="SLC4A1 Gene") +
labs(color = "Geographical Location") +
labs(size = "p-value") + labs(x = "Position of Polymorphism on SLC4A1 Gene") +
scale_size_continuous(range=c(1,4.5), trans = "reverse") +
guides(size = guide_legend(reverse = TRUE))
library(tidyver)
df <- data.frame(x = 1:5, y = 1:5,z = 1:5)
ggplot(df,aes(x = x, y = y, size = z)) +
geom_point()
ggplot(df,aes(x = x, y = y, size = z)) +
geom_point() +
scale_size_continuous(range = 1:2) # control range of circle size
See more here:
https://ggplot2.tidyverse.org/reference/scale_size.html
I want to change the background inside guides geometry (those in gray that are signaled by the red arrows)
library(ggplot2)
dat <- data.frame(x = 1:5, y = 1:5, p = 1:5, q = factor(1:5),
r = factor(1:5))
p <- ggplot(dat, aes(x, y, colour = p, size = q, shape = r)) + geom_point()
p
ggplot(dat, aes(x, y, colour = p, size = q, shape = r)) +
geom_point() +
theme(legend.key = element_rect(fill = "green"))
I'm plotting 11 curves and the program bellow works well. BUT I'm not able two change the wild colors to plot 11 black curves:
library(ggplot2)
#library(latex2exp)
library(reshape)
fn <- "img/plot.eps"
fct1 <- function(x0 ){
return(1/sin(x0)+1/tan(x0))
}
fct2 <- function(beta, t ){
return(2*atan(exp(t)/beta))
}
t<-seq(from=0,to=10,by=0.01)
s1<-cbind(t, fct2(fct1(-pi+0.0001),t),
fct2(fct1(-1.5),t),
fct2(fct1(-0.5),t),
fct2(fct1(-0.05),t),
fct2(fct1(-0.01),t),
fct2(fct1(0),t),
fct2(fct1(0.01),t),
fct2(fct1(0.05),t),
fct2(fct1(0.5),t),
fct2(fct1(1.5),t),
fct2(fct1(pi),t))
colnames(s1)<-c("time","y1","y2","y3","y4","y5","y6","y7","y8","y9","y10","y11")
s2 <- melt(as.data.frame(s1), id = "time")
q <- ggplot(s2, aes(x = time, y = value, color = variable))
q <- q + geom_line() + ylab("y") + xlab("t")+ ylab("x(t)")+
theme_bw(base_size = 7) + guides(colour = FALSE)
ggsave(file = fn, width = 2, height = 1)
q
EDIT Now the code should be reproducible
You need to map the variable to the grouping, and it will produce black lines by default.
q <- ggplot() +
geom_line(data = s2, aes(x = time, y = value,
group = variable)) +
xlab("t")+ ylab("x(t)") +
theme_bw(base_size = 7) + guides(colour = FALSE)
q
To be perfectly clear, it is possible to map the color to the variable, which can produce black lines, but not without changing the legend. Here is how you would amend the colors after the fact, if you wanted to, having already mapped the color to the variable.
q <- ggplot() +
geom_line(data = s2, aes(x = time, y = value,
color = variable)) +
xlab("t")+ ylab("x(t)") +
theme_bw(base_size = 7) + guides(colour = FALSE) +
scale_color_manual(values = rep("black",11))
q
This R code produces a ggplot2 graph in which the legend key contains the letter "a" repeated in red, blue and green.
x <- rnorm(9); y <- rnorm(9); s <- rep(c("F","G","K"), each = 3)
df <- data.frame(x, y, s)
require(ggplot2)
ggplot(df, aes(x = x, y = y, col = s, label = s)) +
geom_text() +
scale_colour_discrete(name = "My name", breaks = c("F","K","G"), labels = c("Fbig","Kbig","Gbig"))
I would like to replace the repeated "a" in the legend key with "F", "K" and "G".
Is this possible please? Thank you.
Adapting code for this answer:
The idea is to inhibit the geom_text legend, but to allow a legend for geom_point, but make the point size zero so the points are not visible in the plot, then set size and shape of the points in the legend in the guides statement
x <- rnorm(9); y <- rnorm(9); s <- rep(c("F","G","K"), each = 3)
df <- data.frame(x, y, s)
#
require(ggplot2)
#
ggplot(df, aes(x = x, y = y, colour = s, label = s)) +
geom_point(size = 0, stroke = 0) + # OR geom_point(shape = "") +
geom_text(show.legend = FALSE) +
guides(colour = guide_legend(override.aes = list(size = 5, shape = c(utf8ToInt("F"), utf8ToInt("K"), utf8ToInt("G"))))) +
scale_colour_discrete(name = "My name", breaks = c("F","K","G"), labels = c("Fbig","Kbig","Gbig"))
to manually rename the legend add
+ scale_x_continuous(breaks=c(x1,x2,x3), labels=c("F", "K", "G"))
where x1,x2,x3 are the point number