Change the font size of variable names in ggplot - r

I am not able to increase the font size of the names of the variables in a graphic realized with ggplot.
I tried to include these codes inside ggplot code, but unsuccessfully :
theme(text = element_text(size=20))
theme(axis.text=element_text(size=20))
theme(axis.title=element_text(size=14))
theme_grey(base_size = 20)
geom_text(size=20)
My code is :
library(ggplot2)
library(reshape2)
dataplot <- read.csv("/Documents/R.csv",header=T,sep=";")
dataPlotMelt <- melt(data = dataplot, id.vars = c("variable"),variable.name = "Method",value.name = "SMD")
varNames <- as.character(dataplot$variable)
dataPlotMelt$variable <- factor(dataPlotMelt$variable,levels = varNames)
ggplot(data=dataPlotMelt,mapping=aes(x=variable,y=SMD,group=Method, color=Method))+
ylab("Standardizedmeandifference(%)")+
xlab("") +
geom_point(aes(shape=Method),size=2) +
geom_hline(yintercept=15,color="black",size=0.1,linetype="dashed") +
geom_hline(yintercept=-15,color="black",size=0.1,linetype="dashed") +
coord_flip() +
theme(axis.text.x=element_blank()) +
scale_y_continuous(breaks=c(-65,-15,15,105)) +
theme_bw() +
theme(legend.text=element_text(size=12)) +
theme(legend.title=element_blank(),legend.key=element_blank()) +
scale_colour_manual(values=c("grey","black"))
I'd like to increase the font size of the names of the variables in the graphic and, besides, increase the text "Standardized mean difference (%)" and remove the vertical line between the yintercept and ybreak on both sides

new graphic
Thank you Richard for giving me the solution.
As you suggested I used theme after theme_bw
I managed to suppress the useless vertical lines as well with the command theme(panel.grid.minor = element_blank())
Here is the new code for ggplot :
ggplot(data = dataPlotMelt, mapping = aes(x = variable, y = SMD,group = Method,
color = Method)) +
ylab("Standardized mean difference (%)") + xlab("") +
geom_point(aes(shape = Method),size=2) +
geom_hline(yintercept = 15, color = "black", size = 0.1, linetype = "dashed") +
geom_hline(yintercept = -15, color = "black", size = 0.1, linetype = "dashed") +
coord_flip() +
theme(axis.text.x = element_blank()) +
scale_y_continuous(breaks=c(-65,-15,0,15,105)) +
theme_bw() + theme(legend.text = element_text(size=13)) +
scale_colour_manual(values= c("grey","black")) +
theme(axis.text.y = element_text(size=12)) +
theme(axis.title.x = element_text(size=13)) +
theme(panel.grid.minor = element_blank()) +
theme(legend.title = element_blank(), legend.key=element_blank())

Related

Reordering Plot and Changing Axis Size (R ggplot)

I'm trying to change in both of my plots, the order and the x axis size for both. These are not being able to be changed accordingly
DF Creation
contig_count_average <- data.frame(Genome_Type = c("MBT", "Anglucyclines", "Whole Genome"),
Contig_Count_Average = c("2.91","83.7","608.3"))
Plot
p2 <- ggplot(contig_count_average, mapping = aes(x = reorder(Genome_Type, Contig_Count_Average), Contig_Count_Average, fill = Genome_Type)) +
xlab("Genome") +
ylab("Contig No.") +
ggtitle("Contig Count per Genome Distribution") +
geom_bar(stat = "identity") +
theme(text = element_text(size=20),
axis.text.x = element_text(angle=90, hjust=1)) +
guides(fill=guide_legend(title="Genome Type")) +
coord_flip() +
theme_bw() +
scale_y_continuous(limits = c(0,2835), expand = c(0, 0)) +
scale_x_discrete(labels = abbreviate)
p
I get the following warning:
1: In Ops.factor(Contig_Count_Average) : ‘-’ not meaningful for factors
The issue is because Contig_Count_Average is treated as factors in contig_count_average.
We can change it to numeric by doing either :
contig_count_average <- type.convert(contig_count_average, as.is = TRUE
Or
contig_count_average$Contig_Count_Average <- as.numeric(as.character(contig_count_average$Contig_Count_Average))
and then use the ggplot code.
p2 <- ggplot(contig_count_average, mapping = aes(x = reorder(Genome_Type,
Contig_Count_Average), Contig_Count_Average, fill = Genome_Type)) +
xlab("Genome") +
ylab("Contig No.") +
ggtitle("Contig Count per Genome Distribution") +
geom_bar(stat = "identity") +
theme(text = element_text(size=20),
axis.text.x = element_text(angle=90, hjust=1)) +
guides(fill=guide_legend(title="Genome Type")) +
coord_flip() +
theme_bw() +
scale_y_continuous(limits = c(0,2835), expand = c(0, 0)) +
scale_x_discrete(labels = abbreviate)
p2
Also note that you can use geom_col instead of geom_bar(stat = "identity").

Scale the distance between ticks on x-axis in gglot r

I have a graph like this. I am interested in the minor change between the range (-3.5, 0.5), however, the occupied only a small portion of the x-axis, so that it's hard to interpret.
I tried to use transform to log scale for better visualization, however, it apparently not works for negative values.
So is there any method to expand this region to make the graph look nicer?
Code:
ggplot() + geom_line(data = Final_diction, aes(x = Final_diction[,1], y
= Final_diction[,4])) +
xlim(-3.5,20) +
geom_vline(xintercept=c(-0.5,0.5), linetype="dashed", color = "red") +
geom_vline(xintercept=c(-0.25,0.25), linetype="dashed", color = "blue") +
theme_bw() +
theme(axis.title = element_text(size = 20)) +
theme(axis.text = element_text(size = 18))
Something like this
library(ggforce)
library(ggolot2)
ggplot(mtcars, aes(x=mpg, y=disp, group=1)) +
geom_line() + facet_zoom(xlim = c(15, 20))
You may try adding the xlim = c(minor value, major value) option of ggplot, and use the range which works better for you
Something like that:
ggplot() + geom_line(data = Final_diction, aes(x = Final_diction[,1], y
= Final_diction[,4])) +
xlim(-3.5,20) +
geom_vline(xintercept=c(-0.5,0.5), linetype="dashed", color = "red") +
geom_vline(xintercept=c(-0.25,0.25), linetype="dashed", color = "blue") +
theme_bw() +
theme(axis.title = element_text(size = 20)) +
theme(axis.text = element_text(size = 18)) +
xlim = c(-4, 1)

Adding annotate text to plot with as.Date x-axis

I've seen similar questions but the answers did not seem to help my graph. I have a plot here:
n_cases_plot <- ggplot(data = n_cases, aes(x = month, y = n_cases, color
= "#668cff", fill = "#668cff")) +
geom_bar(stat = "identity") +
theme(panel.background = element_blank()) +
ggtitle("Number of Parole Cases Heard by Month") +
theme(plot.title = element_text(hjust = 0.4)) +
guides(fill=FALSE) +
guides(color = FALSE) +
xlab("") +
ylab("") +
theme(text=element_text(size=12, family="Georgia")) +
geom_smooth(method = lm, se = FALSE, aes(color = "lightskyblue1")) +
theme(panel.background = element_rect(fill = "moccasin")) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
I'm trying to add text to describe the decrease as -29%. I've tried adding
+ annotate("text", x = 4, y = 10, "-29%")
But I keep getting the error
Error: Invalid input: date_trans works with objects of class Date only

Overplotting of labels, data points and more (w geom_dl & direct label)

I can not find a solution for my overplotting problem. If somebody could help me find a solution I would appreciate that a lot.
My data look like this (csv format): http://pastebin.com/embed_js.php?i=Cnfpkjsz
This is the code I am running:
library(dplyr)
library(gdata)
library(ggplot2)
library(directlabels)
all<-read.xls('all_auto_bio_adjusted.xls')
all$station<-as.factor(all$station)
all$automatic<-log(all$automatic)
all$averagebiol<-log(all$averagebiol)
all$stdevbiol<-log(all$stdevbiol)
pd <- position_dodge(.9)
allp<-ggplot(data=all, aes(y=averagebiol, x=automatic, colour=group)) +
geom_errorbar(aes(ymin=averagebiol-stdevbiol, ymax=averagebiol+stdevbiol), colour="red", width=.1, position=pd) +
geom_point(aes(size=size), show_guide = TRUE) +
geom_abline(intercept=0, slope=1) +
stat_smooth(method="loess",se=FALSE,colour='blue') +
geom_dl(aes(label=shortname),method="last.bumpup",cex = 1.3, hjust = 1) +
facet_wrap(~station,nrow=2)+
xlab("auto") +
ylab("manual") +
ggtitle("Comparison of automatic vs manual identification") +
scale_y_continuous(limits=c(0, max(all$averagebiol + all$stdevbiol))) +
theme_bw() +
theme(plot.title = element_text(lineheight=.8, face="bold", size=20,vjust=1), axis.text.x = element_text(colour="grey20",size=15,angle=0,hjust=.5,vjust=.5,face="bold"), axis.text.y = element_text(colour="grey20",size=15,angle=0,hjust=1,vjust=0,face="bold"), axis.title.x = element_text(colour="grey20",size=20,angle=0,hjust=.5,vjust=0,face="bold"), axis.title.y = element_text(colour="grey20",size=20,angle=90,hjust=.5,vjust=1,face="bold"),legend.position="right")
allp
I tried around quite a bit with different geom_dl methods but can't find the right one. Is there one that can plot above the error bars?
If there is no good one for me. What could I do to at least have the labels plotted nicely so that I can rearrange them myself in photoshop?
Thanks a lot for your input!
I'm not familiar with directlabels but if you want to move the labels to the top you could just do it with geom_text():
allp <- ggplot(data = all, aes(y = averagebiol, x = automatic, colour = group)) +
geom_errorbar(aes(ymin = averagebiol - stdevbiol, ymax = averagebiol + stdevbiol),
colour = "red", width = 0.1, position = pd) +
geom_point(aes(size = size), show_guide = TRUE) +
geom_abline(intercept = 0, slope = 1) +
stat_smooth(method = "loess", se = FALSE, colour = "blue") +
facet_wrap(~station, nrow = 2) +
xlab("auto") + ylab("manual") +
ggtitle("Comparison of automatic vs manual identification") +
scale_y_continuous(limits = c(0, max(all$averagebiol + all$stdevbiol)))
allp + geom_text(aes(label = shortname, y = averagebiol + stdevbiol), vjust = -0.1)
However still seems too busy to distinguish between the groups. How about skipping the text labels and facetting over station and group? Here's a possible start, if you like it you would need to tune it...
allp <- ggplot(data = all, aes(y = averagebiol, x = automatic, colour = group)) +
geom_errorbar(aes(ymin = averagebiol - stdevbiol, ymax = averagebiol + stdevbiol),
colour = "grey", width = 0.5, position = pd) +
geom_point(aes(size = size), show_guide = TRUE) +
geom_abline(intercept = 0, slope = 1) +
stat_smooth(method = "loess", se = FALSE, colour = "blue") +
facet_grid(station ~ group) +
xlab("auto") + ylab("manual") +
ggtitle("Comparison of automatic vs manual identification") +
scale_y_continuous(limits = c(0, max(all$averagebiol + all$stdevbiol))) +
theme_minimal()
allp
I hope you wanted somehting like this.
My code:
allp<-ggplot(data=all, aes(y=averagebiol, x=automatic, colour=group)) +
geom_point(aes(size=size), show_guide = TRUE) +
geom_abline(intercept=0, slope=1) +
stat_smooth(method="loess",se=FALSE,colour='blue') +
facet_wrap(~station,nrow=2)+
xlab("auto") +
ylab("manual") +
ggtitle("Comparison of automatic vs manual identification") +
scale_y_continuous(limits=c(0, max(all$averagebiol + all$stdevbiol + 1, na.rm=T))) +
theme_bw() +
theme(plot.title = element_text(lineheight=.8, face="bold", size=20,vjust=1), axis.text.x = element_text(colour="grey20",size=15,angle=0,hjust=.5,vjust=.5,face="bold"), axis.text.y = element_text(colour="grey20",size=15,angle=0,hjust=1,vjust=0,face="bold"), axis.title.x = element_text(colour="grey20",size=20,angle=0,hjust=.5,vjust=0,face="bold"), axis.title.y = element_text(colour="grey20",size=20,angle=90,hjust=.5,vjust=1,face="bold"),legend.position="right")+
geom_errorbar(aes(ymin=averagebiol-stdevbiol, ymax=averagebiol+stdevbiol), colour="red", width=.1, position=pd) +
geom_text(aes(label = shortname, y = averagebiol+stdevbiol), vjust = -.3)
I removed your line of code for the labels and I introduced this one:
geom_text(aes(label = shortname, y = averagebiol+stdevbiol), vjust = -.3)
This is just putting the labels on top of the Error bar, with a small adjustment.
Also I modified this part:
scale_y_continuous(limits=c(0, max(all$averagebiol + all$stdevbiol + 1, na.rm=T)))
With the labels on top, some of the labels where covered by the gray bar, so I increased a bit the value of the X bar (+1), but the max didn't want to work correctly with my little addition, so I had to remove the NA values.
Indeed I ended up faceting by station and group after input from Harrop.
Here is my code (The underlying data changed slightly)
library(dplyr)
library(gdata)
library(ggplot2)
library(directlabels)
all<-read.xls('all_auto_bio_adjusted_c.xls')
all$size.new<-sqrt(all$size.new)
all$station<-as.factor(all$station)
all$group.new<-factor(all$group, levels=c('C. hyperboreus','C. glacialis','Special Calanus','M. longa','Pseudocalanus sp.','Copepoda'))
pd <- position_dodge(w = 50)
allp <- ggplot(data = all, aes(y = averagebiol, x = automatic, colour = group.new, group=group.new)) +
geom_abline(intercept = 0, slope = 1) +
geom_point(aes(size = size.new), show_guide=TRUE, position=pd) +
scale_size_identity()+
geom_errorbar(aes(ymin = averagebiol - stdevbiol, ymax = averagebiol + stdevbiol),colour = "grey", width = 0.1, position=pd) +
facet_grid(group.new~station, scales="free") +
xlab("Automatic identification") + ylab("Manual identification") +
ggtitle("Comparison of automatic vs manual identification") +
theme_bw() +
theme(plot.title = element_text(lineheight=.8, face="bold", size=20,vjust=1), axis.text.x = element_text(colour="grey20",size=15,angle=0,hjust=.5,vjust=.5,face="bold"), axis.text.y = element_text(colour="grey20",size=15,angle=0,hjust=1,vjust=0,face="bold"), axis.title.x = element_text(colour="grey20",size=20,angle=0,hjust=.5,vjust=0,face="bold"), axis.title.y = element_text(colour="grey20",size=20,angle=90,hjust=.5,vjust=1,face="bold"), legend.position="none", strip.text.x = element_text(size = 12, face="bold", colour = "black", angle = 0), strip.text.y = element_text(size = 12, face="bold", colour = "black"))
allp

Working with "old" ggplot2 code

I made ton of figures with ggplot 0.8.9 (which is what I'm still running). Now I need to modify these figures to include legends. I am running across all sorts of problems that are hard to solve because I am getting really confused about theme and opts and many SO answers that apply to later versions.
At this point, it seems like I need to update ggplot2 and rewrite all of my code just so I can have legends on my figures. Is this true? I've read the ggplot2 transition guide, it makes it seem true.
Here's what the old code looks like (does not produce a legend): And here is the data for the sake of reproducibility: mean10v2 and stderr10.
me10<-read.table("mean10v2.txt", header=TRUE)
se10<-read.table("stderr10.txt", header=TRUE)
ggplot() +
geom_ribbon(aes(x = me10[me10$trt=="CC", "tu"], ymin=(me10[me10$trt=="CC", "biomassA"]-
se10[se10$trt=="CC", "biomassA"]), ymax=(me10[me10$trt=="CC",
"biomassA"]+se10[se10$trt=="CC", "biomassA"])), alpha=0.25) +
geom_line(aes(me10[me10$trt=="CC", "tu"], y=me10[me10$trt=="CC", "biomassA"]), size=1)+
geom_ribbon(aes(x = me10[me10$trt=="PF", "tu"], ymin=(me10[me10$trt=="PF", "biomassA"]-
se10[se10$trt=="PF", "biomassA"]), ymax=(me10[me10$trt=="PF",
"biomassA"]+se10[se10$trt=="PF", "biomassA"])), alpha=0.25) +
geom_line(aes(me10[me10$trt=="PF", "tu"], y=me10[me10$trt=="PF", "biomassA"]),
colour="red2", linetype="dashed", size=1) +
geom_ribbon(aes(x = me10[me10$trt=="P", "tu"], ymin=(me10[me10$trt=="P", "biomassA"]-
se10[se10$trt=="P", "biomassA"]), ymax=(me10[me10$trt=="P",
"biomassA"]+se10[se10$trt=="P", "biomassA"])), alpha=0.25) +
geom_line(aes(me10[me10$trt=="P", "tu"], y=me10[me10$trt=="P", "biomassA"]),
colour="blue3", linetype="dotted", size=1) +
opts(panel.grid.minor = theme_blank()) +
opts(panel.grid.major = theme_blank()) +
opts(panel.background = theme_blank()) +
opts(axis.line = theme_segment()) +
opts(legend.position=c(.5,.5)) +
opts(axis.title.x = theme_text(size=12,vjust=-0.5)) +
opts(axis.title.y = theme_text(size=12,angle=90)) +
opts(axis.text.x = theme_text(colour="black", size=16)) +
opts(axis.text.y = theme_text(colour="black", size=16)) +
annotate("text", x = -Inf, y = Inf, label = "a", face="bold", hjust = -5, vjust=2, size
= 9) +
ylab("") +
xlab("") +
ylim(0,2200)
Updating the theme parts is actually quite simple. You really just need to change opts() to theme() and replace theme_* with element_*. Some other names have changed, like you'll use element_line instead of theme_segment.
But more generally, you're using ggplot all wrong:
my_df <- me10[,c('trt','tu','biomassA')]
my_se <- setNames(se10[,c('trt','tu','biomassA')],c('trt','tu','se'))
my_df <- merge(my_df,my_se)
ggplot(data = my_df,aes(x = tu,y = biomassA)) +
geom_ribbon(aes(group = trt,ymin = biomassA - se,ymax = biomassA + se),alpha = 0.25) +
geom_line(aes(group = trt,linetype = trt,colour = trt)) +
labs(x = "",y = "") +
ylim(0,2200) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(),
legend.position=c(.5,.5),
axis.title.x = element_text(size=12,vjust=-0.5),
axis.title.y = element_text(size=12,angle=90),
axis.text.x = element_text(colour="black", size=16),
axis.text.y = element_text(colour="black", size=16)) +
annotate("text", x = -Inf, y = Inf, label = "a", face="bold", hjust = -5, vjust=2, size = 9)
Notice how much cleaner that is, and putting the data into an appropriate form only took three lines. Also note that there is absolutely no need to keep repeating the opts() or theme() calls for every....single....thing...you....set.
And then if you want to choose specific colors/linetypes for each group, you do that using the scale functions, not by setting them individually:
+ scale_colour_manual(values = c('black','red2','blue3')) +
scale_linetype_manual(values = c('solid','dashed','dotted'))

Resources