I'm preparing a figure for a publication. I'm omitting the x label by setting xlab(""), however ggplot2 produces a whitespace instead of completely removing the label. How can I get rid of the whitespace (marked by red rectangle in the plot below)?
The full code:
ggplot(data, aes(x=Celltype, y=Mean, fill=factor(Dose), label=p.stars)) +
geom_bar(stat = "identity", position = position_dodge(width=0.9), aes(group=Dose)) +
geom_errorbar(aes(ymin = Mean - SEM, ymax = Mean + SEM), stat = "identity", position = position_dodge(width=0.9), width=0.25) +
geom_text(aes(y = Mean + SEM), size = 5, position = position_dodge(width=0.9), hjust = .5, vjust = -1) +
xlab("") +
ylab("Concentration") +
scale_fill_grey(name = "Dose") +
theme_bw()
Use theme() to remove space allocated for the x axis title. When you set xlab("") there is still space made for this title.
+ theme(axis.title.x=element_blank())
Have you tried plot.margin?
library(grid)
ggplot(data, aes(x=Celltype, y=Mean, fill=factor(Dose), label=p.stars)) +
geom_bar(stat = "identity", position = position_dodge(width=0.9), aes(group=Dose)) +
geom_errorbar(aes(ymin = Mean - SEM, ymax = Mean + SEM), stat = "identity", position = position_dodge(width=0.9), width=0.25) +
geom_text(aes(y = Mean + SEM), size = 5, position = position_dodge(width=0.9), hjust = .5, vjust = -1) +
xlab("") +
ylab("Concentration") +
scale_fill_grey(name = "Dose") +
theme_bw() +
theme(plot.margin = unit(c(1,1,0,1), "cm")) # ("left", "right", "bottom", "top")
Try this function:
savepdf <- function(file, width=16, height=10) {
fname <- paste("figures/",file,".pdf",sep="")
pdf(fname, width=width/2.54, height=height/2.54,
pointsize=10)
par(mgp=c(2.2,0.45,0), tcl=-0.4, mar=c(3.3,3.6,1.1,1.1))
}
You can also crop the white space in the resulting pdf file once created. In Unix, the system command is:
pdfcrop filename.pdf filename.pdf
pdfcrop does work on Mac provided the standard LaTeX distribution (Mactex or texlive) is installed. Of course, this command can be executed in R as follows:
system(paste("pdfcrop", filename, filename))
You could also set x = NULL in labs()
ggplot(data, aes(x=Celltype, y=Mean, fill=factor(Dose), label=p.stars)) +
geom_bar(stat = "identity", position = position_dodge(width=0.9), aes(group=Dose)) +
geom_errorbar(aes(ymin = Mean - SEM, ymax = Mean + SEM), stat = "identity", position = position_dodge(width=0.9), width=0.25) +
geom_text(aes(y = Mean + SEM), size = 5, position = position_dodge(width=0.9), hjust = .5, vjust = -1) +
labs(x = NULL, y = "Concentration") +
scale_fill_grey(name = "Dose") +
theme_bw()
Related
I am trying to use both coord_trans and coord_flip in the same plot, but that seems to not work. Any suggestion how to use coord_trans for a plot that needs to be flipped?
Using scale_y_log10 does not work since it messes up the stat_summary
p <- ggplot(df.2,aes(reorder(x,y),y,colour=z)) +
geom_jitter(width = 0.2,size=0.1) +
theme_classic(base_size = 8) +
stat_summary(
fun = mean,
geom = "errorbar",
aes(ymax = ..y.., ymin = ..y..),
position = position_dodge(width = 0.1),
width = 0.7,
colour="black") +
coord_flip() +
theme(legend.position = "none") +
labs(x="",y="") +
scale_color_manual(values = mycolors)
p + coord_trans(y = "log10")
I have the following code. I'd like to change the color of the boxplots so they all have the same fill color (grey).
Also I'd like to have the stat_summary texts to stick to the bottom of each barplot but vjust only seem to provide relative position?
Thanks
boxp <- ggplot(mtcars, aes(as.factor(cyl), wt, fill=as.factor(am)) ) +
geom_bar(position = "dodge", stat = "summary", fun.y = "median") +
geom_boxplot(outlier.shape = NA, width=0.2, color = "black", position = position_dodge(0.9)) +
stat_summary(aes(label=round(..y..,2)), fun.y=median, geom="text", size=8, col = "white", vjust=8, position = position_dodge(0.9)) +
stat_summary(fun.y=mean, geom="point", shape=18, size=4, col="white", position = position_dodge(0.9)) +
labs(x = "Conditions", y = "Medians") +
scale_y_continuous(limits=c(0,7),oob = rescale_none) +
theme_bw()
boxp
Here is a possible solution, but it needs ggplot v3.3.0 for the stage() function.
To point out major changes:
Instead of using the fill as an implicit grouping, I've explicitly set the grouping so it isn't tied to the fill.
I added the fill as an aesthetic of the bar geom.
The boxplot now has the unmapped aesthetic fill = 'gray'
The text stat summary uses stage() to calculate the statistic but then uses 0 as actual placement.
library(ggplot2)
library(scales)
ggplot(mtcars, aes(as.factor(cyl), wt,
group = interaction(as.factor(cyl), as.factor(am)))) +
geom_bar(aes(fill=as.factor(am)), position = "dodge", stat = "summary", fun = "median") +
geom_boxplot(outlier.shape = NA, width=0.2,
color = "black", fill = 'gray',
position = position_dodge(0.9)) +
stat_summary(aes(label=round(after_stat(y), 2), y = stage(wt, after_stat = 0)),
fun=median, geom="text", size=8, col = "white", vjust=-0.5,
position = position_dodge(0.9)) +
stat_summary(fun=mean, geom="point", shape=18, size=4, col="white", position = position_dodge(0.9)) +
labs(x = "Conditions", y = "Medians") +
scale_y_continuous(limits=c(0,7),oob = rescale_none) +
theme_bw()
Created on 2020-05-06 by the reprex package (v0.3.0)
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())
Where and how do I specify colors, axis lines, and removal of background in geombar? Ultimately, I want to have one bar to be dark gray and one bar to be light gray. They are currently blue and pink which were defaults. I also want the the x and y to have axis lines, and the figure to have no gray background. I have everything else figured out, using the below code. Thank you for your help.
library(ggplot2)
dodge <- position_dodge(width = 0.9)
limits <- aes(ymax = myData$mean + myData$se,
ymin = myData$mean - myData$se)
p <- ggplot(data = myData, aes(x = names, y = mean, fill = names)) +
p + geom_bar(stat = "identity", position = dodge) +
geom_errorbar(limits, position = dodge, width = 0.9) +
theme(axis.text.x=element_blank(), axis.ticks.x=element_blank(),
axis.title.x=element_blank())
limits <- aes(ymax = myData$mean + myData$se,
ymin = myData$mean - myData$se)
p <- ggplot(data = myData, aes(x = factor(site), y = mean,
fill = factor(infectionstatus)))
p + geom_bar(stat = "identity",
position = position_dodge(0.9)) +
geom_errorbar(limits, position = position_dodge(0.9),
width = 0.25) +
labs(x = "Sites", y = "Average Calories in White Muscle Tissue") +
scale_fill_discrete(name = "Infection Status")
You probably wanted something like this:
# Generate data
myData <- data.frame(names = letters[1:2],
mean = 1:2,
SE = 0.1)
# Plot data
library(ggplot2)
ggplot(myData, aes(names, mean)) +
geom_bar(aes(fill = names),
stat = "identity", position = "dodge") +
geom_errorbar(aes(ymin = mean - SE, ymax = mean + SE),
position = position_dodge(width = 0.5), width = 0.5) +
labs(title = "Calorie Amount",
subtitle = "Averaged per Tissue",
x = NULL,
y = "Average Calories in White Muscle Tissue",
fill = "Infection Status") +
scale_fill_manual(values = c("grey40", "grey60")) +
theme_classic() +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_blank())
I used theme_classic() as it does most of the job when you want clean plot. And specified colors with scale_fill_manual(values = c("grey40", "grey60"))
Given the following data:
df.plot <- data.frame(x=c("outcome name","outcome name"),
Condition=c("A","B"),
Score=c(41.5,51.8))
I can produce the following graph:
With this code:
ggplot(df.plot, aes(x=x, y=Score, fill=Condition)) +
geom_bar(position = 'dodge', stat='identity', width=.5) +
xlab(NULL) + coord_cartesian(ylim=c(0,100)) +
geom_text(aes(label=round(Score,2)), position=position_dodge(width=0.5), vjust=-0.25)
I would like to add a confidence interval to the "B" bar that goes from 27.5 to 76.1. I would like those values to be labeled in the graph.
I tried modifying df.plot to include this information and using geom_errorbar but i endup with 2 intervals intead of just one for Condition "B"
df.plot <- data.frame(x=c("outcome name","outcome name"),
Condition=c("A","B"),
Score=c(41.5,51.8),
lb = c(NULL,27.5),
ub = c(NULL,76.1))
ggplot(df.plot, aes(x=x, y=Score, fill=Condition)) +
geom_bar(position = 'dodge', stat='identity', width=.5) +
xlab(NULL) + coord_cartesian(ylim=c(0,100)) +
geom_errorbar(aes(ymin = lb, ymax = ub),
width = 0.2,
linetype = "dotted",
position = position_dodge(width = 0.5),
color="red", size=1) +
geom_text(aes(label=round(Score,2)), position=position_dodge(width=0.5), vjust=-0.25)
Finally, i'm not sure how to add the labels to the top and bottom of the interval.
NA is used for missing values not NULL
This should work as you expect:
df.plot <- data.frame(x=c("outcome name","outcome name"),
Condition=c("A","B"),
Score=c(41.5,51.8),
lb = c(NA,27.5),
ub = c(NA,76.1))
ggplot(df.plot, aes(x=x, y=Score, fill=Condition)) +
geom_bar(position = 'dodge', stat='identity', width=.5) +
xlab(NULL) + coord_cartesian(ylim=c(0,100)) +
geom_errorbar(aes(ymin = lb, ymax = ub),
width = 0.2,
linetype = "dotted",
position = position_dodge(width = 0.5),
color="red", size=1) +
geom_text(aes(label=round(Score,2)), position=position_dodge(width=0.5), vjust=-0.25) +
geom_text(aes(y = lb, label = lb), position=position_dodge(width=0.5), vjust=2)