Scale the distance between ticks on x-axis in gglot r - 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)

Related

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

Broken confidence interval areas when using ylim in ggplot2

I've been using ggplot2 for long, but never experienced this issue. I am representing confidence intervals of some regressions. However, I decided to manually control the ylim(). I realized that those areas which exceed the y limits are broken. See this picture:
The red regression on the right contains a very wide CLs. As you can see there is a gap in there as its highest point is outside ylim range.
This is the code I used:
ggplot(dataset, aes(x=variable, y=value, fill=Species, colour=Species, linetype = Species)) +
geom_smooth(method="lm", formula= y~poly(x,3), level=0.95, alpha=0.2) +
xlab("A") +
ylab("B") +
ylim(0, 30) +
theme(axis.text.x = element_text(angle = 0, hjust = 0.5, size = 10),
panel.background = element_blank(),
legend.position='bottom',
panel.grid.major = element_line(colour="azure2"),
axis.line = element_line(colour = "black",
size = 0.15, linetype = "solid")) +
scale_x_continuous(breaks=seq(1, 10, 1), limits=c(1, 10)) +
scale_color_manual(values=c("coral4", "coral1", "darkolivegreen3", "darkgoldenrod4", "darkgoldenrod2", "deepskyblue3", "darkorchid3")) +
scale_fill_manual(values=c("coral4", "coral1", "darkolivegreen3", "darkgoldenrod4", "darkgoldenrod2", "deepskyblue3", "darkorchid3")) +
scale_linetype_manual(values=c(1,1,1,3,3,2,2))
I would like to keep these y limits. I used coord_cartesian with no success. Can anybody help me?
coord_cartesian should work, but you have to remove the ylim()
Some data
set.seed(1)
df <- data_frame(x = -5:5, y = rnorm(11, x^2, 5))
Replicating your problem
ggplot(df, aes(x, y)) +
geom_smooth() +
ylim(-1, NA)
With coord_cartesian
ggplot(df, aes(x, y)) +
geom_smooth() +
coord_cartesian(ylim = c(-1, 40))

Mapping mean values onto count diagram using ggplot2 in R

I have a data frame as follows:
variable=c("D","D","C","C","C","A","B","B","B","B")
value=c(80,100,70,68,65,45,33,31,36,32)
Count=as.integer(c(5,10,4,5,2,7,3,5,6,2))
mean=c(93.3,93.3,68.2,68.2,68.2,45,33.4,33.4,33.4,33.4)
df=data.frame(variable=variable,value=value,Count=Count,mean=mean)
I can make a nice plot (where the size of the square corresponds to the count of observations with that particular x-value and y-value), as shown below:
ggplot(df, aes(variable, value)) + geom_point(aes(size = Count), pch=15) + guides(fill=guide_legend(title="New")) + theme(legend.text=element_text(size=rel(2.3)), legend.title=element_text(size=rel(2.3), face="plain"), legend.position="right", axis.text = element_text(size=rel(2.3)), axis.title = element_text(size = rel(2.3))) + labs(x="Topic", y = "Percentage Grade")
However, I now want to superimpose a horizontal bar to each of the four topics, indicating the mean percentage grade. Those values are stored in df$mean. I cannot figure out how to accomplish this. I have tried using the geom_line() function with the horizontal line option... but this seems to plot vertical lines!
ggplot(df, aes(variable, value)) + geom_point(aes(size = Count), pch=15) + guides(fill=guide_legend(title="New")) + theme(legend.text=element_text(size=rel(2.3)), legend.title=element_text(size=rel(2.3), face="plain"), legend.position="right", axis.text = element_text(size=rel(2.3)), axis.title = element_text(size = rel(2.3))) + labs(x="Topic", y = "Percentage Grade") + geom_line(stat = "hline", yintercept = df$mean)
Thank you...
You can do this with geom_segment:
ggplot(df, aes(variable, value)) +
geom_point(aes(size = Count), pch=15) +
geom_segment(aes(x=variable, y=mean-.1,
xend=variable, yend=mean+.1),
color="red", size=I(40))

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

ggplot change fill colour without losing colour gradient

Maybe this is a very basic question but I'm a ggplot and R beginner.
I'm using this command to get a barplot:
ggplot(data=melt, aes(x=variable, y=value, fill=value)) +
geom_bar(width=.8, stat="identity") +
xlab("Samples") + ylab("Expression") + ggtitle("Gapdh") +
theme(plot.title=element_text(face="bold", size=12)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size=10)) +
theme(axis.text.y = element_text(size=10))
I want to change the colors of barplot, but keeping the gradient of the colors depending on value column. I've tried this but I lose the gradient:
ggplot(data=melt, aes(x=variable, y=value, fill=value)) +
geom_bar(width=.8, stat="identity", fill="red") +
xlab("Samples") + ylab("Expression") + ggtitle("Gapdh") +
theme(plot.title=element_text(face="bold", size=12)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size=10)) +
theme(axis.text.y = element_text(size=10))
The data is simple, only two columns( variable - value ):
variable value
1 nu73 13576.49
2 nu73t 10891.88
3 nu81 12673.33
4 nu81t 12159.91
5 nu83 12570.82
6 nu83t 11828.04
Thank you guys in advance
You want to adjust the scale, in particular the continuous scale of fill colors, hence the function scale_fill_continuous().
ggplot(data = melt, aes(x = variable, y = value, fill = value)) +
geom_bar(width = .8, stat = "identity") +
labs(x = "Samples", y = "Expression", title = "Gapdh") +
theme(plot.title = element_text(face = "bold", size = 12),
axis.text.x = element_text(angle = 45, hjust = 1, size = 10),
axis.text.y = element_text(size = 10)) +
scale_fill_continuous(low = "firebrick4", high = "firebrick1")
(I slightly modified your plotting code: you can call theme once with multiple arguments, and I find labs nicer than a bunch of individual labeling calls.)
One other option is to use the palettes from the RColorBrewer package (which are incorporated into ggplot2). The scale_fill_brewer() scale if for discrete color scales, you can "distill" them into continuous scales with scale_fill_distiller(). For example
scale_fill_distiller(type = "seq", palette = "Reds")
To see all of the available scales, run RColorBrewer::display.brewer.all().

Resources