I use the following code to create a boxplot:
plot <- ggplot(WL, aes(y = wavelength, x = factor(category, level = c("A", "B")))) +
theme_bw() +
geom_boxplot(outlier.colour = "gray30", outlier.shape = 8, outlier.size = 2, lwd=1, fill = c("#C6DBEF", "#FEE391")) +
ylab(expression(lambda[(km)])) +
theme(plot.margin = unit(c(2,2,2,2), "cm"),
axis.title.x = element_blank(),
axis.title.y=element_text(size=20),
plot.title = element_text(size = 22, vjust = 2))
How can I add a legend? I want to place it inside the plot (topright). It should show the two colors and a description.
I am trying to use legends() which does not work for me.
The issue is where you specified the fill - The following code will give you a legend, and changing the position in the last line will allow you to change it where you want:
# sample data
WL <- data.frame(wavelength = rnorm(100, 0,1),
category = sample(LETTERS[1:2], 100, replace = TRUE))
# plot
ggplot(WL, aes(y = wavelength, x = factor(category, level = c("A", "B")), fill = category)) +
theme_bw() +
geom_boxplot(outlier.colour = "gray30", outlier.shape = 8, outlier.size = 2, lwd = 1) +
ylab(expression(lambda[(km)])) +
theme(plot.margin = unit(c(2,2,2,2), "cm"),
axis.title.x = element_blank(),
axis.title.y=element_text(size=20),
plot.title = element_text(size = 22, vjust = 2)) +
scale_fill_manual(values = c("#C6DBEF", "#FEE391")) +
theme(legend.position = "bottom")
Related
I have a data frame with three groups (group1, group2, group3). I would like to show the p-value of their mean comparisons in ggplot2 which I can do however, the values are stacked ontop of one another making it difficult to see what is being compared. When I try to adjust where the p-values are located using the y_position() function, the boxplots collapse (I think because the y-axis is log10) but the p-values are no longer stacked ontop of one another. How can I keep the boxplots from collapsing and keep the p-values displayed so that you can see what is being compared?
Example data
library(ggplot2)
library(dplyr)
library(ggsignif)
df <- data.frame(matrix(ncol = 2, nrow = 30))
colnames(df)[1:2] <- c("group", "value")
df$group <- rep(c("group1","group2","group3"), each = 10)
df[1:10,2] <- rexp(10, 1/10)
df[11:20,2] <- rexp(10, 1/100)
df[21:30,2] <- rexp(10, 1/900)
# Need to say what should be compared for p-value determination
my_comparisons <- list(c("group1", "group2"),
c("group1", "group3"),
c("group2", "group3"))
Boxplots showing the distribution of value for each group however the p-values are ontop of one another so you cannot compare among groups.
df %>%
mutate(group = factor(group, levels = c("group3","group2","group1"))) %>%
ggplot(aes(x = group, y = value)) +
geom_signif(comparisons = my_comparisons,
map_signif_level = function(x) paste("p =", scales::pvalue(x))) +
scale_y_log10() +
geom_boxplot(outlier.colour="white", outlier.fill = "white", outlier.shape = 1, outlier.size = 0) +
geom_jitter(shape=1, position=position_jitter(0.2), color = "black", fill = "white", size = 2) +
labs(x = "",
y = "value") +
theme_bw() +
theme(axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"),
axis.title = element_text(size = 16, color = "black"),
axis.title.x = element_text(vjust = -0.5),
panel.grid = element_blank(),
panel.background = element_blank())
Adjusting the y_position() of where the p-values should display but this collapses the y-axis. I have tried several values within y_position.
df %>%
mutate(group = factor(group, levels = c("group3","group2","group1"))) %>%
ggplot(aes(x = group, y = value)) +
geom_signif(y_position = c(2000,1800,1600),
comparisons = my_comparisons,
map_signif_level = function(x) paste("p =", scales::pvalue(x))) +
scale_y_log10() +
geom_boxplot(outlier.colour="white", outlier.fill = "white", outlier.shape = 1, outlier.size = 0) +
geom_jitter(shape=1, position=position_jitter(0.2), color = "black", fill = "white", size = 2) +
labs(x = "",
y = "value") +
theme_bw() +
theme(axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"),
axis.title = element_text(size = 16, color = "black"),
axis.title.x = element_text(vjust = -0.5),
panel.grid = element_blank(),
panel.background = element_blank())
For some reason this parameter ignores the axis transformation. You therefore need to use the log10 values of the desired positions:
df %>%
mutate(group = factor(group, levels = c("group3","group2","group1"))) %>%
ggplot(aes(x = group, y = value)) +
geom_signif(comparisons = my_comparisons,
y_position = log10(c(5000, 10000, 25000)),
map_signif_level = function(x) paste("p =", scales::pvalue(x))) +
scale_y_log10() +
geom_boxplot(outlier.colour="white", outlier.fill = "white",
-outlier.shape = 1, outlier.size = 0) +
geom_jitter(shape=1, position=position_jitter(0.2), color = "black",
fill = "white", size = 2) +
labs(x = "",
y = "value") +
theme_bw() +
theme(axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"),
axis.title = element_text(size = 16, color = "black"),
axis.title.x = element_text(vjust = -0.5),
panel.grid = element_blank(),
panel.background = element_blank())
I am trying to make a horizontal bar chart in ggplot2 where the bars are of equal width and with text labels centered on the bars. There are two groups on the y axis -- one with 2 bars, and one with three.
There are a lot of similar questions on SO that address both of these issues, but I haven't been able to fix one without breaking the other. Here's my data:
## data
df <- tibble(var1 = c("a", "b", "b", "c", "c"),
var2 = c("x", "y", "x", "y", "x"),
proportion = c(100, 33.3, 66.7, 66.7, 33.3)) %>%
mutate(var1 = factor(var1, levels = var1_order))
var1_order <- c("a", "c", "b")
Here's an example where the widths are good, but the labels of the y group are off:
## labels bad
df %>%
ggplot(aes(x = proportion, y = var2, fill = var1,
label = paste0(round(proportion, 1), "%"))) +
geom_col(position = position_dodge2(preserve = "single", padding = 0), width = .9) +
geom_text(size = 3, position = position_dodge2(width = 0.9), hjust = -.5,
color = "black", aes(group = var1)) +
scale_fill_manual(name = "", values = c("#093D6E","#5D8AA8", "#00918B",
"#F8AF54", "#CD9575")) +
labs(x = NULL) +
theme(axis.ticks = element_blank(),
axis.title.y = element_blank(),
axis.line=element_blank(),
axis.text.x = element_blank(),
panel.background = element_blank(),
strip.text = element_text(size = 7, face = "bold")) +
scale_x_continuous(expand = c(.2, .2)) +
guides(fill = guide_legend(reverse = TRUE))
And here's an example where the labels are good but the widths are now off:
## col widths bad
df %>%
ggplot(aes(x = proportion, y = var2, fill = var1,
label = paste0(round(proportion, 1), "%"))) +
geom_col(position = position_dodge(width = 0.9)) +
geom_text(size = 3, position = position_dodge(width = 0.9), hjust = -.5,
color = "black", aes(group = var1)) +
scale_fill_manual(name = "", values = c("#093D6E","#5D8AA8", "#00918B",
"#F8AF54", "#CD9575")) +
labs(x = NULL) +
theme(axis.ticks = element_blank(),
axis.title.y = element_blank(),
axis.line=element_blank(),
axis.text.x = element_blank(),
panel.background = element_blank(),
strip.text = element_text(size = 7, face = "bold")) +
scale_x_continuous(expand = c(.2, .2)) +
guides(fill = guide_legend(reverse = TRUE))
Note that this will be part of a parameterized report, so it needs to be capable of dealing with different numbers of var1 and var2 groups. Thanks!
Try this approach. You can use position_dodge2() to keep uniform bars. Here the code:
library(ggplot2)
#Code
df %>%
ggplot(aes(x = proportion, y = var2, fill = var1,
label = paste0(round(proportion, 1), "%"))) +
geom_col(position = position_dodge2(preserve = 'single',width = 0.9)) +
geom_text(size = 3, position = position_dodge2(preserve = 'single',width = 0.9), hjust = -.5,
color = "black", aes(group = var1)) +
scale_fill_manual(name = "", values = c("#093D6E","#5D8AA8", "#00918B",
"#F8AF54", "#CD9575")) +
labs(x = NULL) +
theme(axis.ticks = element_blank(),
axis.title.y = element_blank(),
axis.line=element_blank(),
axis.text.x = element_blank(),
panel.background = element_blank(),
strip.text = element_text(size = 7, face = "bold")) +
scale_x_continuous(expand = c(.2, .2)) +
guides(fill = guide_legend(reverse = TRUE))
Output:
I am trying to plot a 2y-axes plot; on the left, the actual values, and on the right, the % values. In addition to this, I need to apply coord_trans on the left y-axis for a better visualization of small values. However, when I do it, the labels on the right do not show up.
Here the data (example)
Here the code
DAXIS <- ggplot(x1, aes(hour, value_T, colour=season, linetype = variable)) +
geom_line(size = 1) +
scale_linetype_manual(c("var"), values=c("solid", "dashed", "dotted"))+ # here to change one name
geom_point(aes(shape = season), size = 1)+
labs(x = "hour", y = "T") +
scale_x_continuous(breaks = c(0, 6, 12, 18, 23), labels= c(0, 6, 12, 18, 24))+
scale_y_continuous("T",
sec.axis = sec_axis(~./2.341598, name = " [%] ",
breaks=c(0.2135294, 0.4270588, 0.6405882, 0.8541176,1),
labels = function(b) { paste0(round(b * 100, 0), "%")}))+
#coord_trans(y = "log10", breaks=c(0.5,1,1.5,1.903738), labels = c(0.5,1,1.5,1.9))+ # attemp 1
#coord_trans(y = "log10")+ # attemp 2
scale_color_aaas()+
theme_bw()+
theme(legend.direction = "horizontal", legend.position = "bottom", legend.key = element_blank(),
legend.background = element_rect(fill = "white", colour = "gray30")) +
theme(legend.position="bottom",
text=element_text(size=18),
axis.text.x = element_text(size=15),
axis.text.y = element_text(size=15))
DAXIS
Here the output without coord_trans
Here the output with coord_trans
Any help is very much appreciated
In this plot i've to deal with the following problem:
I want to increaze the size of the white dividing lines between the squares. The plot code is:
p <- ggplot(long_form_mittelwerte2, aes(Var1, Var2)) +
geom_tile(aes(fill = Rang), colour = "white")
pneu2 <- p +
scale_fill_gradient(low = "white", high = "blue", limits= c(1, 3), breaks = c(1, 2, 3)) +
geom_text(aes(label = mittelwerte_text2$value), size = 12, color = "gray0") +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank()) +
theme(axis.text.y = element_text(size = 30, color = "black"),
axis.text.x = element_text(size = 30, color = "black")) +
scale_y_discrete(labels = c(h_Filter3x3="3x3", h_Filter3x5="3x5", h_Filter3x9="3x9"),
expand = c(0, 0)) +
scale_x_discrete(expand = c(0, 0)) +
coord_fixed(ratio = 1) +
guides(fill= guide_colorbar(barheight = unit(10, "cm"))) +
theme(legend.text = element_text(size = 30, face = "bold"),
legend.title = element_blank())
If you need data, please let me know. Thanks for helping.
You can set the size and colour arguments inside geom_tile to adjust the space and colour between the tiles:
mtcars %>%
ggplot(aes(cyl, am, fill = mpg)) +
geom_tile(colour = "white", size = 4)
Could someone explain to me on how to get full control over the legends in ggplot2 with two data frames with different x-scales presented in two different geoms. The 'name1' and 'name2' is a function that are created with other filtering function.
1.
Why geom_point shape appears in the legend for "Group 1"? I expect the legend will show only colour in Group1 and shape for Group2.
Is it possible to rearrange the legends as well? i.e Group2 appears first in the row.
df1 <- data.frame(g1 = c("a", "b", "c", "e"),
y1 = c(12, 8, 3, 20))
df2 <- data.frame(g1 = letters[1:5],
y1 = 20:24)
name1 <- "Group 1"
name2 <- "Group 2"
require(ggplot2)
ggplot(NULL, aes(x=g1, y=y1)) +
geom_bar(data = df1, stat = "identity",
aes(fill=factor(name1))) +
geom_point(data = df2, stat = "identity",
size = 5, shape = 2, aes(fill=factor(name2))) +
theme(plot.margin = unit(c(2,1,1,1), "lines"),
plot.title = element_text(hjust = 0, size=18),
axis.title = element_text(face = "bold", size = 12),
legend.position = 'top',
legend.text = element_text(size = 12),
legend.title = element_blank())
The key is to define fill and shape in both aes(). You can then define the shape and fill as NA for the one you don't need.
ggplot(NULL, aes(x=g1, y=y1)) +
geom_bar(data = df1, stat = "identity", aes(fill=name2, shape=name2)) +
geom_point(data = df2, size = 5, aes(shape=name1, fill=name1)) +
theme(plot.margin = unit(c(2,1,1,1), "lines"),
plot.title = element_text(hjust = 0, size=18),
axis.title = element_text(face = "bold", size = 12),
legend.position = 'top',
legend.text = element_text(size = 12),
legend.title = element_blank()) +
scale_shape_manual(values=c(2, NA)) +
scale_fill_manual(values=c(NA, "red")) +
guides(fill = guide_legend(reverse = TRUE),
shape = guide_legend(reverse = TRUE))