I have a density plot with different colors per group, where I would like to add a marginal boxplot per group at the top. But the grouping is not done correctly in the boxplots, both show the same data.
set.seed(123)
library(ggplot2)
library(ggExtra)
library(data.table)
Data <- data.table(x = rnorm(100),
group = rep(c("group1", "group2"), times = c(30, 70)))
Data[group == "group1", x := x + 3]
p <-
ggplot(Data, aes(x = x, fill = group, colour = group)) +
geom_density(alpha = 0.5)
p %>% ggMarginal(type = "boxplot",
margins = "x",
size = 5,
groupColour = TRUE,
groupFill = TRUE)
UPDATE:
With geom_point it does work:
p <-
ggplot(Data, aes(x = x, y = x, fill = group, colour = group)) +
geom_point(alpha = 0.5)
p %>% ggMarginal(type = "boxplot",
margins = "x",
size = 5,
groupColour = TRUE,
groupFill = TRUE)
So, why is it not working with geom_density?
As noted in the help file for ?ggMarginal, the function expects a ggplot2 scatterplot in p.
The following would work:
p2 <- ggplot(Data, aes(x = x, fill = group, colour = group)) +
geom_point(aes(y = 0.1), alpha = 0) + # add an invisible scatterplot geom as the first layer
geom_density(alpha = 0.5)
p2 %>% ggMarginal(type = "boxplot",
margins = "x",
size = 5,
groupColour = TRUE,
groupFill = TRUE)
Related
I have code that plots paired points and also graphs half violin and box plots of pretest and posttest scores. I added lines between the paired points, but I am having difficulty with adding an arrow to the lines and changing the color depending on whether the score decreased or increased.
Below is my code and an image of my graph.
f5 <- ggplot(data = d, aes(y = y)) +
#Add geom_() objects
geom_point(data = d %>% filter(x =="1"), aes(x = xj), color = 'dodgerblue', size = 1.5,
alpha = .6) +
geom_point(data = d %>% filter(x =="2"), aes(x = xj), color = 'darkorange', size = 1.5,
alpha = .6) +
geom_line(aes(x = xj, group = id), color = 'lightgray', alpha = .3) +
geom_half_boxplot(
data = d %>% filter(x=="1"), aes(x=x, y = y), position = position_nudge(x = -.25),
side = "r",outlier.shape = NA, center = TRUE, errorbar.draw = FALSE, width = .2,
fill = 'dodgerblue') +
geom_half_boxplot(
data = d %>% filter(x=="2"), aes(x=x, y = y), position = position_nudge(x = .15),
side = "r",outlier.shape = NA, center = TRUE, errorbar.draw = FALSE, width = .2,
fill = 'darkorange') +
geom_half_violin(
data = d %>% filter(x=="1"),aes(x = x, y = y), position = position_nudge(x = -.3),
side = "l", fill = 'dodgerblue') +
geom_half_violin(
data = d %>% filter(x=="2"),aes(x = x, y = y), position = position_nudge(x = .3),
side = "r", fill = "darkorange") +
#Define additional settings
scale_x_continuous(breaks=c(1,2), labels=c("Pre-Test", "Post-Test"), limits=c(0, 3)) +
xlab("Test") + ylab("Score") +
ggtitle("Before and After Scores with box- and violin plots") +
theme_classic()+
coord_cartesian(ylim=c(y_lim_min, y_lim_max))
I want to create a graph where I can change the line size for each line c(1,2,3) and the alpha values for each line c(0.5,0.6,0.7). I tried to use scale_size_manual but it didn't make any difference. Any ideas on how to proceed?
var <- c("T","T","T","M","M","M","A","A","A")
val <- rnorm(12,4,5)
x <- c(1:12)
df <- data.frame(var,val,x)
ggplot(aes(x= x , y = val, color = var, group = var), data = df) +
scale_color_manual(values = c("grey","blue","black")) + geom_smooth(aes(x = x, y = val), formula = "y ~ x", method = "loess",se = FALSE, size = 1) + scale_x_continuous(breaks=seq(1, 12, 1), limits=c(1, 12)) + scale_size_manual(values = c(1,2,3))
To set the size and alpha values for your lines you have to map on aesthetics. Otherwise scale_size_manual will have no effect:
library(ggplot2)
ggplot(aes(x = x, y = val, color = var, group = var), data = df) +
scale_color_manual(values = c("grey", "blue", "black")) +
geom_smooth(aes(x = x, y = val, size = var, alpha = var), formula = "y ~ x", method = "loess", se = FALSE) +
scale_x_continuous(breaks = seq(1, 12, 1), limits = c(1, 12)) +
scale_size_manual(values = c(1, 2, 3)) +
scale_alpha_manual(values = c(.5, .6, .7))
I need to make a graph with multiple kinds of data on it, and I'm plotting one type of data with lines and one type with points. I've added a manually specified legend to show which type is points and which is lines (admittedly, my approach is a bit hacky), and that's working except for the legend order. Here's a dummy example:
DF1 <- data.frame(X = 1:10,
Y = c(1:10*0.5, 1:10*0.25),
Fruit = rep(c("mango", "kiwi"), each = 10))
DF2 <- data.frame(X = 1:10,
Y = c(1:10*2, 1:10*4),
Cat = rep(c("tabby", "calico"), each = 10))
Empty <- data.frame(X = mean(DF$X),
Y = as.numeric(NA),
# Q = c(0, 1))
Type = c("Cat", "Fruit"))
Mygraph <- ggplot(DF1, aes(x = X, y = Y, color = Fruit)) +
geom_point() +
geom_line(data = DF2, aes(x = X, y = Y, linetype = Cat),
inherit.aes = F) +
labs(color = NULL, linetype = NULL) +
geom_point(data = Empty, aes(x = X, y = Y, alpha = Type),
inherit.aes = F) +
geom_line(data = Empty, aes(x = X, y = Y, alpha = Type),
inherit.aes = F) +
scale_alpha_manual(
name = "Type of item", values = c(1, 1),
breaks = c("Fruit", "Cat"),
guide = guide_legend(override.aes =
list(linetype = c("blank", "solid"),
shape = c(16, NA)))) +
theme_bw()
Mygraph
This graph looks pretty good:
But check out what happens to the "Type of item" bit when I try to specify the order:
Mygraph +
guides(alpha = guide_legend(order = 1),
linetype = guide_legend(order = 2),
color = guide_legend(order = 3))
Why do my specified aesthetics go away? How can I both specify what that part of the legend should look like and also specify that the order of the three parts of the legend should be 1. alpha, 2. linetype, and then 3. color?
You were attempting to override aesthetics for alpha in two places (ie guides() and scale_alpha...()), and ggplot was choosing to just interpret one of them. I suggest including your shape override with your legend order override, like this:
library(ggplot2)
ggplot(DF1, aes(x = X, y = Y, color = Fruit)) +
geom_point() +
geom_line(data = DF2, aes(x = X, y = Y, linetype = Cat), inherit.aes = F) +
labs(color = NULL, linetype = NULL) +
geom_point(data = Empty, aes(x = X, y = Y, alpha = Type), inherit.aes = F) +
geom_line(data = Empty, aes(x = X, y = Y, alpha = Type), inherit.aes = F) +
scale_alpha_manual(name = "Type of item", values = c(1, 1), breaks = c("Fruit", "Cat")) +
guides(alpha = guide_legend(order = 1,
override.aes=list(linetype = c("blank", "solid"),
shape = c(16,NA))),
linetype = guide_legend(order = 2),
color = guide_legend(order = 3)) +
theme_bw()
data:
DF1 <- data.frame(X = 1:10,
Y = c(1:10*0.5, 1:10*0.25),
Fruit = rep(c("mango", "kiwi"), each = 10))
DF2 <- data.frame(X = 1:10,
Y = c(1:10*2, 1:10*4),
Cat = rep(c("tabby", "calico"), each = 10))
Empty <- data.frame(X = mean(DF1$X),
Y = as.numeric(NA),
Type = c("Cat", "Fruit"))
I struggling on how I can plot my real values, present in the real_values vector, next to the estimates values. My problem here is that the estimates values have a range (via the geom_errorbar), and for the real values I would like to plot just the point, in black, on the left side of each of the 10 estimates.
Here's an example of what I tried:
est_values = rnorm(20)
real_values = rnorm(10)
dat_ex = data.frame(
xvalues = 1:10,
values = est_values,
method = c(rep("A",10),rep("B",10)),
ic_0.025 = c(est_values - rnorm(20,1,0.1)),
ic_0.975 = c(est_values + rnorm(20,1,0.1)))
ggplot(dat_ex) +
#geom_point(aes(x = 1:10, y= real_values), size = 2) +
geom_point(aes(x = xvalues, y= values, group = method, colour = method), position=position_dodge(.9), size = 3) +
geom_errorbar(aes(x = xvalues, y= values, group = method, colour = method,ymin = ic_0.025, ymax = ic_0.975), size = 1.3,position=position_dodge(.9), width = .2)
ggplot generally works best with data in data frames. So we put your real_values in a data frame and plot them in a separate layer, and "nudge" them to the left, as requested:
ggplot(dat_ex) +
geom_point(aes(x = xvalues, y= values, group = method, colour = method), position=position_dodge(.9), size = 3) +
geom_errorbar(aes(x = xvalues, y= values, group = method, colour = method,ymin = ic_0.025, ymax = ic_0.975), size = 1.3,position=position_dodge(.9), width = .2) +
geom_point(
data = data.frame(values = real_values, xvalues = dat_ex$xvalues),
aes(x = xvalues, y = values),
position = position_nudge(x = -.4),
color = "black")
A nicer method might be to put them all in the same data frame. This can simplify the code and will automatically put them in the legend.
library(dplyr)
dat_ex = data.frame(
xvalues = 1:10,
values = real_values,
method = "real"
) %>%
bind_rows(dat_ex) %>%
mutate(method = factor(method, levels = c("real", "A", "B")))
ggplot(dat_ex, aes(x = xvalues, y = values, color = method)) +
geom_point(position=position_dodge(.9), size = 3) +
geom_errorbar(aes(ymin = ic_0.025, ymax = ic_0.975, group = method),
size = 1.3, position=position_dodge(.9), width = .2) +
scale_color_manual(values = c("real" = "black", "A" = "orange", "B" = "blue"))
I would add real_values to your data as another level of method, so they will be dodged along with "A" and "B" (and included in the legend):
library(ggplot2)
dat_ex <- rbind(
dat_ex,
data.frame(
xvalues = 1:10,
values = real_values,
method = "Real",
ic_0.025 = NA_real_,
ic_0.975 = NA_real_
)
)
# arrange so "Real" is on the left
dat_ex$method <- factor(dat_ex$method, levels = c("Real", "A", "B"))
ggplot(dat_ex) +
geom_point(aes(x = xvalues, y= values, group = method, colour = method), position=position_dodge(.9), size = 3) +
geom_errorbar(aes(x = xvalues, y= values, group = method, colour = method,ymin = ic_0.025, ymax = ic_0.975), size = 1.3,position=position_dodge(.9), width = .2) +
scale_colour_manual(values = c("black", "forestgreen", "royalblue"))
In this SO answer, user #Crops shows how to add a legend to a ggalt::geom_dumbbell plot. Very nice.
library(ggalt)
df <- data.frame(trt=LETTERS[1:5], l=c(20, 40, 10, 30, 50), r=c(70, 50, 30, 60, 80))
df2 = tidyr::gather(df, group, value, -trt)
ggplot(df, aes(y = trt)) +
geom_point(data = df2, aes(x = value, color = group), size = 3) +
geom_dumbbell(aes(x = l, xend = r), size=3, color="#e3e2e1",
colour_x = "red", colour_xend = "blue",
dot_guide=TRUE, dot_guide_size=0.25) +
theme_bw() +
scale_color_manual(name = "", values = c("red", "blue") )
I want to sort trt descending on r. I tried replacing y = trt with y = reorder(trt, r), but I get an error that object r is not found.
Here is a way where we reorder the factor levels of trt in df and df2 before we plot.
# reorder factor levels
df$trt <- reorder(df$trt, df$r)
df2$trt <- factor(df2$trt, levels = levels(df$trt))
ggplot(df, aes(y = trt)) +
geom_point(data = df2, aes(x = value, color = group), size = 3) +
geom_dumbbell(aes(x = l, xend = r), size=3, color="#e3e2e1",
colour_x = "red", colour_xend = "blue",
dot_guide=TRUE, dot_guide_size=0.25) +
theme_bw() +
scale_color_manual(name = "", values = c("red", "blue") )
Using the dumbbell package
##Reformat data
df3<-df %>% arrange(r)
df2<-df%>% mutate("key"="trt")
df2$trt<-factor(df2$trt,df3$trt)
##plot
dumbbell::dumbbell(df2, id="trt", column1="l", column2="r",key="key", delt =1, textsize=3, lab1 = "l", lab2="r", pt_val = 1, pointsize = 3,pt_alpha = 0.6, arrow=1, leg = "Add legend title", pval=2) + xlim(8,85) + facet_wrap(key ~.)
Added in some bells and whistles, you can remove them toggling with the options.
I dont have enough points to embed for here is the link. Hope someone finds it useful.