I am trying to make a stacked and grouped barplot using the following datasets:
dfplot <- data.frame(organisms=c("M.musculus","D.melanogaster" ,"H.sapiens","O.sativa","S.pombe","C.familiaris",
"G.gallus","P.falciparum","A.thaliana","C.elegans","D.rerio","B.taurus","S.cerevisiae","R.norvegicus","C.intestinalis","B.subtilis","E.coli"),
KEGGv2=c(20,7,21,126,106,62,26,80,5,5,13,306,8,35,32,104,107),
KEGGv1=c(286,124,289,0,0,244,135,0,121,124,148,0,101,271,87,0,0),
Reactome=c(358,146,596,115,54,306,370,23,155,112,365,341,52,364,0,0,0))
# Melt the dataframe
melted <- melt(dfplot, "organisms")
# Reformat the data labels
melted$cat <- ''
melted[melted$variable == 'Reactome',]$cat <- "Reactome"
melted[melted$variable != 'Reactome',]$cat <- "KEGG"
ggplot(melted, aes(x=cat, y=value, fill=variable)) +
geom_bar(stat = 'identity', position = 'stack') +
facet_grid(~ organisms)+
scale_fill_manual(values=c("deepskyblue4", "lightblue1", "olivedrab2")) +
labs(y = "Number of Pathways") +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.title=element_blank(),
strip.text.x = element_blank(),
strip.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"))
And my output is the following one:
I used a facet_grid per organism because I wanted to have a mix between stack (for both KEGGv1 and KEGGv2) and then group it with the other group Reactome. However, when doing the melting, the x-axis will have two labels per facet_grid (KEGG and Reactome, which is the variable obtained from the melting [cat]). I did not want that label so I remove them.
What I am trying to do is to have the organism name per each of the groupings, so each of the 17 grouping bars will have the name of the corresponding organism**. Therefore, I am missing that, I have tried several ways but I cannot find the proper way of doing it.
Thanks in advance,
You can do two things to make this work:
Rotate the labels of the facet so that they plot at 90 degrees
Plot the facet labels beneath the graph using the switch = 'x' argument within facet_grid.
Here is the complete example:
ggplot(melted, aes(x=cat, y=value, fill=variable)) +
geom_bar(stat = 'identity', position = 'stack') +
facet_grid(~organisms, switch = 'x')+
scale_fill_manual(values=c("deepskyblue4", "lightblue1", "olivedrab2")) +
labs(y = "Number of Pathways") +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.title=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line.y = element_line(colour = "black"),
strip.text = element_text(angle = 90))
I left the default shaded background, but you can easily style as you prefer.
Aproach Two
You might want to consider rotating the plot. This requires you to flip the coordinates of the plot using coord_flip before the facet is called. As we are faceting in the other direction we change the facet argument to facet_grid(organisms~.). All other arguments which refer to x or y are swapped over:
ggplot(melted, aes(x=cat, y=value, fill=variable)) +
geom_bar(stat = 'identity', position = 'stack') +
coord_flip() +
facet_grid(organisms~., switch = 'y') +
scale_fill_manual(values=c("deepskyblue4", "lightblue1", "olivedrab2")) +
labs(y = "Number of Pathways") +
theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
legend.title=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line(colour = "black"),
strip.text.y = element_text(angle = 180))
First you can set the organism labels to be below the bars, rather than above. This is established by setting the switch argument of the facet_grid function to "x" (see code below). Secondly, you should then remove axis.title.x=element_blank() argument within your theme function call. The updated code is below. Hope this helps!
ggplot(melted, aes(x = cat, y = value, fill =variable)) +
geom_bar(stat = 'identity', position = 'stack') +facet_grid(~organisms, switch="x")+
scale_fill_manual(values=c("deepskyblue4","lightblue1","olivedrab2"))+
ylab("Number of pathways")+
theme(axis.title.x=element_blank(),axis.text.x=element_blank(),axis.ticks.x=element_blank())+theme(legend.title=element_blank())+
theme(strip.background = element_blank())+theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "black"))
Related
I have this code. Basically, when mousing over the ggplotly plot in the value=50, I have two labels overlapping each other so basically I cannot see the West label, only the South.
How can I prevent that from happening? What am I doing wrong? I would like to see both labels separated when mousing over.
library(ggplot2)
library(ggplotly)
data <- data.frame(
name=c( "A" ),
value=c( 30,40,50,50),
location=c("North","East", "West","South")
)
pxp<- ggplot(data, aes(x=name, y=value, text=location)) + geom_boxplot() +geom_point() +
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+
labs(y="Value")
ggplotly(pxp)
Thank you,
You can use jitter and give appropriate width and height to see the points separately. Then it displays the appropriate values when you hover. Try this
pxp<- ggplot(data, aes(x=name, y=value, text=location)) + geom_boxplot() + # geom_point(position=jitter, width=0.1) +
geom_jitter(alpha=0.6, width=0.02, height=0.1)+
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+
labs(y="Value")
ggplotly(pxp)
I would like to plot in black and white with ggplot2 however I don't want to use shape (ie solid black vs open black outline) because I need the shape to describe another group.
library(ggplot2)
str(mtcars)
p <- ggplot(data = mtcars, aes(x = wt, y=mpg, col=factor (vs), shape= factor (cyl) ))
p + geom_point(size=10) +
theme_bw() +
theme(legend.position="bottom", legend.title=element_blank(), legend.key = element_blank(),
axis.text.x = element_text(size=17),
axis.text.y = element_text(size=17),
axis.title.x = element_text(size=20),
axis.title.y = element_text(size=20),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.text =element_text(size=22)
) +
scale_colour_manual(values = c("red", "blue"))
The plot looks like this.
I would like to have 0 and 1 be plotted as black and white (black outline) however in this case its difficult since the shape has already been taken with factor (vs). Is there any other thing I can do? thanks.
If you want two separate legends for the two factors as you have in your example, you can use "fillable" shapes and the fill aesthetic instead of the color aesthetic. Shapes are shown here; the fillable ones are the ones in yellow, 21-25.
To get your legends to look how you want them, particularly the fill legend, you can override the shape via override.aes in guide_legend. Here I also fill the shape legend in black, but that isn't necessary if you don't mind the white legend.
ggplot(data = mtcars, aes(x = wt, y=mpg, fill = factor(vs), shape = factor (cyl)
)) +
geom_point(size=10) +
theme_bw() +
scale_fill_manual(values = c("black", "white")) +
scale_shape_manual(values = c(21, 24, 22) ) +
guides(fill = guide_legend(override.aes = list(shape = 21) ),
shape = guide_legend(override.aes = list(fill = "black" ) ) )
Here's a solution:
str(mtcars)
p <- ggplot(data = mtcars, aes(x = wt, y=mpg, shape=paste0(vs,cyl) ))
p + geom_point(size=10) +
theme_bw() +
theme(legend.position="bottom", legend.title=element_blank(), legend.key = element_blank(),
axis.text.x = element_text(size=17),
axis.text.y = element_text(size=17),
axis.title.x = element_text(size=20),
axis.title.y = element_text(size=20),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.text =element_text(size=22)
)+scale_shape_manual(values = c("04"=15,"06"=16,"08"=17,"14"=0,"16"=1,"18"=2))
I am ploting a figure in R with ggplot2. The legend in the figure has two rows but I want to unwrap them and make it in one row. I searched around and found that the guide_legned() is probably the way to go. I tried a couple of different ways and the only way does not give an error is like this:
fig <- ggplot(data, aes(y=y, x=x, shape=z))+
geom_point(size = 4)+
scale_shape_manual(values = c(0,1,2,3,4,6))+
geom_abline('somestuff in here')+
scale_fill_continuous(guide=guide_legend(nrow=1))+
annotate('somestuff in here')+
theme_bw()+
theme(legend.title = element_blank(),
panel.grid = element_blank(),
legend.position='top',
legend.key = element_blank())
However, the legend still turned out to be 2 rows. I am wondering why the guide_legend(nrow=1) is not working (even without an error). And what is the correct way to do it? Thanks!
You have a shape aesthetic, but no fill aesthetic in your plot, so scale_fill_continuous isn't applicable here. It's the shape legend you want to format. Two options:
fig <- ggplot(data, aes(y=y, x=x, shape=z))+
geom_point(size = 4)+
scale_shape_manual(values = c(0,1,2,3,4,6), guide=guide_legend(nrow=1)+
geom_abline('somestuff in here')+
annotate('somestuff in here')+
theme_bw()+
theme(legend.title = element_blank(),
panel.grid = element_blank(),
legend.position='top',
legend.key = element_blank())
fig <- ggplot(data, aes(y=y, x=x, shape=z))+
geom_point(size = 4)+
scale_shape_manual(values = c(0,1,2,3,4,6))+
geom_abline('somestuff in here')+
annotate('somestuff in here')+
theme_bw()+
theme(legend.title = element_blank(),
panel.grid = element_blank(),
legend.position='top',
legend.key = element_blank()) +
guides(shape=guide_legend(nrow=1))
I have created a function to create a plot in ggplot2, one of the requirementes is to sort the bar chart according to the score results for each bar. For this, I have rearranged the data frame but when I try to use the scale_x_discrete with the function variable (Geog), it just doesnt work:
f_OS_RegionF <- function(Geog) {
#Carrier Mean Score###########
mean_c<-mean(co_dataa$Score)
###########################
#Region=deparse(substitute(Region))
data_carr<-co_dataa
ScorexGeog_C <- cast(data_carr, as.formula(paste(Geog,"Carrier",sep="~")), mean, value="Score")
names(ScorexGeog_C)[2] <- "Score"
ScorexGeog_C[2]<-round(ScorexGeog_C[2],digits=1)
ScorexGeog_C<- ScorexGeog_C[order(ScorexGeog_C$Score), ]
write.table(ScorexGeog_C,file="ScorexGeog_C_F_Check.csv",sep=",",row.names=F, col.names=T)
d <- ggplot(ScorexGeog_C, aes_string(x=Geog, y="Score", fill=Geog))+
geom_bar(stat="identity", colour="white")+
coord_flip()+
scale_fill_manual(values = coloress)+
scale_x_discrete(limits=paste("ScorexGeog_C",Geog,sep="$"))+
theme(axis.ticks = element_blank(), axis.title.y = element_blank(),axis.text.y = element_blank(), axis.title.x = element_blank(), axis.text.x = element_blank())+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank())+
theme(legend.position = "none") +
geom_text(data=ScorexGeog_C,aes_string(x=Geog,y="Score",label="Score"),colour="white",vjust=0.5,hjust=2,size=2.5, fontface="bold")+
geom_text(data=ScorexGeog_C,aes_string(x=Geog,y=0.3,label=Geog),colour="white", angle=0, vjust=0.5, hjust=0, fontface="bold",size=2)+
geom_hline(yintercept=mean_c, colour = "gray",linetype="solid", size =1, alpha=.8)
print(d)
}
I have a plot in ggplot:
ggplot(plot1,aes(x=c,y=value,colour=variable,linetype=variable,size=variable)) +
geom_line() +
scale_x_continuous(breaks=seq(1,10,1)) +
scale_y_continuous(breaks=seq(0,1, 0.1))+
scale_colour_manual(values=rep(c("blue3","red3"),each=9)) +
scale_linetype_manual(values = rep(c(3,1),each=9)) +
scale_size_manual(values = rep(c(0.6,0.3),each=9)) +
theme_bw(base_size=12, base_family="Helvetica") +
theme(axis.text=element_text(size=8),
axis.title=element_text(size=8),
text = element_text(size=14),
axis.line = element_line(size=0.25),
axis.ticks=element_line(size=0.25),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.position="none" ,
legend.direction="vertical",
legend.title=element_blank(),
legend.text=element_text(size=8),
legend.background=element_blank(),
legend.key=element_blank())+labs(title='a',x='b', y='c' )+theme(plot.title=element_text(size=8))+theme(plot.margin=unit(c(0.1,0,0.1,0),"cm"))
I would like to disable:
the numbers on the y axis
the small lines indicating the position of the numbers
the label
Is this possible to do without affecting the x axis?
I think you can use this approach.
p1 + theme(axis.text.y=element_blank())+ylab("") + scale_y_discrete(breaks=NULL)
# p1 = your initial graph
# axis.text.y removes number
# ylab("") removes y label
# scale_y_discrete(breaks=NULL) removes the tick marks. Your x-axis will not be affected.
Good luck.
You can also do everything in the theme() part of your code. Just add the following code:
p1 + theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())