I have created a plot:
p = qplot(distance, Recombination, data = forplot, colour = factor(Species), size=I(4))
p + labs(title="Effective Recombination", x="Distance Categories", y="Effective Recombination Ratio", colour="Species") + ylim(0,0.022)
However, I need the legend names (s. mimosarum and s. africanus) to be in italics. How do I do that? I have experimented with using expression and fontface, but I cannot get anything working.
Try sth like that
p + theme(legend.text = element_text(face = "italic"))
Related
Is it possible to put the scientific names of the fish in italics on the x-axis and in the legend and to use normal font for the rest?
In my case I would like that for example Barbatula barbatula (Bachschmerle) only Barbatula barbatula is in italics and (Bachscmerle) in the normal font
This is the bar chart right now
And this is a part of the data im using
My code is:
ggplot(R_Sandbach, aes(x = fct_infreq (Species), fill=Species ))+
geom_bar()+
theme_minimal()+
geom_text(aes(label=..count..), stat = "count", vjust = -.1, colour= "black")+
theme(axis.text.x = element_text(angle = 90, size = 5))+
xlab("Fischarten")+
ylab("Individuenanzahl")
ggtext is definitely a good option, although you can just manipulate aspects of your plot directly using theme as you've done in your question.
See an example below:
library(tidyverse)
ggplot() +
geom_blank() +
labs(x = "Fischarten", y = "Individuenanzahl", title = "My Super Awesome Title") +
theme(axis.title.x = element_text(face = "italic"),
plot.title = element_text(face = "italic"))
This produces a blank plot with the plot title and x axis both in italics. You can add as many changes into your theme() function as you like.
EDIT:
In the case of which you need specific words of your titles in italics, etc. you can manually incorporate html into your text and then format your plot to read this html.
In my personal experience this has sometimes caused issues with the general themes of ggplot.
Using this package library(mdthemes) solves the issues of html not being read as it should.
For example:
ggplot() +
geom_blank() +
labs(x = paste("<i>Fischarten</i>", "Other Title Stuff"), y = "Individuenanzahl", title = paste("My Super", "<i>Awesome Title</i>")) +
mdthemes::md_theme_classic()
I can´t figure it out how to fit a legend with a graph in Markdown R.
My result looks like this.
plot_druhych_davek = ockovani %>%
ggplot(aes(x=datum,y=druhych_davek, color = kraj_nazev))+ #+ scale_y_log10()
geom_line(alpha = 0.8)+
labs(title = str_wrap( sprintf("Průběh druhých dávek očkování od: %s DO: %s", OD, DO),40) ,
x = "Datum",
y = "Počet dávek",
color = "kraj_nazev")+
theme_fivethirtyeight()+
theme(axis.title = element_text()) +
theme(legend.title = element_blank())
plot(plot_druhych_davek)
Sadly I dont have enough Rep to put this as a comment but you might be interested in
ggplot(xyz) + scale_color_xxx(guide = guide_legend(keywidth = xxx, keyhight = xxx))
this line of code lets you customize your legend quite granularily, however for me always involves a lot of trial and error. There are much more arguments than the ones I used within guide_legend(). Good luck!
I'm having trouble changing the title and the look of my legend in ggplot, right now it looks like this:
But I want the title to be "Whatever I please" and the colors representing the different data to be larger.(e.g. taking up the whole square instead of being a tiny circle)
For the title I tried changing:
theme(legend.position="top", legend.title='Whatever I please')
But ggplot doesn't accept this. How can I make this adjustments?
ggplot(...) + geom_point(...) +
labs(color = "Your title here") +
guides(color = guide_legend(override.aes = list(size = 5)))
You may need to change the size in the guide to get the look you want.
You could also use the name and guide arguments of the scale_colour_discrete function to do that:
library(ggplot2)
ggplot(mtcars, aes(x = hp, y = qsec, col = as.factor(cyl)))+
geom_point() +
scale_colour_discrete(name = "Whatever I please",
guide = guide_legend(override.aes = list(size = 10)))
The legend.title argument of the theme function only accepts element_text values (so your 'Whatever I please' there won't work), and is used mostly to change font related aspects of the legend title, not the text itself.
Good morning,
I am making a heat map in ggplot of correlations between specific phenotypes. I would like to label each tile with the R^2 for the association.
I have a correlation matrix, max_all, which looks like this:
phenolist2 pheno1 pheno2 pheno3 pheno4 pheno5
max.pheno1 pheno1 0.05475998 0.05055959 0.05056578 0.10330301 0.05026997
max.pheno2 pheno2 0.15743312 0.05036100 0.05151750 0.04880302 0.31008809
max.pheno3 pheno3 0.05458550 0.07672537 0.04043422 0.16845294 0.14268895
max.pheno4 pheno4 0.05484327 0.04391523 0.05151107 0.09521869 0.19776296
max.pheno5 pheno5 0.08658449 0.05183693 0.16292683 0.22369817 0.53630569
Otherwise, my code is as follows:
tmp_Rsq <- melt(max_all)
tmp_Rsq <- ddply(tmp_Rsq, .(variable), transform, rescale=rescale(value))
labels_Rsq <- expression(paste(R^2, " = ", format(tmp_Rsq$value, digits=2), sep=""))
ggplot(tmp, aes(variable, phenolist2)) +
geom_tile(aes(fill =-log10(value)), colour = "white") +
geom_text(aes(label=as.character(labels_Rsq), parse = TRUE, size=4)) +
scale_fill_gradientn(colours = myPalette(101), name="-log10(P)", limits=c(0 , 3.5)) +
theme(axis.title.x = element_blank(), axis.title.y=element_blank(),
plot.title=element_text(size=20))+
theme(axis.text = element_text(colour="black", face="bold"))
My problem is that I can not get the expression to write out so that 2 is a superscript of R.
I realize there are a number of questions on this website addressing similar issues, for example ggplot2 two-line label with expression, Combining paste() and expression() functions in plot labels and Adding Regression Line Equation and R2 on graph but I have been unable to get the solutions suggested in these answers to apply to my case (likely because I have been trying to use a vector of labels).
Thanks a lot for your help.
Parse needs to be outside the aes, and the labels need to be a character vector.
labels_Rsq <- paste0("R^2 ==", format(tmp_Rsq$value, digits=2))
> head(labels_Rsq)
[1] "R^2 ==0.055" "R^2 ==0.157" "R^2 ==0.055" "R^2 ==0.055" "R^2 ==0.087" "R^2 ==0.051"
ggplot(tmp_Rsq, aes(variable, phenolist2)) +
geom_tile(aes(fill =-log10(value)), colour = "white") +
geom_text(aes(label=as.character(labels_Rsq)), parse = TRUE, size=4) +
# scale_fill_gradientn(colours = myPalette(101), name="-log10(P)", limits=c(0 , 3.5)) +
theme(axis.title.x = element_blank(), axis.title.y=element_blank(),
plot.title=element_text(size=20))+
theme(axis.text = element_text(colour="black", face="bold"))
I have a question concerning the legend in ggplot2.
Say I have a hypothetical dataset about mean carrot length for two different colours at two farms:
carrots<-NULL
carrots$Farm<-rep(c("X","Y"),2)
carrots$Type<-rep(c("Orange","Purple"),each=2)
carrots$MeanLength<-c(10,6,4,2)
carrots<-data.frame(carrots)
I make a simple bar plot:
require(ggplot2)
p<-ggplot(carrots,aes(y=MeanLength,x=Farm,fill=Type)) +
geom_bar(position="dodge") +
opts(legend.position="top")
p
My question is: is there a way to remove the title ('Type') from the legend?
Thanks!
I found that the best option is to use + theme(legend.title = element_blank()) as user "gkcn" noted.
For me (on 03/26/15) using the previously suggested labs(fill="") and scale_fill_discrete("") remove one title, only to add in another legend, which is not useful.
You can modify the legend title by passing it as the first parameter to a scale. For example:
ggplot(carrots, aes(y=MeanLength, x=Farm, fill=Type)) +
geom_bar(position="dodge") +
theme(legend.position="top", legend.direction="horizontal") +
scale_fill_discrete("")
There is also a shortcut for this, i.e. labs(fill="")
Since your legend is at the top of the chart, you may also wish to modify the legend orientation. You can do this using opts(legend.direction="horizontal").
You can use labs:
p + labs(fill="")
The only way worked for me was using legend.title = theme_blank() and I think it is the most convenient variant in comparison to labs(fill="") and scale_fill_discrete(""), which also could be useful in some cases.
ggplot(carrots,aes(y=MeanLength,x=Farm,fill=Type)) +
geom_bar(position="dodge") +
opts(
legend.position="top",
legend.direction="horizontal",
legend.title = theme_blank()
)
P.S. There are more useful options in documentation.
You've got two good options already, so here's another using scale_fill_manual(). Note this also lets you specify the colors of the bars easily:
ggplot(carrots,aes(y=MeanLength,x=Farm,fill=Type)) +
geom_bar(position="dodge") +
opts(legend.position="top") +
scale_fill_manual(name = "", values = c("Orange" = "orange", "Purple" = "purple"))
If you are using the up-to-date (As of January 2015) version of ggplot2 (version 1.0), then the following should work:
ggplot(carrots, aes(y = MeanLength, x = Farm, fill = Type)) +
geom_bar(stat = "identity", position = "dodge") +
theme(legend.position="top") +
scale_fill_manual(name = "", values = c("Orange" = "orange", "Purple" = "purple"))
#pascal 's solution in a comment to set the name argument of a scale function, such as scale_fill_discrete, to NULL, is the best option for me. It allows removing the title together with the blank space that would remain if you used "", while at the same time allowing the user to selectively remove titles, which is not possible with the theme(legend.title = element_blank()) approach.
Since it is buried in a comment, I am posting it as an answer to potentially increase its visibility, with kudos to #pascal.
TL;DR (for the copy-pasters):
scale_fill_discrete(name = NULL)