coord_trans and coord_flip in same plot - r

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

Related

ggplot multiple boxplots and stat_summary position

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)

"Thermal Bar" plot in R?

I want to create a plot with a single vertical bar (colored continuously), with a mark on it showing the score for a particular person. Image:
I can generate the colored bar in ggplot, but only as a legend (not the actual plot). For example the legend resulting from the following is fine:
ggplot(mtcars, aes(x=wt, y=mpg, color=mpg)) +
geom_point() +
scale_color_gradientn(colors = rainbow(5))
Is there any way to do this? Any help would be really appreciated - I'm completely stuck on this.
ggplot(data.frame(y = 51), aes( y=y)) +
geom_tile(data = data.frame(y = 0:100),
aes(x= 0.5, y = y, fill = y)) +
geom_segment(aes(x=0, xend=1, yend=y)) +
geom_text(aes(label = y, x = 1), hjust = -0.3) +
coord_cartesian(clip = "off", xlim = c(0,1.2)) +
scale_fill_gradientn(colors = rainbow(5)) +
scale_x_continuous(labels = NULL) +
guides(fill = FALSE) +
theme_minimal() +
theme(line = element_blank()) +
labs(x="", y = "")

writing the x-axis label on all facets without scaling

I want to write the x-axis on all facets but without the scale.
The output
I tried to use scales='free_x' with scale_x_discrete(drop=FALSE) but it didn't work .
plot2 <- ggplot(counter2, aes(x = as.character(week) , y = freq,group=Delivery.time)) +
geom_point(colour = "red") + geom_line(colour = "red") +
labs(x = "Month", y = "Number Of Customers") +
facet_grid(.~Delivery.time) +
theme(axis.text.x = element_text(angle = 45, hjust=1)) +
geom_text(aes(label = freq),size = 2.5, hjust = .5, vjust=-0.5) +
facet_wrap(~Delivery.time, ncol = 1, scales = x) +
scale_x_discrete(drop=FALSE)
Plot with scaled x-axis
I want all a-axis

Add confidence interval with labels to a bar plot

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)

How to get rid of whitespace in a ggplot2 plot?

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

Resources