How to fix a missing assignment of colour to figure in R - r

I have used the following code to generate this plot:
Cb64k <- c("#7A87A1", "#788D66",
"#885578", "#FAD09F", "#FF8A9A", "#D157A0", "#BEC459", "#456648", "#0086ED", "#886F4C",
"#34362D", "#B4A8BD", "#00A6AA", "#452C2C", "#636375", "#A3C8C9", "#FF913F", "#938A81",
"#575329", "#00FECF", "#B05B6F", "#8CD0FF", "#3B9700", "#04F757", "#C8A1A1", "#1E6E00",
"#7900D7", "#A77500", "#6367A9", "#A05837", "#6B002C", "#772600", "#D790FF", "#9B9700",
"#549E79", "#FFF69F", "#201625", "#72418F", "#BC23FF", "#99ADC0", "#3A2465", "#922329",
"#5B4534", "#FDE8DC", "#404E55", "#0089A3", "#CB7E98", "#A4E804", "#324E72", "#6A3A4C")
Species2 = group$Species
Species2 = Species2[Species2 != "Filler"]
ggplot(group, aes(x = variable, y = value, fill = Species)) +
geom_bar(position = "fill", stat = "identity") +
scale_fill_manual(breaks = Species2, values = Cb64k) +
scale_y_continuous(labels = percent_format()) +
theme(legend.position = "bottom", text=element_text(size=11),
axis.text.x = element_text(angle=0, vjust=1)) +
guides(fill = guide_legend(ncol=5)) +
facet_grid(rows=vars(Program), scales = "free_x", space = "free_x") +
ggtitle(opt$gtitle) +
xlab("Patient ID") + ylab("Relative Abundance")
As you can see Debaryomyces fabryi is included in the plot (empty segments in lower graph), however it looks like a transparent colour was assigned to it so it isn't really visible. How can I fix this?

You should try to filter in ggplot2 by subsetting your dataframe::
ggplot(subset(group, Species != "Debaryomyces fabryi"), aes(x = variable, y = value, fill = Species)) +
geom_bar(position = "fill", stat = "identity") +
scale_fill_manual(breaks = Species2, values = Cb64k) +
scale_y_continuous(labels = percent_format()) +
theme(legend.position = "bottom", text=element_text(size=11),
axis.text.x = element_text(angle=0, vjust=1)) +
guides(fill = guide_legend(ncol=5)) +
facet_grid(rows=vars(Program), scales = "free_x", space = "free_x") +
ggtitle(opt$gtitle) +
xlab("Patient ID") + ylab("Relative Abundance")
Does it answer your question ?
If not, please provide a reproducible example of your dataset by following this link: How to make a great R reproducible example

Related

How to use loop with geom_vline and facet_wrap?

I have data similar to the one I've created below:
set.seed(42)
dates <- seq.Date(as.Date("2012-08-01"), as.Date("2014-08-30"), "day")
n <- length(dates)
dat <- data.frame(date = dates,
category = rep(LETTERS[1:4], n/2),
daily_count = sample(18:100, n, replace=TRUE))
#following to be used for creating dotted lines; highlighting a certain point for each category
point_dates <- sample(seq.Date(as.Date("2012-08-01"), as.Date("2014-08-30"), "month"),4)
category_name <- list("A", "B", "C", "D")
I am creating a boxplot for each category using facet_wrap, and point_dates are important for me as they shows the point of interest in each boxplot. This is how I am creating the plot:
ggplot(dat) +
geom_boxplot(aes(y = daily_count,
x = yearmonth(date),
group = paste(yearmonth(date), category),
fill = category)) +
labs(x = 'Month & Year',
y = 'Count',
fill = "Category") +
theme_bw() +
theme(axis.text=element_text(size=10),
axis.title=element_text(size=10),
legend.position="none") +
geom_vline(xintercept = lubridate::ymd("2013-08-23"), linetype=1, colour="red", size = 0.5)+
sapply(point_dates[[1]], function(xint) geom_vline(data=filter(dat,
category==category_name[[1]]),aes(xintercept = xint),
linetype=3, colour="black", size = 1))+
sapply(point_dates[[2]], function(xint) geom_vline(data=filter(dat,
category==category_name[[2]]),aes(xintercept = xint),
linetype=3, colour="black", size = 1))+
sapply(point_dates[[3]], function(xint) geom_vline(data=filter(dat,
category==category_name[[3]]),aes(xintercept = xint),
linetype=3, colour="black", size = 1))+
sapply(point_dates[[4]], function(xint) geom_vline(data=filter(dat,
category==category_name[[4]]),aes(xintercept = xint),
linetype=3, colour="black", size = 1))+
facet_wrap(~category, nrow = 2)
And this is the output of the code:
The plot is being created just fine. My question is, is there any better way (loop may be?) that would help me get rid of writing sapply multiple times. Because the number of categories may change (increase/decrease), that would be to change the code everytime.
Any guidance please?
I'm not sure that this is the best way, but you could do all of them in one go using map2 from tidyr. This would save you time from having to write out individual sapply.
library(tidyverse)
ggplot(dat) +
geom_boxplot(aes(y = daily_count,
x = yearmonth(date),
group = paste(yearmonth(date), category),
fill = category)) +
labs(x = 'Month & Year',
y = 'Count',
fill = "Category") +
theme_bw() +
theme(axis.text=element_text(size=10),
axis.title=element_text(size=10),
legend.position="none") +
geom_vline(xintercept = lubridate::ymd("2013-08-23"),
linetype=1, colour="red", size = 0.5)+
map2(point_dates, category_name,
~geom_vline(data=filter(dat, category==.y),
aes(xintercept = .x),
linetype=3, colour="black", size = 1))+
facet_wrap(~category, nrow = 2)
You can use map() to iterate the calls to sapply():
ggplot(dat) +
geom_boxplot(aes(y = daily_count,
x = yearmonth(date),
group = paste(yearmonth(date), category),
fill = category)) +
labs(x = 'Month & Year',
y = 'Count',
fill = "Category") +
theme_bw() +
theme(axis.text=element_text(size=10),
axis.title=element_text(size=10),
legend.position="none") +
geom_vline(xintercept = lubridate::ymd("2013-08-23"), linetype=1, colour="red", size = 0.5)+
map(seq_along(unique(dat$category)), ~sapply(point_dates[[.]], function(xint) geom_vline(data=filter(dat,
category==category_name[[.]]),aes(xintercept = xint),
linetype=3, colour="black", size = 1))) +
facet_wrap(~category, nrow = 2)
If i got it correct, you have already defined the dates for each group. So make the first plot:
library(ggplot2)
library(tsibble)
g = ggplot(dat) +
geom_boxplot(aes(y = daily_count,
x = yearmonth(date),
group = paste(yearmonth(date), category),
fill = category)) +
labs(x = 'Month & Year',
y = 'Count',
fill = "Category") +
theme_bw() +
theme(axis.text=element_text(size=10),
axis.title=element_text(size=10),
legend.position="none") +
geom_vline(xintercept = lubridate::ymd("2013-08-23"), linetype=1, colour="red", size = 0.5)+
facet_wrap(~category, nrow = 2)
You just need to provide a new data frame and call geom_vline:
tmp = data.frame(category=unlist(category_name),date=point_dates)
g + geom_vline(data=tmp,aes(xintercept = date),
linetype=3, colour="black", size = 1)

Combining legends for two different aesthetics fails

library(ggplot2)
x <- data.frame(Specimen=c("A","B","C","D"), Value=rep(0.5,4),
Type=c("c1","c1","c2","c2"), Treatment=factor(rep("A", 4)),
bar=c("hot", "cold", "cold", "cold"))
list2env(split(x, x$Type), envir = .GlobalEnv)
p1 <- ggplot() +
geom_bar(data=c1, aes(x = Treatment, y = Value, fill = Specimen, colour=bar),
stat="identity", position="fill", width=0.5) +
scale_fill_manual("",values=c("gold", "green"))+
scale_color_manual("",values=c("gray40","black")) +
scale_y_continuous(expand = c(0, 0),labels = scales::percent) +
theme(legend.position = "bottom") +
coord_flip()
p2 <- ggplot() +
geom_bar(data=c2, aes(x = Treatment, y = Value, fill = Specimen),
stat="identity", position="fill", col="gray40", width=0.5) +
scale_fill_manual("",values=c("red", "blue"))+
scale_y_continuous(expand = c(0, 0),labels = scales::percent) +
theme(legend.position = "bottom",
axis.text.y=element_blank()) +
xlab("")+
coord_flip()
library(cowplot)
plot_grid(p1,p2, nrow=1, align="v")
In this example, i had to shut down the guide for color, as i couldnt combine it with the guide for fill, despite following the guidelines proposed in this question.
After turning off the guide for col in p1 (guide=F), the legends now appear to be differently drawn (one with col="gray40", the other without any border, as the col-guide is set to false):
]1
How to combine the two legends in p1?
fill and color are mapped to two different varaibles, it's only by chance that in this (trivial) case "A" is always "hot" and "B" is always "cold".
You can map both fill and color to Specimen or bar, but different variable will always result in different legends.
An alternative may be to create an interaction between the two varaibles:
library(ggplot2)
ggplot() +
geom_col(data=c1, aes(x = Treatment,
y = Value,
fill = interaction(Specimen, bar, sep = '-'),
color = interaction(Specimen, bar, sep = '-')),
position="fill", width=0.5) +
scale_fill_manual("",values=c("gold", "green")) +
scale_color_manual("",values=c("gray40", "black")) +
scale_y_continuous(expand = c(0, 0),labels = scales::percent) +
theme(legend.position = "bottom") +
coord_flip()
Created on 2018-05-08 by the reprex package (v0.2.0).

Adding an extra item to the legend

I have the following data:
trait,beta,se,p,analysis,signif
trait1,0.078,0.01,9.00E-13,group1,1
trait2,0.076,0.01,1.70E-11,group1,1
trait3,-0.032,0.01,0.004,group1,0
trait4,0.026,0.01,0.024,group1,0
trait5,0.023,0.01,0.037,group1,0
trait1,0.042,0.01,4.50E-04,group2,1
trait2,0.04,0.01,0.002,group2,1
trait3,0.03,0.01,0.025,group2,0
trait4,0.025,0.01,0.078,group2,0
trait5,0.015,0.01,0.294,group2,0
trait1,0.02,0.01,0.078,group3,0
trait2,0.03,0.01,0.078,group3,0
trait3,0.043,0.01,1.90E-04,group3,0
trait4,0.043,0.01,2.40E-04,group3,1
trait5,0.029,0.01,0.013,group3,0
And make a plot with the following code:
library(ggplot2)
ggplot(GEE, aes(y=beta, x=reorder(trait, beta), group=analysis)) +
geom_point(data = GEE[GEE$signif == 1, ],
color="red",
shape = "*",
size=12,
show.legend = F) +
geom_point(aes(color=analysis)) +
geom_errorbar(aes(ymin=beta-2*se, ymax=beta+2*se,color=analysis), width=.2,
position=position_dodge(.2)) +
geom_hline(yintercept = 0) +
theme_light() +
theme(axis.title.y=element_blank(),
legend.title=element_blank()) +
coord_flip()
Which gives me the following plot:
I would like to add an extra element to the legend, namely the red asterisk, and I want it to say "significant". How do I go about doing that?
PS. If you like this piece of code, I have another problem with it, specified here :)
Add dummy aes() to geom_point - for example fill that is named significant aes(fill = "Significant").
# Using OPs data
library(ggplot2)
ggplot(GEE, aes(y=beta, x=reorder(trait, beta), group=analysis)) +
geom_point(data = GEE[GEE$signif == 1, ],
color="red",
shape = "*",
size=12,
aes(fill = "Significant")) +
geom_point(aes(color=analysis)) +
geom_errorbar(aes(ymin=beta-2*se, ymax=beta+2*se,color=analysis), width=.2,
position=position_dodge(.2)) +
geom_hline(yintercept = 0) +
theme_light() +
theme(axis.title.y=element_blank(),
legend.title=element_blank()) +
coord_flip() +
guides(colour = guide_legend(order = 1),
fill = guide_legend(override.aes = list(size = 5))) +
theme(legend.margin = margin(-0.5,0,0,0, unit="cm"))
PS: I also removed show.legend = F from asterik geom_point

ggplot - Remove alpha legend

I have created a plot with the following dataset:
Locus;Island;AR;Type;Shapetype
MS1;ST;4,6315;MS;NA
MS1;FG;3,9689;MS;NA
MS1;SN;3;MS;NA
MS2;ST;2;MS;NA
MS2;FG;2;MS;NA
MS2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
TLR5;SN;2;TLR;TLR5
TLR21;ST;2,91;TLR;TLR21
TLR21;FG;1;TLR;TLR21
TLR21;SN;1;TLR;TLR21
Here's the code for the plot:
ggplot(comb, aes(Island, AR, group = Locus, colour = (factor(Type)))) +
geom_line(aes(colour = factor(Type), alpha = factor(Type), size = factor(Type))) +
scale_alpha_manual(values = c("MS"=0.2, "MT"=0.2, "TLR" = 1)) +
scale_size_manual(values = c("MS"=0.5, "MT"=0.5, "TLR" = 0.3)) +
xlab("Island") +
ylab("Allelic Richness") +
scale_x_discrete(labels = c("Santiago", "Fogo", "Sao Nicolau"),
limits = c("ST", "FG", "SN")) +
geom_point(aes(shape = (factor(Shapetype)))) +
scale_shape_manual(values = c(1,2,3,4,5,6,7,8,9,10),
breaks=c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3",
"TLR4", "TLR5","TLR21", "MS", "MT")) +
scale_colour_manual(values = c("Red","Blue","Black"),
breaks=c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3",
"TLR4","TLR5","TLR21", "MS", "MT")) +
theme_bw() +
labs(shape="Functional", colour="Neutral")
The plot is okay, however, I need to remove the legend that is created for the alpha values. I have tried to use both + scale_alpha(guide = 'none')and guide = 'none', but none of them seem to work (I may be placing them in the wrong places, though). I suspect that they do not work, because of the manual adjustment of the alpha values.
Please be aware that this is not a minimal example.
Please note that your alpha legend is also your size legend, but this is very hard to see since your sizes are very similar. Set guide = 'none' in both scale_alpha_manual and scale_size_manual to remove that portion of the legend.
If you only do it in scale_alpha_manual you can actually see that the alpha becomes 1 for those lines, so it works as intended. So #Thierry's answer is correct.
Full code
ggplot(comb, aes(Island, AR, group = Locus, colour = (factor(Type)))) +
geom_line(aes(colour = factor(Type), alpha = factor(Type), size = factor(Type))) +
scale_alpha_manual(values = c("MS"=0.2, "MT"=0.2, "TLR" = 1), guide = 'none') +
scale_size_manual(values = c("MS"=0.5, "MT"=0.5, "TLR" = 0.3), guide = 'none') +
xlab("Island") +
ylab("Allelic Richness") +
scale_x_discrete(labels = c("Santiago", "Fogo", "Sao Nicolau"),
limits = c("ST", "FG", "SN")) +
geom_point(aes(shape = (factor(Shapetype)))) +
scale_shape_manual(values = c(1,2,3,4,5,6,7,8,9,10),
breaks=c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3",
"TLR4", "TLR5","TLR21", "MS", "MT")) +
scale_colour_manual(values = c("Red","Blue","Black"),
breaks=c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3",
"TLR4","TLR5","TLR21", "MS", "MT")) +
theme_bw() +
labs(shape="Functional", colour="Neutral")
Result
(Note that my y-axis is wrong because your data includes comma's and I was lazy.)
guide = "none" should do the trick
ggplot(
comb,
aes(Island, AR, group = Locus, colour = (factor(Type)))
) +
geom_line(aes(alpha = factor(Type), size = factor(Type))) +
geom_point(aes(shape = factor(Shapetype))) +
scale_x_discrete(
"Island",
labels = c("Santiago", "Fogo", "Sao Nicolau"),
limits = c("ST", "FG", "SN")
) +
ylab("Allelic Richness") +
scale_alpha_manual(values = c("MS"=0.2, "MT"=0.2, "TLR" = 1), guide = "none") +
scale_size_manual(values = c("MS"=0.5, "MT"=0.5, "TLR" = 0.3)) +
scale_shape_manual(
"Functional",
values = c(1,2,3,4,5,6,7,8,9,10),
breaks = c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3","TLR4","TLR5","TLR21", "MS", "MT")
) +
scale_colour_manual(
"Neutral",
values = c("Red","Blue","Black"),
breaks = c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3","TLR4","TLR5","TLR21", "MS", "MT")
) +
theme_bw()
Starting with this basic plot
bp <- df %>%
ggplot(aes(column_of_interest, alpha = 0.25)) + geom_density()
from r cookbook, where bp is your ggplot -
Remove legend for a particular aesthetic (alpha):
bp + guides(alpha="none")

to show mean value in ggplot box plot

I need to be able to show the mean value in ggplot box plot. Below works for a point but I need the white dashed lines? Any body help?
x
Team Value
A 10
B 5
C 29
D 35
ggplot(aes(x = Team , y = Value), data = x)
+ geom_boxplot (aes(fill=Team), alpha=.25, width=0.5, position = position_dodge(width = .9))
+ stat_summary(fun.y=mean, colour="red", geom="point")
Here's my way of adding mean to boxplots:
ggplot(RQA, aes(x = Type, y = engagementPercent)) +
geom_boxplot(aes(fill = Type),alpha = .6,size = 1) +
scale_fill_brewer(palette = "Set2") +
stat_summary(fun.y = "mean", geom = "text", label="----", size= 10, color= "white") +
ggtitle("Participation distribution by type") +
theme(axis.title.y=element_blank()) + theme(axis.title.x=element_blank())
ggplot(df, aes(x = Type, y = scorepercent)) +
geom_boxplot(aes(fill = Type),alpha = .6,size = 1) +
scale_fill_brewer(palette = "Set2") +
stat_summary(fun.y = "mean", geom = "point", shape= 23, size= 3, fill= "white") +
ggtitle("score distribution by type") +
theme(axis.title.y=element_blank()) + theme(axis.title.x=element_blank())
I would caution against using text to this and do geom_line instead as text is offset slightly and gives the wrong portrayal of the mean.
Hey user1471980, I think people are more inclined to help if you have a unique user name but then again you have a lot of points :)
this is a hack but does this help:
Value<-c(1,2,3,4,5,6)
Team<-c("a","a","a","b","b","b")
x<-data.frame(Team,Value) #note means for a=2, mean for b=5
ggplot(aes(x = Team , y = Value), data = x) + geom_boxplot (aes(fill=Team), alpha=.25, width=0.5, position = position_dodge(width = .9)) +
annotate(geom="text", x=1, y=2, label="----", colour="white", size=7, fontface="bold", angle=0) +
annotate(geom="text", x=2, y=5, label="----", colour="white", size=7, fontface="bold", angle=0)

Resources