Overlaying stripchart on barplot - how to align columns? - r

I have a barplot that I have overlaid with a scatterplot using stripchart.
Barplot(data1means)
stripchart(data1, add=TRUE, vertical = TRUE)
However, the points on the scatterplot are misaligned with the bars on the barplot, as shown here:
.
So how do I alter the spacing of the scatter plot so they match up? As I understand it, stripchart doesn't have a space or width variable like barplot does.

With base graphics, you can plot points on top of the bar plot using the points function. We get the x-positions of the bars from the bar plot itself. I've also included an alternative approach where the means are plotted with point markers rather than bars:
# Fake data
set.seed(1)
dat = data.frame(group=LETTERS[1:5], y=rnorm(25,20,2))
# Assign the barplot to x so that x will contain the bar positions.
x = barplot(tapply(dat$y, dat$group, FUN=mean), ylim=c(0,1.05*max(dat$y)), col=hcl(240,100, 70))
points(rep(x, table(dat$group)), dat$y[order(dat$group)], pch=21, bg="red")
plot(rep(1:length(unique(dat$group)), table(dat$group)),
dat$y[order(dat$group)], pch=21, bg="blue",
ylim=c(0,1.05*max(dat$y)), xlim=c(0.5,5.5), xaxt="n")
points(1:length(unique(dat$group)),
tapply(dat$y, dat$group, FUN=mean),
pch="\U2013", cex=3, col="red")
axis(side=1, at=1:5, labels=LETTERS[1:5])
Here's a version of the same two plots using ggplot2.
library(ggplot2)
ggplot(dat, aes(group, y)) +
stat_summary(fun.y=mean, geom="bar", fill=hcl(240,100,50)) +
geom_point() +
theme_minimal()
ggplot(dat, aes(group, y)) +
geom_point() +
stat_summary(fun.y=mean, geom="point", pch="\U2013",
size=8, colour="red") +
scale_y_continuous(limits=c(0, max(dat$y))) +
theme_bw()

Related

R ggplot2 overlapping histogram, adding in legend for overlapping part

I have a histogram that is plotting 2 different groups with some overlap between them. I have been able to manually color the groups and a legend is generated for each group, however I am asking how to add into the legend a color and label for the overlapping part?
For example, in the above histogram I would like to add a legend for the purplish part where A and B overlap (which should be labeled as "Overlap" in the legend, underneath B).
Code for generating above histogram:
set.seed(42)
n <- 100
dat <- data.frame(id=1:n,
group=rep(LETTERS[1:2], n/2),
x=rnorm(n))
ggplot(dat, aes(x=x, fill=group)) + geom_histogram(alpha=.5, position="identity") +
scale_fill_manual(values=c("blue","red"))
A partially overlap solution
Sample code:
library(ggplot2)
ggplot(dat, aes(x=x, fill=group)) +
geom_histogram(position = position_dodge(width = 0.6))+
scale_fill_manual(values=c("blue","red"))+
scale_y_continuous(expand=c(0,0))+
theme_bw()
Plot:

ggplot2: Varying facet width with independent `Y` axes

Dummy data
d = data.frame(
x = factor(LETTERS[c(1,2,3,4,1,2,3,4,1,2,1,2,1,2,1,2)]),
y = c(100,80,70,60,130,90,65,60,2,3,3,3,2,2,1,2),
grid = rep(letters[1:2], each=8)
)
Issue
ggplot(d, aes(x=x, y=y)) + facet_grid(~grid, scales="free",space="free_x") + geom_point()
I like this graph. My only issue is that both grids use the same Y axis. So, I tried using facet_wrap instead of facet_grid and got
ggplot(d, aes(x=x, y=y)) + facet_wrap(~grid, scales="free") + geom_point()
But unfortunately, facet_wrap does not have a "space" parameter and as a result the right and the left graph are of the same width.
Question
How can I do so that the space between levels of the variable d$x is equal among both facets (leading to facets having different width) AND to have a separate Y axis for each facet. Of course, I would like to keep the facets to be aligned horizontally.
Use ggplot grob and modify the widths in the table
# Capture the plot
q = ggplot(d, aes(x=x, y=y)) + facet_grid(~grid, scales="free",space="free_x") + geom_point()
gt = ggplotGrob(q)
# Modify the widths
gt$widths[5] = unit(8, "cm")
gt$widths[9] = unit(4, "cm")
# Plot the graph
grid.newpage()
grid.draw(gt)

How to adjust the distance between the facet_grid frame and boxplots using ggplot_build & ggplot_gtable

We are presenting outcome data using boxplots and group these for different approaches using facet_grid with ggplot2 and geom_boxplot.
We would like to add more space between the boxplots and the frame of the facet_grid as shown in the graphic below.
The code we used included ggplot_build and gglot_table.
Which parameter of ggplot_build needs to be set to get more space in the panels?
require(ggplot2)
require(grid)
dat <- rbind(data.frame(approach=1,product=1,value=seq(1,20,0.5)),
data.frame(approach=1,product=2,value=seq(5,15,0.3)),
data.frame(approach=1,product=3,value=seq(5,17,0.2)),
data.frame(approach=2,product=1,value=seq(1,13,0.3)),
data.frame(approach=2,product=2,value=seq(3,18,0.5)),
data.frame(approach=2,product=3,value=seq(4,25,0.7)),
data.frame(approach=3,product=1,value=seq(1,15,0.6)),
data.frame(approach=3,product=2,value=seq(3,16,0.5)),
data.frame(approach=3,product=3,value=seq(1,10,0.1)))
dat$product<-as.factor(dat$product)
gg1<-ggplot(dat, aes(x =product, y = value)) +
geom_boxplot() +
facet_grid(cols=vars(approach))
gt = ggplot_gtable(ggplot_build(gg1))
grid.draw(gt)
ggplot(dat, aes(x =product, y = value)) +
geom_boxplot() +
coord_cartesian(xlim = c(1.2, 2, 2.8)) +
facet_grid(cols=vars(approach))

Adding titles and formatting Y-axis labels for multiple plots produced by ggplot2

I have a multiplot with 10 scatter plots produced using ggplot2. The code i have used to create the plot has been lifted from here R cookbook. My problem is that i want to add different titles for each and every scatter plot e.g., plot 1 title can be titled "plot 1", while plot 2 can be titled "plot 2" and so on and so forth. I would also want to change the labels from the current label "Y" to "purchases" for all the plots.
Just create your plots and title each one individually as the code you referenced does. Then arrange using the gridExtra package. ggtitle does the title, the ylab function can be used for the y-label.
library(ggplot2)
# This example uses the ChickWeight dataset, which comes with ggplot2
# First plot
p1 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) +
geom_line() +
ggtitle("Growth curve for individual chicks")
# Second plot
p2 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet)) +
geom_point(alpha=.3) +
geom_smooth(alpha=.2, size=1) +
ggtitle("Fitted growth curve per diet")
# Third plot
p3 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, colour=Diet)) +
geom_density() +
ggtitle("Final weight, by diet")
# Fourth plot
p4 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, fill=Diet)) +
geom_histogram(colour="black", binwidth=50) +
facet_grid(Diet ~ .) +
ggtitle("Final weight, by diet") +
theme(legend.position="none") # No legend (redundant in this graph)
require(gridExtra)
grid.arrange(p1, p2, p3, p4, nrow = 2)

overlay rotated density plot

I'm struggling to overlap rotated density plot onto the original scatterplot. Here are 2 plots I have:
require(ggplot2); set.seed(1);
df1 <- data.frame(ID=paste0('ID',1:1000), value=rnorm(1000,500,100))
p1 <- ggplot(data = df1, aes(x=reorder(ID, value), y=value)) +
geom_point(size=2, alpha = 0.7)+
coord_trans(y="log10")
p2 <- ggplot(data = df1, aes(x=value)) +
coord_trans(x="log10") +
geom_density() +
coord_flip()
p1
p2
First, there's a little problem with the density plot that its vertical axis is not log10-transformed. But main issue is that I can't find how to draw it on the previous plot keeping correct coordinates.
Because you are using coord_flip on your second plot you are effectively trying to plot two different values onto the same x axis (density and ID). There are plenty of posts discouraging this, here's one for example: How do I plot points with two different y-axis ranges on the same panel in the same X axis?.

Resources