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
Related
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
I´m trying to get just one legend with shape that have the same color as the graph but it is just in black color:
type1 <-c("tmax","tmax","tmax","tmin","tmin","tmin","tmax","tmax","tmax","tmin","tmin","tmin")
station1 <-c("Anda","Anda","Anda","Anda","Anda","Anda","Mach","Mach","Mach","Mach","Mach","Mach")
date1 <-c(2001,2002,2003,2001,2002,2003,2002,2003,2004,2002,2003,2004)
meanTemp1<-c(15,16,15.5,5,7,8,13,14,12,9,9,7)
data11 <- data.frame(type1,station1,date1,meanTemp1)
plot1<- ggplot(data11, aes(x=date1, y=meanTemp1,group = station1,colour=station1,shape=station1)) +
geom_line () + guides(colour=FALSE)+
geom_point() +
xlab("year") + ylab("°C") +
labs(shape = "Station")+
facet_wrap(~type1,scales = "free")+
theme(axis.text.x = element_text(angle = 60,hjust = 1))
plot1
How can I get the legend fill with the same color as the graph instead of "black"?
As you rename shape legend in labs, you also need to rename colour legends using the same name in order they get merge.
Instead of using guides(colour = FALSE), you can pass in geom_line, the argument show.legend = FALSE to remove the colored lines in the legend:
plot1<- ggplot(data11, aes(x=date1, y=meanTemp1, group = station1,
colour=station1,
shape=station1)) +
geom_line (show.legend = FALSE) +
geom_point() +
xlab("year") + ylab("°C") +
labs(shape = "Station", colour = "Station")+
facet_wrap(~type1,scales = "free")+
theme(axis.text.x = element_text(angle = 60,hjust = 1))
plot1
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).
EDITED
I have the following example where I create 3 pie charts , but I would like to have them 3 combined into 1 pie + donuts pie.
Besides, it would be really useful to have the numbers as well, how can this be accomplished? Thanks a lot.
df.mut <- data.frame(Avrg.muts.HLA.A11.A24=c(20.20000,37.39286,11.85714,50.26087,20.20000,37.39286,11.85714,50.26087,20.20000,37.39286,11.85714,50.26087), Avrg.muts.HLA.A11=c(32.86842,32.86842,35.72973,35.72973,32.86842,32.86842,35.72973,35.72973,32.86842,32.86842,35.72973,35.72973), Avrg.muts.HLA.A24=c(15.33333,43.19608,15.33333,43.19608,15.33333,43.19608,15.33333,43.19608,15.33333,43.19608,15.33333,43.19608), variable=c("HLA.A11.A24","HLA.A11.A24","HLA.A11.A24","HLA.A11.A24","HLA.A11","HLA.A11","HLA.A11","HLA.A11","HLA.A24","HLA.A24","HLA.A24","HLA.A24"), value=c("+/+","+/-","-/+","-/-","+","+","-","-","+","-","+","-"))
df.mut$variable <- factor(df.mut$variable, levels=unique(df.mut$variable))
png(file="IMAGES/test1.png")
print(
ggplot(df.mut, aes(x="")) +
facet_grid(variable~., scales="free_y") +
geom_bar(data=subset(df.mut, variable=='HLA.A11.A24'),
aes(x='0', y=Avrg.muts.HLA.A11.A24, fill=value), width = 1, stat = "identity") +
geom_bar(data=subset(df.mut, variable=='HLA.A11'),
aes(x='1', y=Avrg.muts.HLA.A11, fill=value), width = 1, stat = "identity") +
geom_bar(data=subset(df.mut, variable=='HLA.A24'),
aes(x='2', y=Avrg.muts.HLA.A24, fill=value), width = 1, stat = "identity") +
ggtitle("TEST1") +
theme(axis.text.x=element_blank(), legend.title=element_blank(), legend.position="right", legend.background=element_blank(), legend.box.just="left", plot.title=element_text(size=15, face="bold", colour="black", vjust=1.5)) +
scale_y_continuous(name="") +
scale_x_discrete(name="") +
coord_polar(theta="y")
)
dev.off()
This produces the following image:
However, when I try to having the 3 of them together, the best I get is this mess:
How can I combine the pie charts above? And include numbers.
This should get you started:
df.test <- data.frame(genotype.1=c("+","+","-","-"), genotype.2=c("+","-","+","-"), count=c(345,547,678,987))
require(ggplot2)
require(grid)
ggplot(df.test, aes(y = count)) +
geom_bar(aes(x='0', fill = paste(genotype.1, genotype.2, sep="/")), color='black', width = 1, stat = "identity") +
geom_bar(aes(x='1', fill = genotype.1), width = 1, color='black', stat = "identity") +
geom_bar(aes(x='2', fill = genotype.2), width = 1, color='black', stat = "identity") +
coord_polar(theta="y") +
scale_x_discrete(name='', breaks=c('0', '1', '2'), labels=rep('', 3)) +
theme(axis.ticks.length = unit(0, "npc")) +
scale_fill_discrete(name='genotype', breaks = c('-', '+', '-/-', '-/+', '+/-', '+/+')) +
scale_y_continuous(breaks=0)
EDIT: Part of the reason, you get something different with faceting than without is because you use scales="free_y". To get the same thing without the facets, you can do scale the variables yourself.
p <- ggplot(df.mut, aes(x="")) +
geom_bar(data=subset(df.mut, variable=='HLA.A11.A24'),
aes(x='0', y=Avrg.muts.HLA.A11.A24/sum(Avrg.muts.HLA.A11.A24), fill=value), color='black', width = 1, stat = "identity") +
geom_bar(data=subset(df.mut, variable=='HLA.A11'),
aes(x='1', y=Avrg.muts.HLA.A11/sum(Avrg.muts.HLA.A11), fill=value), color='black', width = 1, stat = "identity") +
geom_bar(data=subset(df.mut, variable=='HLA.A24'),
aes(x='2', y=Avrg.muts.HLA.A24/sum(Avrg.muts.HLA.A24), fill=value), color='black', width = 1, stat = "identity") +
ggtitle("TEST1") +
theme(axis.text.x=element_blank(), legend.title=element_blank(), legend.position="right", legend.background=element_blank(), legend.box.just="left", plot.title=element_text(size=15, face="bold", colour="black", vjust=1.5)) +
scale_y_continuous(name="") +
scale_x_discrete(name="") +
coord_polar(theta="y")
# now look at the faceted and unfaceted plots...
p
p + facet_grid(variable~., scales="free_y")
However, your faceted plots also don't line up as nicely as your previous test data did. That just appears to be because the data is actually not exactly lined up (there are really only 2 unique values for the HLA.A11 and HLA.A24, so it's impossible to get 4 different sizes).
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)