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)
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'm trying to obtain 2 aligned bar plots with percentage scales of 2 different factors. The y scales calculated as percent are different. I would like to have the same y scales for both plots, for example from 0 to 40% in both. I've tried ylim() which doesn't work on the percentage scale. Example below
library(ggplot2)
library(scales)
data("diamonds")
First bar-plot for cut
p<- ggplot(diamonds, aes(x = cut)) +
geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) +
scale_y_continuous(labels = percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label =
scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Cut") + theme(plot.title = element_text(hjust = 0.5, size=14,
face="bold")) +
xlab("Cut") +
ylab("Percent") +
theme(legend.position="bottom")
Second bar-plot for clarity
p1<- ggplot(diamonds, aes(x = clarity)) +
geom_bar(aes(y = (..count..)/sum(..count..), fill=clarity)) +
scale_y_continuous(labels = percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label =
scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Clarity") + theme(plot.title = element_text(hjust = 0.5, size=14,
face="bold")) +
xlab("Clarity") +
ylab("Percent") +
theme(legend.position="bottom")
Arranging bar-plot with different scales
grid.arrange(p,p1, ncol = 2)
different scales but I would like for example both at 40% top
If scales weren't percentages I would do this:
p<- ggplot(diamonds, aes(x = cut)) +
geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) +
scale_y_continuous(labels = percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Cut") + theme(plot.title = element_text(hjust = 0.5, size=14, face="bold")) +
xlab("Cut") +
ylab("Percent") +
ylim(0, 40)
theme(legend.position="bottom")
But here, of course, it doesn't work and returns this:
Ok I found a way, here the code for Cut for a % scale limited to 60%
p<- ggplot(diamonds, aes(x = cut)) +
geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) +
geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Diamonds Cut") + theme(plot.title = element_text(hjust = 0.5, size=14, face="bold")) +
scale_y_continuous(labels = scales::percent, limits=c(0,0.6)) + labs(y="Percent")
xlab("Cut") +
theme(legend.position="bottom")
p
I'm trying to plot a bar that uses 3 data sets. This bar chart, p4, is a summary of the previous 3 bar charts, The first 3 graphs work, its p4 that is the issue. Once i tried making it i got the error:
Error: stat_bin() must not be used with a y aesthetic
I'm not sure what the error could be, i'm very new to r and ggplot2.
Code:
library(ggplot2)
p<-ggplot(data=english, aes(x = reorder(V1, -V3), y=V3))+
geom_bar(stat="identity",
fill="steelblue",
color="black",
width = 0.8,
position = position_dodge(width = 0.9)) +
ggtitle("English") +
theme(plot.title = element_text(hjust = 0.5)) +
labs(x="Letter", y="Frequency") +
coord_flip() +
scale_y_continuous(limits = c(0,max(english[,"V3"])+3)) +
geom_text(aes(label=V3), vjust=0.4, hjust=-0.1, size=3.5)
p2<-ggplot(data=french, aes(x = reorder(V1, -V3), y=V3))+
geom_bar(stat="identity",
fill="steelblue",
color="black",
width = 0.8,
position = position_dodge(width = 0.9)) +
ggtitle("French") +
theme(plot.title = element_text(hjust = 0.5)) +
labs(x="Letter", y="Frequency") +
coord_flip() +
scale_y_continuous(limits = c(0,max(french[,"V3"])+3)) +
geom_text(aes(label=V3), vjust=0.4, hjust=-0.1, size=3.5)
p3<-ggplot(data=german, aes(x = reorder(V1, -V3), y=V3))+
geom_bar(stat="identity",
fill="steelblue",
color="black",
width = 0.8,
position = position_dodge(width = 0.9)) +
ggtitle("German") +
theme(plot.title = element_text(hjust = 0.5)) +
labs(x="Letter", y="Frequency") +
coord_flip() +
scale_y_continuous(limits = c(0,max(german[,"V3"])+3)) +
geom_text(aes(label=V3), vjust=0.4, hjust=-0.1, size=3.5)
p4<-ggplot(aes(x=V1, y=V3))+
geom_bar(data=english,
stat="identity",
fill="steelblue",
color="black",
width = 0.8,
position = position_dodge(width = 0.9))+
geom_bar(data=french,
stat="identity",
fill="green",
color="black",
width = 0.8,
position = position_dodge(width = 0.9))+
geom_bar(data=german,
stat="identity",
fill="red",
color="black",
width = 0.8,
position = position_dodge(width = 0.9))+
ggtitle("Laguages") +
theme(plot.title = element_text(hjust = 0.5)) +
labs(x="Letter", y="Frequency")
require(gridExtra)
grid.arrange(p, p2, p3, p4, ncol=2)
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()