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

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

Related

Add trend line to longitudinal data line graph

How can I add a trend line that represents the average values for the groups in red and black? I have tried geom_line() and geom_smooth() and haven't gotten it to work.
Here is the code I used to make this graph:
ggplot(data = long_data, aes(x = day, y = val, group = ID, colour = outcome)) +
geom_line(aes(color = as.factor(outcome), linetype = as.factor(outcome)), size = 0.7) +
scale_linetype_manual(values = c("longdash", "solid")) +
scale_color_manual(values = c("black", "red")) + theme_bw() +
theme(legend.position = "", panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
labs(x = "day", y = var, colour = "outcome ") +
xlim(0, 25) +
ggtitle(var) +
theme(plot.title = element_text(hjust = 0.5)) +
ylim(0,100)
Your grouping variable is ID, that's what geom_smooth will inherit.
Try specifying your desired grouping variable outcome by using geom_smooth(aes(group = outcome))

Issues with displaying error bars on graph when copying code

enter link description hereDummy Data Sheet
I'm having issues with getting my graphs to display error bars, even though I've copied and modified code that has worked just fine.
This is the code and graph that is working:
graph2 <- summary %>%
filter(Accuracy == "FALSE") %>%
ggplot(.) + aes(x = Author, y = mean_err, fill = reorder(Quality, -mean_err)) +
geom_bar(stat = "summary", fun.y = "mean", position = "dodge") +
xlab("Race of Author") + ylab("Proportion Lure Reported") + labs(fill = "Text Quality") +
theme(panel.grid.major = element_blank(), panel.grid.minor =
element_blank(),panel.background = element_blank(), axis.line = element_line(colour =
"black")) +
geom_errorbar(aes(ymin= mean_err - se_error, ymax= mean_err + se_error),
position=position_dodge(width=0.9), width=.1) +
geom_text(aes(label=round(mean_err,digits=2)), position=position_dodge(width=.9),
vjust=5) + scale_fill_manual(values=wes_palette("GrandBudapest2"))```
This prints out a working graph with error bars.
When I tried to modify the code to have it instead show the proportion of unsure answers reported, I used this code:
filter(Accuracy == "TRUE") %>%
ggplot(.) + aes(x = Author, y = mean_unsu, fill= Author) +
geom_bar(stat = "summary", fun.y = mean, position = "dodge") +
xlab("Race of Author") + ylab("Proportion Unsure Reported") +
theme(panel.grid.major = element_blank(), panel.grid.minor =
element_blank(),panel.background = element_blank(), axis.line = element_line(colour =
"black")) +
geom_errorbar(aes(ymin= mean_unsu - se_unsu, ymax= mean_unsu + se_unsu),
position=position_dodge(width=0.9), width=.1) +
geom_text(aes(label=round(mean_err,digits=2)), position=position_dodge(width=.9),
vjust=5) + scale_fill_manual(values=wes_palette("GrandBudapest2"))
I get this image with no error bars.
I'm very confused at what I'm doing wrong. I feel like it is probably something very simple.

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").

Change the font size of variable names in ggplot

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())

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

Resources