Show empty groups even when all groups are empty - r

Using scale_x_discrete(drop = FALSE) I manage to keep empty groups on its place on the x-axis:
library(ggplot2)
iris_filtered <- subset(iris, Sepal.Length > 7)
ggplot(data = iris_filtered, mapping = aes(x = Species, y = Sepal.Width)) +
geom_boxplot() +
scale_x_discrete(drop = FALSE)
Except when all the groups are empty, I get:
iris_filtered <- subset(iris, Sepal.Length > 8)
ggplot(data = iris_filtered, mapping = aes(x = Species, y = Sepal.Width)) +
geom_boxplot() +
scale_x_discrete(drop = FALSE)
My hoped-for-output is:

You could just specify the x-axis limits:
iris_filtered <- subset(iris, Sepal.Length > 8)
ggplot(data = iris_filtered, mapping = aes(x = Species, y = Sepal.Width)) +
geom_boxplot() +
scale_x_discrete(drop = FALSE, limits = unique((iris$Species))
Similar approach to show the scale of the y-axis:
ggplot(data = iris_filtered, mapping = aes(x = Species, y = Sepal.Width)) +
geom_boxplot() +
scale_x_discrete(drop = FALSE, limits = c("a","b","c")) +
ylim(min(iris$Sepal.Length), max(iris$Sepal.Length))

Related

How to set alpha parameter for background in gganimate

I try to set alpha parameter 0.1 for background in my animation:
library(tidyverse)
library(gganimate)
mtcars_ <- rename(mtcars, mpg_ = mpg, disp_ = disp)
mtcars_$mpg = min(mtcars$mpg)
gg <- ggplot(mtcars, aes(x = mpg, y = disp)) + geom_density_2d_filled(data = mtcars_, aes(x = mpg_, y = disp_), alpha = 0.1) + geom_line() + theme(legend.position = "none")
gg
anim <- gg + transition_reveal(mpg) + shadow_wake(1)
anim
but alpha is 1 in final movie. How to fix it?
I need movie with this image
One way to do this would be to replicate the data you need for each frame. geom_density should see everything in every frame, but geom_line should only "see" the values up to the currently displayed value. We could accomplish that using tidyr::uncount to make copies of our data, and then creating a variable for geom_line that is NA when the value is too high for the current frame.
library(tidyverse)
library(gganimate)
distinct_mpg <- mtcars %>% distinct(mpg) %>% arrange(mpg) %>% pull(mpg)
mtcars_frames <- mtcars %>%
uncount(length(distinct_mpg), .id = "frame") %>%
mutate(mpg_reveal = distinct_mpg[frame],
mpg_shown = if_else(mpg <= mpg_reveal, mpg, NA_real_))
animate(
ggplot(mtcars_frames, aes(y = disp)) +
geom_density_2d_filled(aes(x = mpg), alpha = 0.1) +
geom_line(aes(x = mpg_shown, group = frame)) +
transition_states(frame) +
scale_fill_viridis_d(guide = NULL),
fps = 20
)
You might just want either to remove the shadow_wake() or set its wake_length closer to 0. The visual results will be similar, but the computation time will be higher for the shadow_wake() option.
gg1 <- ggplot(mtcars, aes(x = mpg, y = disp)) +
geom_density_2d_filled(data = mtcars_, aes(x = mpg_, y = disp_), alpha = 0.2) + geom_line() +
theme(legend.position = "none",
panel.background = element_blank())
gg1 + transition_reveal(mpg)
shadow_wake() removed
Or set shadow_wake to a lower setting.
gg2 <-
ggplot(data = mtcars, aes(x = mpg, y = disp)) +
geom_density_2d_filled(data = mtcars_ , aes(x = mpg_, y = disp_), alpha = 0.2) +
geom_line() +
theme(legend.position = "none",
panel.background = element_blank())
gg2 + transition_reveal(mpg) + shadow_wake(wake_length = 0.05)
shadow_wake() lowered

Creating a facet theme/design plot in ggplot2 without using facet_

Is there any possibility to create a facet_wrap looking plot in ggplot2 without using facet_wrap() The reason I would like to achieve this is to match some other design. In the plot without_facet below, can I somehow add "Setosa" in the top, so it looks like the with_facet plot, without using facet_wrap.
library(ggplot2)
df <- iris[iris$Species == 'setosa', ]
with_facet <- ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point() +facet_wrap(~Species)
with_facet
without_facet <- ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()
You can try
ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point() +
ggtitle("setosa") +
theme(plot.title = element_text(hjust = 0.5))
A more "hackish"-one could be this hardcoded approach:
ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point() +
ggtitle("setosa") +
geom_rect(xmin = 4.225, xmax = 5.875 , ymin=4.5, ymax=4.6, fill ="lightgrey") +
coord_cartesian(clip = 'off', expand = 0.05) +
theme(plot.title = element_text(hjust = 0.5, size = 12),
plot.margin = margin(t = 30, r = 20, b = 20, l = 20, unit = "pt"))
From Paleolimbot answer on Gitbub (https://github.com/tidyverse/ggplot2/issues/2344)
element_textbox <- function(...) {
el <- element_text(...)
class(el) <- c("element_textbox", class(el))
el
}
element_grob.element_textbox <- function(element, ...) {
text_grob <- NextMethod()
rect_grob <- element_grob(calc_element("strip.background", theme_bw()))
ggplot2:::absoluteGrob(
grid::gList(
element_grob(calc_element("strip.background", theme_bw())),
text_grob
),
height = grid::grobHeight(text_grob),
width = grid::unit(1, "npc")
)
}
From my original question, I added theme_bw()
library(ggplot2)
library(gridExtra)
df <- iris[iris$Species == 'setosa', ]
with_facet <- ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point() +
facet_wrap(~Species) +
theme(plot.background = element_rect(color = 'black')) + theme_bw()
without_facet <- ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point() +
ggtitle("setosa") +
theme_bw() +
theme(
plot.title = element_textbox(
hjust = 0.5, margin = margin(t = 5, b = 5), size = 10
),
)
grid.arrange(with_facet, without_facet)
Not identical, but works for my purpose.
This might be one option:
library(ggplot2)
df <- iris[iris$Species == 'setosa', ]
# with annotate:
with_annotate <-
ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point() +
annotate('text', x = 5, y = 4.7, label = "setosa", size = 12)
with_annotate
#or if you do not want the heading to print over the plot area
with_coord_cart <-
ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point() +
annotate('text', x = 5, y = 4.7, label = "setosa", size = 8)+
coord_cartesian(ylim = c(2, 4.5), clip = 'off') +
theme(plot.margin = margin(2, 1, 1, 1, "lines"))
with_coord_cart
Which gives you:
Note: I had deleted it because it seemed no longer relevant after the original question was updated.
I am not sure I understand correctly. In case you want to arrange different plots together:
library(gridExtra)
grid.arrange(without_facet,
without_facet,
without_facet,
without_facet, nrow = 2)

Plotting in layers in R

I'm trying to plot individual regression lines for all of my experimental subjects (n=40) on the same plot where I show the overall regression line.
I can do the plots separately with ggplot, but I haven't found a way to superpose them on the same graph.
I can illustrate what I did with the iris data frame:
#first plot
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
theme_classic()
# second plot, grouped by species
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, colour =Species)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
theme_classic()
# and I've been trying things like this:
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
theme_classic() +
geom_point(aes(x = Sepal.Width, y = Sepal.Length, colour =Species))) +
stat_smooth(method = lm, se = FALSE) +
theme_classic()
which returns the message "Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?", so I get that this is not the right way to combine them, but what is?
How can I combine both graphs in one?
Thanks in advance!
Repeat the whole data and set Species to be something else ("Together") in example below. Attach the repeated data to the original data and just call the second plot.
d1 = iris
d2 = rbind(d1, transform(d1, Species = "Together"))
ggplot(d2, aes(x = Sepal.Width, y = Sepal.Length, colour =Species)) +
stat_smooth(method = lm, se = FALSE) +
geom_point(data = d1) +
theme_classic()
Similar to #d.b's answer, consider expanding the data frame with rbind, assigning an "All" category for Species and adjust for factor levels (so All shows at top on legend):
new_species_level <- c("All", unique(as.character(iris$Species)))
iris_expanded <- rbind(transform(iris, Species=factor("All", levels=new_species_level)),
transform(iris, Species=factor(Species, levels=new_species_level)))
ggplot(iris_expanded, aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
theme_classic()

Ignoring unknown aesthetics: fill

There was a warning when entering values ​​in the boxplot using geom_text.
As a result of studying, it is judged that there is no 'fill' option in aes of geom_text.
I want to know what I should do to get rid of the warning message.
means <- aggregate(d13C~Species, data=scat, meam)
means$d13C <- round(means$d13C,2)
ggplot(data=scat,
mapping=aes(x=scat$Species, y=scat$d13C, fill=scat$Species)) +
geom_boxplot() +
stat_summary(fun.y=mean, colour='darkred', geom="point",
shape=3, size=3, show.legend=F) +
geom_text(data=means,
aes(x=means$Species, y = d13C+1, label=d13C, fill=Species))
Try the following
means <- aggregate(d13C ~ Species, data = scat, mean) # there was a typo here
means$d13C <- round(means$d13C, 2)
ggplot(data = scat, aes(x = Species, y = d13C)) +
geom_boxplot(aes(fill = Species)) +
stat_summary(
fun.y = mean,
colour = 'darkred',
geom = "point",
shape = 3,
size = 3,
show.legend = F
) +
geom_text(data = means, aes(x = Species, y = d13C + 1, label = d13C))
If not working properly, please share a minimal reproducible dataset.
A general advise: Don't write ggplot(data = scat, aes(x = scat$Species, y = scat$d13C)) + ... but use the bare column names in aes.

All my plots in Rstudio just show up as gray boxes?

I am using ggplot and geoms to show my data, but the plot sidebar area just shows a gray box with the x and y axis correctly labeled.
Here is the output image:
The code which made the plot:
ggplot(Wc, aes(y = popsafe, x = rnground)) +
geom_jitter(aes(col = me)) +
geom_smooth(method = "lm", se = FALSE, col = "black")
Looks like your dataset is empty. We don't know what your dataset contains, so here an example with the built-in iris dataset. First a proper plot, using the same geoms and mappings you use:
library(ggplot2)
ggplot(iris, aes(y = Sepal.Length, x = Sepal.Width)) +
geom_jitter(aes(col = Species)) +
geom_smooth(method = "lm", se = FALSE, col = "black")
Now I remove all the data from the dataset and replot:
library(dplyr)
iris_empty <- filter(iris, Sepal.Length < 0)
ggplot(iris_empty, aes(y = Sepal.Length, x = Sepal.Width)) +
geom_jitter(aes(col = Species)) +
geom_smooth(method = "lm", se = FALSE, col = "black")
A simple head(Wc) would confirm whether your dataset actually contains any data.

Resources