I am constructing the following graph but the acceptance and rejection labels do not look good and I tried anyway and nothing
p1 <- ggplot(data = data.frame(x = c(0, 4)), aes(x))
p1<-p1+stat_function(fun = dnorm, n = 49, args =list(mean = 2, sd = 3/7),geom = "area",fill="blue2",alpha=0.5,aes(color="aceptacion"))
p1<-p1 +stat_function(fun = dnorm, args = list(mean = 2, sd = 3/7), xlim = c(2.905, 4),geom ="area", fill = "red", alpha = 0.5,aes(color="rechazo"))
p1
How do I correct it?
Try this
mycolor<- c("blue2","red")
myvalues <- c("aceptacion","rechazo")
df <- data.frame(x = c(0, 4))
p1 <- ggplot(data = df, aes(x))
p1 <- p1+stat_function(fun = dnorm, n = 49, args =list(mean = 2, sd = 3/7),xlim = c(0, 2.905) ,geom = "area",fill="blue2",alpha=0.5)
p1 <- p1 +stat_function(fun = dnorm, args = list(mean = 2, sd = 3/7), xlim = c(2.905, 4),geom ="area", fill = "red")
p1 <- p1 + aes(color=myvalues)
p1 <- p1 + scale_color_manual(name="", values=mycolor)
p1 <- p1 + guides(color= guide_legend(override.aes=list(fill=mycolor)))
p1
You get the following output:
Related
I have 2 normal probability plots as below
library(ggplot2)
# Plot 1
ggplot(data.frame(x = c(-4, 4)), aes(x)) +
stat_function(fun = dnorm, args = list(mean = 0, sd = 1), col='red') +
stat_function(fill='red', fun = dnorm, xlim = c(-4, -1), geom = "area") +
stat_function(fill='red', fun = dnorm, xlim = c(-1, 4), geom = "area", alpha = 0.3)
# Plot 2
ggplot(data.frame(x = c(-4, 4)), aes(x)) +
stat_function(fun = dnorm, args = list(mean = 0, sd = 2), col='blue') +
stat_function(fill='blue', fun = dnorm, args = list(mean = 0, sd = 2), xlim = c(-4, -1), geom = "area") +
stat_function(fill='blue', fun = dnorm, args = list(mean = 0, sd = 2), xlim = c(-1, 4), geom = "area", alpha = 0.3)
Individually they are just fine. However I wanted to combine these 2 plots and place them in same plot window with same x-axis.
I also want to add a legend based on fill color in the combined plot to distinguish them.
Is there any way to achieve this with ggplot?
Any pointer will be very helpful
You could combine both the stat_functions and create two with an aes for your fill to create a legend with scale_fill_manual like this:
library(ggplot2)
ggplot(data.frame(x = c(-4, 4)), aes(x)) +
stat_function(fun = dnorm, args = list(mean = 0, sd = 1), col='red') +
stat_function(fun = dnorm, xlim = c(-4, -1), geom = "area", aes(fill = "plot 1")) +
stat_function(fill='red', fun = dnorm, xlim = c(-1, 4), geom = "area", alpha = 0.3) +
stat_function(fun = dnorm, args = list(mean = 0, sd = 2), col='blue') +
stat_function(fun = dnorm, args = list(mean = 0, sd = 2), xlim = c(-4, -1), geom = "area", aes(fill = "plot 2")) +
stat_function(fill='blue', fun = dnorm, args = list(mean = 0, sd = 2), xlim = c(-1, 4), geom = "area", alpha = 0.3) +
scale_fill_manual(name = "Legend", values = c("red", "blue"))
Created on 2022-12-31 with reprex v2.0.2
I would like to add jpeg picture in a place of 'ao' in the following figure
library(ggplot2)
da <- data.frame(x = seq(0, 100, 1), y = seq(0, 1, 0.01))
ao <- ggplot()
a1 <- ggplot(aes(x = x, y = y), data = da) + geom_line()
a2 <- ggplot(aes(x = x, y = y), data = da) + geom_point()
a3 <- ggplot(aes(x = x, y = y), data = da) + geom_line(lty = 2)
library(cowplot)
plot_grid(ao, a1, a2, a3, labels=c("a)", "c)", "b)", "d)"), ncol = 2, nrow = 2)
I am not restricted to ´cowplot´ library. anything that works is fine.
Ok, I finally solved it like that
library(ggplot2)
ao <- ggplot()
image <- load.image("FIGURE/xxxx.png")
library(magick)
a0 <- ggdraw(a0) + draw_image(image)
da <- data.frame(x = seq(0, 100, 1), y = seq(0, 1, 0.01))
a1 <- ggplot(aes(x = x, y = y), data = da) + geom_line()
a2 <- ggplot(aes(x = x, y = y), data = da) + geom_point()
a3 <- ggplot(aes(x = x, y = y), data = da) + geom_line(lty = 2)
library(cowplot)
plot_grid(ao, a1, a2, a3, labels=c("a)", "c)", "b)", "d)"), ncol = 2, nrow = 2)
I have a data:
df_1 <- data.frame(
x = replicate(
n = 2, expr = rnorm(n = 3000, mean = 100, sd = 10)
),
y = sample(x = 1:3, size = 3000, replace = TRUE)
)
And the follow function:
library(tidyverse)
ggplot(data = df_1, mapping = aes(x = x.1, fill = x.1)) +
geom_histogram(color = 'black', bins = 100) +
scale_fill_continuous(low = 'blue', high = 'red') +
theme_dark()
scale_fill_continuous doesn't work. The graph is black and gray.
Tks.
The problem, I think, is that there are nrow(df_1) values for fill, but only 100 are needed. This could be solved by pre-calculating the bin positions and counts and plotting with geom_col, but a neater solution is to use stat. stat is supposed to be for computed variables (e.g. stat(count) - see ?geom_histogram) but we can give it the vector 1:nbin and it works.
df_1 <- data.frame(
x = replicate(n = 2, expr = rnorm(n = 3000, mean = 100, sd = 10)),
y = sample(x = 1:3, size = 3000, replace = TRUE)
)
library(tidyverse)
nbins <- 100
ggplot(data = df_1, mapping = aes(x = x.1, fill = stat(1:nbins))) +
geom_histogram(bins = nbins) +
scale_fill_continuous(low = "red", high = "blue")
Created on 2020-01-19 by the reprex package (v0.3.0)
The aes fill should be stat(count) rather than x.1
ggplot(data = df_1, mapping = aes(x = x.1, fill = stat(count))) +
geom_histogram(color = 'black', bins = 100) +
scale_fill_continuous(type = "gradient", low = "blue", high = "red") +
theme_dark()
I am trying to get two contours in the same plot using ggplot2 in R.
Here is a reproducible example:
library(MASS)
library(ggplot2)
# first contour
m <- c(.0, -.0)
sigma <- matrix(c(1,.5,.5,1), nrow=2)
data.grid <- expand.grid(s.1 = seq(-3, 3, length.out=200), s.2 = seq(-3, 3, length.out=200))
q.samp <- cbind(data.grid, prob = mvtnorm::dmvnorm(data.grid, mean = m, sigma = sigma))
plot1 <- ggplot(q.samp, aes(x = s.1, y = s.2, z = prob)) +
stat_contour(color = 'green')
# second contour
m1 <- c(1, 1)
sigma1 <- matrix(c(1,-.5,-.5,1), nrow=2)
set.seed(10)
data.grid1 <- expand.grid(s.1 = seq(-3, 3, length.out=200), s.2 = seq(-3, 3, length.out=200))
q.samp1 <- cbind(data.grid1, prob = mvtnorm::dmvnorm(data.grid1, mean = m1, sigma = sigma1))
plot2 <- ggplot(q.samp1, aes(x = s.1, y = s.2, z = prob)) +
stat_contour(color = 'red')
However, trying plot1 + plot2 also does not work. Is there a way to get the two contours on the same plot.
What about including another stat_contour with different data?
ggplot(q.samp1, aes(x = s.1, y = s.2, z = prob)) +
stat_contour(color = 'red') +
stat_contour(data = q.samp, aes(x = s.1, y = s.2, z = prob), color = 'green')
I am trying to use a log-modulus transformation in my plot. It was working fine...
library(tidyverse)
library(scales)
log_modulus_trans <- function()
trans_new(name = "log_modulus",
transform = function(x) sign(x) * log(abs(x) + 1),
inverse = function(x) sign(x) * ( exp(abs(x)) - 1 ))
# fake data
set.seed(1)
d <- data_frame(
tt = rep(1:10, 3),
cc = rep(LETTERS[1:3], each = 10),
xx = c(rnorm(10, mean = 100, sd = 10),
rnorm(10, mean = 0, sd = 10),
rnorm(10, mean = -100, sd = 10)))
ggplot(data = d,
mapping = aes(x = tt, y = xx, group = cc)) +
geom_line() +
coord_trans(y = "log_modulus")
When I tried to add a geom_vline() things got weird...
ggplot(data = d,
mapping = aes(x = tt, y = xx, group = cc)) +
geom_line() +
coord_trans(y = "log_modulus") +
geom_vline(xintercept = 5)
Any idea how to get geom_vline() to go from the top to the bottom of the plot window... or a work around hack?
Here is a solution using geom_segment
ggplot(data = d,
mapping = aes(x = tt, y = xx, group = cc)) +
geom_line() +
geom_segment(x = 5, xend = 5, y = -150, yend = 150) +
coord_trans(y = "log_modulus")