I am trying to create a plot which contains a discrete variable in one of the axis. I am furthermore trying to group these variables with respect to another variable and represent it in a graph using ggplot2. The code I have used is as follows:
size_vs_paper %>%
ggplot(aes(x=Reference,y=S_Max)) +
theme_classic()+
geom_segment(aes(xend =Reference,yend = S_Min),size=0.5) +
#geom_text(size = 5, vjust=-3) +
geom_point(aes(group = Environment), size = 3, shape = "|", color = "black", alpha = 0.7)+
geom_point(aes(y=S_Min, group = Environment), size = 3, shape = "|", color = "black", alpha = 0.7) +
geom_point(aes(y=S_Mean, group = Environment), size = 3, color = "black", alpha = 0.7) +
facet_wrap(Environment ~ ., ncol = 1) +
scale_colour_brewer(palette="Set2") +
scale_y_log10(breaks=c(0.01,1,10,100,1000, 5000, 10000)) +
theme(axis.text = element_text(size = 10),
legend.position = "none") +
coord_flip() +
labs(y = "Size (µm)")
This yields the graph as below:
As you can see, the Y axis representing the references repeats itself for all the facets. I am looking to facet them without the references repeating themselves. Any guidance will be appreciated!
Related
I conducted some interviews and I wanted to create box plots with ggplot based on these interviews. I managed to create the box plots but I do not manage to include the outliers in the box plot. I have only a few observations and therefore I want the outliers to be part of the box plot.
This is the code that I have so far:
data_insurances_boxplot_merged <- ggplot(data_insurances_merged, aes(x = value, y = func, fill = group)) +
stat_boxplot(geom = "errorbar", width = 0.3, position = position_dodge(width = 0.75)) +
geom_boxplot() +
stat_summary(fun.y = mean, geom = "point", shape = 20, size = 3, color = "red",
position = position_dodge2(width = 0.75,
preserve = "single")) +
scale_x_continuous(breaks = seq(1, 7, 1), limits = c(1, 7)) +
scale_fill_manual(values = c("#E6645E", "#EF9C9D")) +
labs(x = "",
y = "", title = "") +
theme_light(base_size = 12) +
theme(legend.title = element_blank())
data_insurances_boxplot_merged
And this is the box plot that is generated:
Does anyone know how to achieve this?
I am including marginal distribution plots on a scatterplot of a continuous and integer variable. However, in the integer variable maringal distribution plot (y-axis) there is this zig-zag pattern that shows up because the y-values are all integers. Is there any way to increase the "width" (not sure that's the right term) of the bins/values the function calculates the distribution density over?
The goal is to get rid of that zig-zag pattern that develops because the y-values are integers.
library(GlmSimulatoR)
library(ggplot2)
library(patchwork)
### Create right-skewed dataset that has one continous variable and one integer variable
set.seed(123)
df1 <- data.frame(matrix(ncol = 2, nrow = 1000))
x <- c("int","cont")
colnames(df1) <- x
df1$int <- round(rgamma(1000, shape = 1, scale = 1),0)
df1$cont <- round(rgamma(1000, shape = 1, scale = 1),1)
p1 <- ggplot(data = df1, aes(x = cont, y = int)) +
geom_point(shape = 21, size = 2, color = "black", fill = "black", stroke = 1, alpha = 0.4) +
xlab("Continuous Value") +
ylab("Integer Value") +
theme_bw() +
theme(panel.grid = element_blank(),
text = element_text(size = 16),
axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"))
dens1 <- ggplot(df1, aes(x = cont)) +
geom_density(alpha = 0.4) +
theme_void() +
theme(legend.position = "none")
dens2 <- ggplot(df1, aes(x = int)) +
geom_density(alpha = 0.4) +
theme_void() +
theme(legend.position = "none") +
coord_flip()
dens1 + plot_spacer() + p1 + dens2 +
plot_layout(ncol = 2, nrow = 2, widths = c(6,1), heights = c(1,6))
From ?geom_density:
adjust: A multiplicate [sic] bandwidth adjustment. This makes it possible
to adjust the bandwidth while still using the a bandwidth
estimator. For example, ‘adjust = 1/2’ means use half of the
default bandwidth.
So as a start try e.g. geom_density(..., adjust = 2) (bandwidth twice as wide as default) and go from there.
I am plotting a smooth to my data using geom_smooth and using geom_ribbon to plot shaded confidence intervals for this smooth. No matter what I try I cannot get a single legend that represents both the smooth and the ribbon correctly, i.e I am wanting a single legend that has the correct colours and labels for both the smooth and the ribbon. I have tried using + guides(fill = FALSE), guides(colour = FALSE), I also read that giving both colour and fill the same label inside labs() should produce a single unified legend.
Any help would be much appreciated.
Note that I have also tried to reset the legend labels and colours using scale_colour_manual()
The below code produces the below figure. Note that there are two curves here that are essentially overlapping. The relabelling and setting couours has worked for the geom_smooth legend but not the geom_ribbon legend and I still have two legends showing which is not what I want.
ggplot(pred.dat, aes(x = age.x, y = fit, colour = tagged)) +
geom_smooth(size = 1.2) +
geom_ribbon(aes(ymin = lci, ymax = uci, fill = tagged), alpha = 0.2, colour = NA) +
theme_classic() +
labs(x = "Age (days since hatch)", y = "Body mass (g)", colour = "", fill = "") +
scale_colour_manual(labels = c("Untagged", "Tagged"), values = c("#3399FF", "#FF0033")) +
theme(axis.title.x = element_text(face = "bold", size = 14),
axis.title.y = element_text(face = "bold", size = 14),
axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
legend.text = element_text(size = 12))
The problem is that you provide new labels for the color-aesthetic but not for the fill-aesthetic. Consequently ggplot shows two legends because the labels are different.
You can either also provide the same labels for the fill-aesthetic (code option #1 below) or you can set the labels for the levels of your grouping variable ("tagged") before calling ggplot (code option #2).
library(ggplot2)
#make some data
x = seq(0,2*pi, by = 0.01)
pred.dat <- data.frame(x = c(x,x),
y = c(sin(x), cos(x)) + rnorm(length(x) * 2, 0, 1),
tag = rep(0:1, each = length(x)))
pred.dat$lci <- c(sin(x), cos(x)) - 0.4
pred.dat$uci <- c(sin(x), cos(x)) + 0.4
#option 1: set labels within ggplot call
pred.dat$tagged <- as.factor(pred.dat$tag)
ggplot(pred.dat, aes(x = x, y = y, color = tagged, fill = tagged)) +
geom_smooth(size = 1.2) +
geom_ribbon(aes(ymin = lci, ymax = uci), alpha = 0.2, color = NA) +
scale_color_manual(labels = c("untagged", "tagged"), values = c("#F8766D", "#00BFC4")) +
scale_fill_manual(labels = c("untagged", "tagged"), values = c("#F8766D", "#00BFC4")) +
theme_classic() + theme(legend.title = element_blank())
#option 2: set labels before ggplot call
pred.dat$tagged <- factor(pred.dat$tag, levels = 0:1, labels = c("untagged", "tagged"))
ggplot(pred.dat, aes(x = x, y = y, color = tagged, fill = tagged)) +
geom_smooth(size = 1.2) +
geom_ribbon(aes(ymin = lci, ymax = uci), alpha = 0.2, color = NA) +
theme_classic() + theme(legend.title = element_blank())
I have created a plot which shows two factors, each with two levels. I have shown these using colour and alpha aesthetics. My legend only shows these aesthetics separately. Is there a way to show all unique combinations of the aesthetics with alpha and colour combined? Thank you for your help!
Picture of plot compared with desired plot
Here is the relevant code (with some irrelevant parts removed):
ggplot(data, aes(x = Session, y = group_mean, group = FactorA:FactorB))+
geom_line(aes(colour = FactorA, alpha = FactorB, group = FactorA:FactorB), linetype = "solid", size = 0.7)+
geom_point(aes(y=group_mean, colour = FactorA, alpha = FactorB, group = FactorA:FactorB), shape = 18, size = 4, position=position_dodge(0.5))+
scale_fill_brewer(palette = "Set1", name = "FactorA")+
scale_colour_brewer(palette = "Set1")+
scale_alpha_manual(values = c(0.4, 0.8)) +
guides(colour = guide_legend(order = 1),
fill = guide_legend(order = 1),
alpha = guide_legend(order = 2))
I am trying to make a line graph using the following code:
ggplot(out2, aes(factor(out2$term, levels=unique(as.character(out2$term)) ),estimate, group = 1)) +
geom_line(aes(group = 1), size = 1.2) +
mytheme2 +
geom_point(shape = 21, colour = "black", fill = "white", size = 5, stroke = 2) +
scale_shape(solid = FALSE) +
theme(axis.text.x = element_text(angle = 50, hjust = 1, size = 15, family = "serif")) +
scale_x_discrete(labels = labels1) +
theme(plot.title = element_text(hjust = 0.5)) +
geom_ribbon(data=out2,aes(ymin=conf.low,ymax=conf.high),alpha=0.1)
Which gives me this graph:
However, based on a variable in the data frame called p.val I would like to add one asterisk if the value of p.val is less then .05, and two asterisks if the value is less than .001.
I tried to add a line at the bottom of the code to achieve this:
ggplot(out2, aes(factor(out2$term, levels=unique(as.character(out2$term)) ),estimate, group = 1)) +
geom_line(aes(group = 1), size = 1.2) +
mytheme2 +
geom_point(shape = 21, colour = "black", fill = "white", size = 5, stroke = 2) +
scale_shape(solid = FALSE) +
theme(axis.text.x = element_text(angle = 50, hjust = 1, size = 15, family = "serif")) +
scale_x_discrete(labels = labels1) +
#labs(y= "Standardized regression coefficient", x = "TAT threshold (Lux) minutes") +
#labs(title = "Sensitivity Analyses showing standardized regression coefficients for models with a range of \nTAT Light Thresholds (lux), Sleep Quality, Activity Level and BMI predicting T1 Hyperactivity.") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_ribbon(data=out2,aes(ymin=conf.low,ymax=conf.high),alpha=0.1) +
geom_point(data=out2[out2$p.value > 0.05,], color="red", size=3)
However, this gives me the error message:
Error: Aesthetics must be either length 1 or the same as the data (6): x, y, group
You are passing in a data frame to the last geom_point() layer that is a smaller subset of the original out2 and ggplot doesn't know how to distribute this shortened data across the original larger data, thus that warning.
It might be easier if you built a column in your data frame for the significance label first and then used geom_text() to layer it on instead of geom_point().
out2$signif_label <- ifelse(out2$p.value < .05, "*", "")
out2$signif_label <- ifelse(out2$p.value < .001, "**", out2$signif_label)
then add this instead of the last geom_point()
geom_text(aes(label = signif_label), color = "red", size = 3)
If you assign data in the initial ggplot(data = ,...) call then all subsequent layers will try to inherit the same data, so we don't need to assign it again, unless it's different.