How to overlay density ggplots from different datasets in R? - r

I have three ggplots (g1, g2, g3).
They are all from different datasets, and they each have the same xlim and ylim.
I would like to plot them all on one page and overlay them.
I have only found resources online explaining how to plot multiple density plots from the same dataset on the same page.
Is there code I can write so that all subsequent plots are plotted on the same page?

As #Phil pointed out you can't overlay different plots. However, you can make one plot containing all three density plots. (; Using mtcars and mpg as example datasets try this:
library(ggplot2)
ggplot() +
geom_density(aes(mpg, fill = "data1"), alpha = .2, data = mtcars) +
geom_density(aes(hwy, fill = "data2"), alpha = .2, data = mpg) +
scale_fill_manual(name = "dataset", values = c(data1 = "red", data2 = "green"))

Related

how to add a fitted distribution to a histogram

i am trying to add a fitted distribution to the histogram, but after I run it, it is just a straight line. How can i get a density line?
hist(data$price) lines(density(data$price)), lwd = 2, col ="red")
You are using graphics function hist. Use MASS function truehist instead
MASS::truehist(data$price)
lines(density(data$price)), lwd = 2, col ="red")
#Chriss gave a good solution--it does produce a density curve on top of the histogram; however, it changes the y-axis so that you only see the density values (losing the count values).
Here is an alternate solution that will place the frequency counts on the left-side y-axis and add density as a right-side y-axis. Tweak code as needed for things like bins, color, etc. I'm using the mtcars data as an example since there was no code or data provided in the question to replicate. In addition to the two libraries used here (ggpubr and cowplot), you may need to use some ggplot functions to better customize these plot options.
Code for this solution was modified from https://www.datanovia.com/en/blog/ggplot-histogram-with-density-curve-in-r-using-secondary-y-axis/
# packages needed
library(ggpubr)
library(cowplot)
# load data (none provided in the original question)
data("mtcars")
# create histogram (I have 10 bins here, but you may need a different amount)
phist <- gghistogram(mtcars, x="hp", bins=10, fill="blue", ylab="Count (blue)") + ggtitle("Car Horsepower Histogram")
# create density plot, removing many plot elements
pdens <- ggdensity(mtcars, x="hp", col="red", size=2, alpha = 0, ylab="Density (red)") +
scale_y_continuous(expand = expansion(mult = c(0, 0.05)), position = "right") +
theme_half_open(11, rel_small = 1) +
rremove("x.axis")+
rremove("xlab") +
rremove("x.text") +
rremove("x.ticks") +
rremove("legend")
# overlay and display the plots
aligned_plots <- align_plots(phist, pdens, align="hv", axis="tblr")
ggdraw(aligned_plots[[1]]) + draw_plot(aligned_plots[[2]])

How can I create a plot grid from plots created with different data with different aesthetic length?

I tried to create a plot grid that shows all the interactions from my model in separate panels with ggarrange from the ggpubr package and with plot_grid from the cowplot package.
In both cases I get the error message: "Aesthetics must be either length 1 or the same as the data (1068): colour, linetype". Is there a way to arrange plots with different aeshetic lengths?
Here is a code sample for two plots and my attempt at arranging them. Each plot looks fine on its own, but can't be arranged together. The data used for the first have 8 rows and those used for plot 2 have 1068 rows:
library(ggplot2)
library(cowplot)
library(ggpubr)
# Interaction 1
plot1 = ggplot(aes(x=age,y=pred),data=newdat)+
geom_line(data=newdat,aes(x=age,y=pred,colour = newdat$weaned,linetype = newdat$weaned),size=1)+
scale_color_manual(values = c("grey20", "blue"))+
scale_linetype_manual(values=c("dashed", "solid"))
# Interaction 2
plot2 = ggplot(aes(x=NPI,y=pred),data=newdat2)+ fig+
geom_line(data=newdat2,aes(x=NPI,y=pred,colour = as.factor(newdat$density),linetype = as.factor(newdat$density)),size=1)+
scale_color_manual(values = c("blue", "grey20", "black"))
#
# Try to combine 2 plots in a grid
plot_grid(plot2, plot1,labels = c("A","B"), align = "h", ncol = 2)
ggarrange(plot2, plot1,labels = c("A", "B"),ncol = 2, nrow = 2)
You can use the patchwork package to combine multiple plots. It uses a very intuitive approach to combining plots.
FOr eg. if you have plot1 and plot2, then plot1 + plot2 will produce a new plot with these two side by side, while plot1 / plot2 will produce a plot with one below the other.
Check it out: Getting Started with Patchwork

Why is this plotting as 2 separate plots? Adding geom_encircle breaks it out into a separate plot, but I want them on the same plot

Why is this plotting as 2 separate plots? Adding geom_encircle breaks it out into a separate plot, but I want them on the same plot:
I'm plotting a dataframe with the ggcyto wrapper for ggplot, and I want to circle the datapoints of a subset of this dataframe which I have split off into a different dataframe. But it keeps plotting two different plots: one with the scatterplot/density plot, and one with the circle. ggplot should be able to plot two different dataframes in the same plot right, so what gives?
ggcyto(data = fcsSet[[1]],
aes(x = 55, y = 59)) +
geom_hex(bins=300) +
stat_density_2d(colour = "black",bins=100,size=.2)+
xlim(-.1,4) +
ylim(-.1,4) +
scale_x_continuous(trans = "log10", limits = c(NA,4)) +
scale_x_continuous(trans = "log10", limits = c(NA,4)) +
geom_encircle(data = splitff[[4]])

R: overlying trajectory plot and scatter plot

I'm working with ggplot2 and trajectory plots, plots whom are like scatter plots, but with lines that connect points due a specific rule.
My goal is to overlay a trajectory plot with a scatter plot, and each of them has different data.
First of all, the data:
# first dataset
ideal <- data.frame(ideal=c('a','b')
,x_i=c(0.3,0.8)
,y_i=c(0.11, 0.23))
# second dataset
calculated <- data.frame(calc = c("alpha","alpha","alpha")
,time = c(1,2,3)
,x_c = c(0.1,0.9,0.3)
,y_c = c(0.01,0.26,0.17)
)
Creating a scatter plot with the first one is easy:
library(ggplot2)
ggplot(calculated, aes(x=x_c, y=y_c)) + geom_point()
After that, I created the trajectory plot, using this helpful link:
library(grid)
library(data.table)
qplot(x_c, y_c, data = calculated, color = calc, group = calc)+
geom_path (linetype=1, size=0.5, arrow=arrow(angle=15, type="closed"))+
geom_point (data = calculated, colour = "red")+
geom_point (shape=19, size=5, fill="black")
With this result:
How can I overlay the ideal data to this trajectory plot (without trajectory of course, they should be only points)?
Thanks in advance!
qplot isn't usually recommended. Here's how you could plot the two dataframes. However, ggplot might work better for you if the dataframes were merged, and you had an x and y column, with an additional method column containing with calculated or ideal.
library(ggplot2)
ideal <- data.frame(ideal=c('a','b')
,x_i=c(0.3,0.8)
,y_i=c(0.11, 0.23)
)
# second dataset
calculated <- data.frame(calc = c("alpha","alpha","alpha")
,time = c(1,2,3)
,x_c = c(0.1,0.9,0.3)
,y_c = c(0.01,0.26,0.17)
)
ggplot(aes(x_c, y_c, color = "calculated"), data = calculated) +
geom_point( size = 5) +
geom_path (linetype=1, size=0.5, arrow = arrow(angle=15, type="closed"))+
geom_point(aes(x_i, y_i, color = "ideal"), data = ideal, size = 5) +
labs(x = "x", y = "y", color = "method")

R: Density plot with colors by group?

I have data from 2 populations.
I'd like to get the histogram and density plot of both on the same graphic.
With one color for one population and another color for the other one.
I've tried this (example):
library(ggplot2)
AA <- rnorm(100000, 70,20)
BB <- rnorm(100000,120,20)
valores <- c(AA,BB)
grupo <- c(rep("AA", 100000),c(rep("BB", 100000)))
todo <- data.frame(valores, grupo)
ggplot(todo, aes(x=valores, fill=grupo, color=grupo)) +
geom_histogram(aes(y=..density..), binwidth=3)+ geom_density(aes(color=grupo))
But I'm just getting a graphic with a single line and a single color.
I would like to have different colors for the the two density lines. And if possible the histograms as well.
I've done it with ggplot2 but base R would also be OK.
or I don't know what I've changed and now I get this:
ggplot(todo, aes(x=valores, fill=grupo, color=grupo)) +
geom_histogram( position="identity", binwidth=3, alpha=0.5)+
geom_density(aes(color=grupo))
but the density lines were not plotted.
or even strange things like
I suggest this ggplot2 solution:
ggplot(todo, aes(valores, color=grupo)) +
geom_histogram(position="identity", binwidth=3, aes(y=..density.., fill=grupo), alpha=0.5) +
geom_density()
#skan: Your attempt was close but you plotted the frequencies instead of density values in the histogram.
A base R solution could be:
hist(AA, probability = T, col = rgb(1,0,0,0.5), border = rgb(1,0,0,1),
xlim=range(AA,BB), breaks= 50, ylim=c(0,0.025), main="AA and BB", xlab = "")
hist(BB, probability = T, col = rgb(0,0,1,0.5), border = rgb(0,0,1,1), add=T)
lines(density(AA))
lines(density(BB), lty=2)
For alpha I used rgb. But there are more ways to get it in. See alpha() in the scales package for instance. I added also the breaks parameter for the plot of the AAs to increase the binwidth compared to the BB group.

Resources